IMP logo
IMP Reference Guide  2.10.1
The Integrative Modeling Platform
CreateLogContext.h
Go to the documentation of this file.
1 /**
2  * \file IMP/CreateLogContext.h
3  * \brief Logging and error reporting support.
4  *
5  * Copyright 2007-2018 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_CREATE_LOG_CONTEXT_H
10 #define IMPKERNEL_CREATE_LOG_CONTEXT_H
11 
12 #include <IMP/kernel_config.h>
13 #include "raii_macros.h"
14 #include "log.h"
15 #include <IMP/nullptr.h>
16 #include "Object.h"
17 #if IMP_KERNEL_HAS_LOG4CXX
18 #include <log4cxx/ndc.h>
19 #include <boost/scoped_ptr.hpp>
20 #endif
21 
22 IMPKERNEL_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_KERNEL_HAS_LOG4CXX
50  log4cxx::NDC ndc0_;
51  boost::scoped_ptr<log4cxx::NDC> ndc1_;
52 
53  public:
54  CreateLogContext(const char* fname, const Object* object)
55  : ndc0_(object->get_quoted_name_c_string()),
56  ndc1_(new log4cxx::NDC(fname)) {}
57  CreateLogContext(const char* fname) : ndc0_(fname) {}
58  CreateLogContext(std::string name) : ndc0_(name) {}
60 #else
61  bool pushed_;
62  std::string name_;
63 
64  public:
65  CreateLogContext(std::string fname, const Object* object = nullptr)
66  : pushed_(true), name_(fname) {
67  // push log context does not copy the string, so we need to save it.
68  push_log_context(name_.c_str(), object);
69  }
71  (const char* fname, const Object* object = nullptr), ,
72  { push_log_context(fname, object);
73  pushed_ = true; },
74  { if (pushed_) pop_log_context();
75  pushed_ = false; }, );
76 #endif
77 };
78 
79 /** @} */
80 
81 IMPKERNEL_END_NAMESPACE
82 
83 #endif /* IMPKERNEL_CREATE_LOG_CONTEXT_H */
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Temporarily change something; undo the change when this object is destroyed.
Definition: RAII.h:26
#define IMP_RAII(Name, args, Initialize, Set, Reset, Show)
Declares RAII-style methods in a class.
Definition: raii_macros.h:34
Provide a nullptr keyword analog.
Common base class for heavy weight IMP objects.
Definition: Object.h:106
#define IMP_UNUSED(variable)
A shared base class to help in debugging and things.
Logging and error reporting support.
Create a new log context.
Various general useful macros for IMP.