IMP  2.0.1
The Integrative Modeling Platform
CreateLogContext.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/CreateLogContext.h
3  * \brief Logging and error reporting support.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_CREATE_LOG_CONTEXT_H
10 #define IMPBASE_CREATE_LOG_CONTEXT_H
11 
12 #include <IMP/base/base_config.h>
13 #include "raii_macros.h"
14 #include "log.h"
15 #include <IMP/base/nullptr.h>
16 #include "Object.h"
17 #if IMP_BASE_HAS_LOG4CXX
18 #include <log4cxx/ndc.h>
19 #include <boost/scoped_ptr.hpp>
20 #endif
21 
22 IMPBASE_BEGIN_NAMESPACE
23 
24 //! Create a new log context
25 /** The following produces
26 
27  \verbatim
28  myfunction:
29  1
30  2
31  3
32  \endverbatim
33 
34  \code
35  {
36  CreateLogContext ii("myfunction ");
37  IMP_LOG_VERBOSE( 1);
38  IMP_LOG_VERBOSE( 2);
39  IMP_LOG_VERBOSE( 3);
40  }
41  IMP_LOG_VERBOSE( "Now it is has ended." << std::endl);
42  \endcode
43 
44  The more interesting use is that you can use it before
45  calling a function to ensure that all the output of that
46  function is nicely offset.
47 */
48 class CreateLogContext: public RAII {
49 #if IMP_BASE_HAS_LOG4CXX
50  log4cxx::NDC ndc0_;
51  boost::scoped_ptr<log4cxx::NDC> ndc1_;
52 public:
53  CreateLogContext(const char * fname, const Object* object):
54  ndc0_(object->get_quoted_name_c_string()),
55  ndc1_(new log4cxx::NDC(fname)) {
56  }
57  CreateLogContext(const char * fname):
58  ndc0_(fname) {
59  }
60  CreateLogContext(std::string name):
61  ndc0_(name) {
62  }
64 #else
65  std::string name_;
66 public:
67  CreateLogContext(std::string fname, const void* object=nullptr):
68  name_(fname) {
69  // push log context does not copy the string, so we need to save it.
70  push_log_context(name_.c_str(), object);
71  }
72  IMP_RAII(CreateLogContext, (const char *fname, const void* object=nullptr),,
73  push_log_context(fname, object),
74  pop_log_context(),);
75 #endif
76 };
77 
78 /** @} */
79 
80 IMPBASE_END_NAMESPACE
81 
82 
83 #endif /* IMPBASE_CREATE_LOG_CONTEXT_H */