IMP logo
IMP Reference Guide  2.20.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-2022 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 "Object.h"
16 #if IMP_KERNEL_HAS_LOG4CXX
17 #include <log4cxx/ndc.h>
18 #include <boost/scoped_ptr.hpp>
19 #endif
20 
21 IMPKERNEL_BEGIN_NAMESPACE
22 
23 //! Create a new log context
24 /** The following produces
25 
26  \verbatim
27  myfunction:
28  1
29  2
30  3
31  \endverbatim
32 
33  \code
34  {
35  CreateLogContext ii("myfunction ");
36  IMP_LOG_VERBOSE( 1);
37  IMP_LOG_VERBOSE( 2);
38  IMP_LOG_VERBOSE( 3);
39  }
40  IMP_LOG_VERBOSE( "Now it is has ended." << std::endl);
41  \endcode
42 
43  The more interesting use is that you can use it before
44  calling a function to ensure that all the output of that
45  function is nicely offset.
46  */
47 class CreateLogContext : public RAII {
48 #if IMP_KERNEL_HAS_LOG4CXX
49  log4cxx::NDC ndc0_;
50  boost::scoped_ptr<log4cxx::NDC> ndc1_;
51 
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  CreateLogContext(const char* fname) : ndc0_(fname) {}
57  CreateLogContext(std::string name) : ndc0_(name) {}
59 #else
60  bool pushed_;
61  std::string name_;
62 
63  public:
64  CreateLogContext(std::string fname, const Object* object = nullptr)
65  : pushed_(true), name_(fname) {
66  // push log context does not copy the string, so we need to save it.
67  push_log_context(name_.c_str(), object);
68  }
70  (const char* fname, const Object* object = nullptr), ,
71  { push_log_context(fname, object);
72  pushed_ = true; },
73  { if (pushed_) pop_log_context();
74  pushed_ = false; }, );
75 #endif
76 };
77 
78 /** @} */
79 
80 IMPKERNEL_END_NAMESPACE
81 
82 #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:28
#define IMP_RAII(Name, args, Initialize, Set, Reset, Show)
Declares RAII-style methods in a class.
Definition: raii_macros.h:34
Common base class for heavy weight IMP objects.
Definition: Object.h:111
#define IMP_UNUSED(variable)
A shared base class to help in debugging and things.
Logging and error reporting support.
Create a new log context.
Macros to aid in writing RAII-style classes.