CYCLUS
logger.h
Go to the documentation of this file.
1 /**
2  @file logger.h
3 
4  Code providing rudimentary logging capability for the Cyclus core.
5  Details outlining proper use of this logging functionality can be found
6  at http://cyclus.github.com/devdoc/main.html
7  */
8 
9 #ifndef CYCLUS_SRC_LOGGER_H_
10 #define CYCLUS_SRC_LOGGER_H_
11 
12 
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 #include <vector>
17 #include <map>
18 
19 namespace cyclus {
20 
21 /// @def LOG(level, prefix)
22 ///
23 /// allows easy logging via the streaming operator similar to std::cout;
24 /// this is the primary way to use the Logging functionality.
25 ///
26 /// @param level LogLevel category or type of log statement.
27 ///
28 /// @param prefix A std::string value that functions as a unique
29 /// identifier for the module. Prefixes longer than 6 characters will be
30 /// truncated.
31 ///
32 /// @warning do not place any state-changing expressions with this macro
33 /// as they may not run if the report level excludes the specified log
34 /// 'level'.
35 #define LOG(level, prefix) \
36  if ((level > cyclus::Logger::ReportLevel()) | cyclus::Logger::NoAgent()) ; \
37  else cyclus::Logger().Get(level, prefix)
38 
39 #define CLOG(level) \
40  if (level > cyclus::Logger::ReportLevel()) ; \
41  else cyclus::Logger().Get(level, "core")
42 
43 #define MLOG(level) \
44  if ((level > cyclus::Logger::ReportLevel()) | cyclus::Logger::NoMem()) ; \
45  else cyclus::Logger().Get(level, "memory")
46 
47 /// @enum LogLevel
48 ///
49 /// categorical (verbosity) levels for log statements.
50 enum LogLevel {
51  LEV_ERROR, //!< Use for errors that require agent code or input file modification (use extremely sparingly)
52  LEV_WARN, //!< Use to report questionable simulation state (use extremely sparingly)
53  LEV_INFO1, //!< Information helpful for simulation users and developers alike - least verbose.
54  LEV_INFO2, //!< Information helpful for simulation users and developers alike.
55  LEV_INFO3, //!< Information helpful for simulation users and developers alike.
56  LEV_INFO4, //!< Information helpful for simulation users and developers alike.
57  LEV_INFO5, //!< Information helpful for simulation users and developers alike - most verbose.
58  LEV_DEBUG1, //!< debugging information - least verbose
59  LEV_DEBUG2, //!< debugging information
60  LEV_DEBUG3, //!< debugging information
61  LEV_DEBUG4, //!< debugging information
62  LEV_DEBUG5 //!< debugging information - most verbose
63 };
64 
65 /// A logging tool providing finer grained control over standard output
66 /// for debugging and other purposes.
67 ///
68 /// @warning do not place any state-changing expressions with the LOG
69 /// macro as they may not run if the report level excludes the specified level.
70 class Logger {
71  public:
72  Logger() {}
73  virtual ~Logger();
74 
75  /// Returns a string stream by reference that is flushed to stdout by
76  /// the Logger class destructor.
77  std::ostringstream& Get(LogLevel level, std::string prefix);
78 
79  /// Use to get/set the (global) log level report cutoff.
80  /// @return the report level cutoff by reference
81  static LogLevel& ReportLevel() {
82  return report_level;
83  }
84 
85  /// Sets the report level. This is here because Cython cannot assign
86  /// to a reference from a function return yet.
87  static void SetReportLevel(LogLevel level) {
88  report_level = level;
89  }
90 
91  /// Set whether or not agent/agent log entries should be printed
92  static bool& NoAgent() {
93  return no_agent;
94  }
95 
96  /// Sets whether or not agent/agent log entries should be printed.
97  /// This is here because Cython cannot assign
98  /// to a reference from a function return yet.
99  static void SetNoAgent(bool na) {
100  no_agent = na;
101  }
102 
103  /// Set whether or not agent/agent log entries should be printed
104  static bool& NoMem() {
105  return no_mem;
106  }
107 
108  /// Sets whether or not agent/agent log entries should be printed.
109  /// This is here because Cython cannot assign
110  /// to a reference from a function return yet.
111  static void SetNoMem(bool nm) {
112  no_agent = nm;
113  }
114 
115  /// Converts a string into a corresponding LogLevel value.
116  ///
117  /// For strings that do not correspond to any particular LogLevel enum value,
118  /// the method returns the LogLevel value `LEV_ERROR`. This method is
119  /// primarily intended for translating command line verbosity argument(s) into
120  /// appropriate report levels. LOG(level) statements
121  static LogLevel ToLogLevel(std::string text);
122 
123  /// Converts a LogLevel enum value into a corrsponding string.
124  ///
125  /// For a level argments that have no corresponding string value, the string
126  /// `BAD_LEVEL` is returned. This method is primarily intended for translating
127  /// LOG(level) statement levels into appropriate strings for output to stdout.
128  static std::string ToString(LogLevel level);
129 
130  protected:
131  std::ostringstream os;
132 
133  private:
134  Logger(const Logger&);
135 
136  Logger& operator =(const Logger&);
137 
138  /// Cuttoff for outputing LOG(level) statement content. Statments where
139  /// level==report_level will print.
140  static LogLevel report_level;
141 
142  /// Indicates whether or not agent/agent log entries should be printed
143  static bool no_agent;
144 
145  /// Indicates whether or not memory management log entries should be printed
146  static bool no_mem;
147 
148  /// Used to map LogLevel enum values into strings
149  static std::vector<std::string> level_to_string;
150 
151  /// Used to map strings into LogLevel enum values
152  static std::map<std::string, LogLevel> string_to_level;
153 
154  /// Used to populate the level_to_string and string_to_level vector/map before
155  /// cyclus code execution begins
156  static void Initialize();
157 
158  /// Used by the initialize method to populate the level_to_string and
159  /// string_to_level static variables.
160  static void AddLevel(LogLevel level, std::string text);
161 
162  /// The number of spaces indentation between different LogLevel enum values.
163  static int spc_per_lev_;
164 
165  /// The width (in characters) of the printed LOG(level) statements' prefixes.
166  /// This should be equal to or greater than the length of the longest LogLevel enum
167  /// value converted to a string (i.e. `LEV_WARN` is the longest with 11
168  /// characters).
169  static int field_width_;
170 };
171 
172 } // namespace cyclus
173 
174 #endif // CYCLUS_SRC_LOGGER_H_
Information helpful for simulation users and developers alike.
Definition: logger.h:54
debugging information
Definition: logger.h:60
static std::string ToString(LogLevel level)
Converts a LogLevel enum value into a corrsponding string.
Definition: logger.cc:68
debugging information
Definition: logger.h:61
static bool & NoMem()
Set whether or not agent/agent log entries should be printed.
Definition: logger.h:104
static LogLevel ToLogLevel(std::string text)
Converts a string into a corresponding LogLevel value.
Definition: logger.cc:59
static void SetNoMem(bool nm)
Sets whether or not agent/agent log entries should be printed.
Definition: logger.h:111
static LogLevel & ReportLevel()
Use to get/set the (global) log level report cutoff.
Definition: logger.h:81
debugging information - most verbose
Definition: logger.h:62
debugging information
Definition: logger.h:59
Information helpful for simulation users and developers alike - least verbose.
Definition: logger.h:53
static void SetNoAgent(bool na)
Sets whether or not agent/agent log entries should be printed.
Definition: logger.h:99
A logging tool providing finer grained control over standard output for debugging and other purposes...
Definition: logger.h:70
static void SetReportLevel(LogLevel level)
Sets the report level.
Definition: logger.h:87
Use to report questionable simulation state (use extremely sparingly)
Definition: logger.h:52
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
debugging information - least verbose
Definition: logger.h:58
static bool & NoAgent()
Set whether or not agent/agent log entries should be printed.
Definition: logger.h:92
Information helpful for simulation users and developers alike.
Definition: logger.h:55
std::ostringstream os
Definition: logger.h:131
std::ostringstream & Get(LogLevel level, std::string prefix)
Returns a string stream by reference that is flushed to stdout by the Logger class destructor...
Definition: logger.cc:18
Information helpful for simulation users and developers alike.
Definition: logger.h:56
Information helpful for simulation users and developers alike - most verbose.
Definition: logger.h:57
Use for errors that require agent code or input file modification (use extremely sparingly) ...
Definition: logger.h:51
LogLevel
categorical (verbosity) levels for log statements.
Definition: logger.h:50
virtual ~Logger()
Definition: logger.cc:35