IMP  2.2.0
The Integrative Modeling Platform
base/log.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/log.h
3  * \brief Logging and error reporting support.
4  *
5  * Copyright 2007-2014 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_LOG_H
10 #define IMPBASE_LOG_H
11 
12 #include <IMP/base/base_config.h>
13 #include "enums.h"
14 #include "WarningContext.h"
15 #include "Flag.h"
16 #include <string>
17 #if IMP_BASE_HAS_LOG4CXX
18 #include <log4cxx/logger.h>
19 #endif
20 
21 IMPBASE_BEGIN_NAMESPACE
22 
23 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
24 namespace internal {
25 extern IMPBASEEXPORT Flag<LogLevel, IMP_HAS_LOG != IMP_SILENT> log_level;
26 }
27 #endif
28 
29 /** \name Logging
30  \anchor log
31  \imp provides tools for controlling the amount of log output produced
32  and directing it to the terminal or a file. Only log messages tagged
33  with a lower level than the current LogLevel are emitted. In addition
34  to a global log level (get_log_level(), set_log_level()), each
35  IMP::Object has an internal log level (IMP::Object::get_log_level(),
36  IMP::Object::set_log_level()) which is used when executing code on
37  that object.
38 
39  Logging is provided by IMP/base/log.h.
40 
41  People implementing IMP::Object classes should also see IMP_OBJECT_LOG()
42  and IMP::SetLogState.
43 
44  All logging is disabled when \imp is built using \c build='fast'.
45  @{
46  */
47 #ifndef IMP_DOXYGEN
48 #if !IMP_BASE_HAS_LOG4CXX
49 //! Push a new log context onto the stack
50 /** A log context is, eg, a function name.
51  */
52 IMPBASEEXPORT void push_log_context(const char *functionname,
53  const void *object);
54 
55 //! pop the top log context
56 IMPBASEEXPORT void pop_log_context();
57 
58 //! Write a string to the log
59 IMPBASEEXPORT void add_to_log(std::string to_write);
60 #endif
61 #endif
62 
63 //! Write a string to the log, for python
64 IMPBASEEXPORT void add_to_log(LogLevel level, std::string to_write);
65 
66 //! Set the current global log level
67 /** Set the current global log level
68 
69  @note may be overriden by set_log_level of specific objects that inherit
70  from IMP::base::Object
71  @note this global method should not, currently, be used directly during
72  kernel::Model::evaluate() calls.
73 
74  \see get_log_level()
75  \see IMP::base::Object::set_log_level()
76 */
77 IMPBASEEXPORT void set_log_level(LogLevel l);
78 
79 //! Set whether log messages are tagged with the current log time
80 IMPBASEEXPORT void set_log_timer(bool tb);
81 
82 //! Reset the log timer
83 IMPBASEEXPORT void reset_log_timer();
84 
85 //! Get the currently active global log level
86 /** Get the currently active global log level
87 
88  @note This may not always match the value passed to set_log_level()
89  as objects can temporarily override the global level
90  while they are evaluating.
91 
92  \see set_log_level()
93  \see IMP::base::Object::set_log_level()
94 
95  */
96 inline LogLevel get_log_level() { return LogLevel(internal::log_level); }
97 
98 #if IMP_BASE_HAS_LOG4CXX
99 inline log4cxx::LoggerPtr get_logger() {
100  static log4cxx::LoggerPtr ret = log4cxx::Logger::getLogger("IMP");
101  return ret;
102 }
103 #else
104 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
105 inline bool get_is_log_output(LogLevel l) { return l <= get_log_level(); }
106 #endif
107 #endif
108 
109 /** @} */
110 
111 /** \name Create a progress bar in the terminal
112 
113  @{
114  */
115 /** Set up the progress bar with the passed description.
116 
117  See IMP_PROGRESS_DISPLAY().
118 */
119 IMPBASEEXPORT void set_progress_display(std::string description,
120  unsigned int steps);
121 /** Set the current progress. When it equals the number of steps,
122  the bar is done.*/
123 IMPBASEEXPORT void add_to_progress_display(unsigned int step = 1);
124 
125 /** @} */
126 
127 IMPBASE_END_NAMESPACE
128 
129 #endif /* IMPBASE_LOG_H */
void set_log_timer(bool tb)
Set whether log messages are tagged with the current log time.
LogLevel
The log levels supported by IMP.
Definition: base/enums.h:20
void reset_log_timer()
Reset the log timer.
void set_log_level(LogLevel l)
Set the current global log level.
void set_progress_display(std::string description, unsigned int steps)
LogLevel get_log_level()
Get the currently active global log level.
Definition: base/log.h:96
void add_to_log(LogLevel level, std::string to_write)
Write a string to the log, for python.
Various general useful macros for IMP.
Basic types used by IMP.
void add_to_progress_display(unsigned int step=1)
Logging and error reporting support.