IMP  2.2.1
The Integrative Modeling Platform
base/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-2014 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 
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  std::string name_;
62 
63  public:
64  CreateLogContext(std::string fname, const Object* object = nullptr)
65  : 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), pop_log_context(), );
72 #endif
73 };
74 
75 /** @} */
76 
77 IMPBASE_END_NAMESPACE
78 
79 #endif /* IMPBASE_CREATE_LOG_CONTEXT_H */
#define IMP_UNUSED(variable)
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Provide a nullptr keyword analog.
Various general useful macros for IMP.
Common base class for heavy weight IMP objects.
Definition: base/Object.h:106
#define IMP_RAII(Name, args, Initialize, Set, Reset, Show)
Declares RAII-style methods in a class.
Logging and error reporting support.
A shared base class to help in debugging and things.
Create a new log context.