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  /// Set whether or not agent/agent log entries should be printed
86  static bool& NoAgent() {
87  return no_agent;
88  }
89 
90  /// Set whether or not agent/agent log entries should be printed
91  static bool& NoMem() {
92  return no_mem;
93  }
94 
95  /// Converts a string into a corresponding LogLevel value.
96  ///
97  /// For strings that do not correspond to any particular LogLevel enum value,
98  /// the method returns the LogLevel value `LEV_ERROR`. This method is
99  /// primarily intended for translating command line verbosity argument(s) into
100  /// appropriate report levels. LOG(level) statements
101  static LogLevel ToLogLevel(std::string text);
102 
103  /// Converts a LogLevel enum value into a corrsponding string.
104  ///
105  /// For a level argments that have no corresponding string value, the string
106  /// `BAD_LEVEL` is returned. This method is primarily intended for translating
107  /// LOG(level) statement levels into appropriate strings for output to stdout.
108  static std::string ToString(LogLevel level);
109 
110  protected:
111  std::ostringstream os;
112 
113  private:
114  Logger(const Logger&);
115 
116  Logger& operator =(const Logger&);
117 
118  /// Cuttoff for outputing LOG(level) statement content. Statments where
119  /// level==report_level will print.
120  static LogLevel report_level;
121 
122  /// Indicates whether or not agent/agent log entries should be printed
123  static bool no_agent;
124 
125  /// Indicates whether or not memory management log entries should be printed
126  static bool no_mem;
127 
128  /// Used to map LogLevel enum values into strings
129  static std::vector<std::string> level_to_string;
130 
131  /// Used to map strings into LogLevel enum values
132  static std::map<std::string, LogLevel> string_to_level;
133 
134  /// Used to populate the level_to_string and string_to_level vector/map before
135  /// cyclus code execution begins
136  static void Initialize();
137 
138  /// Used by the initialize method to populate the level_to_string and
139  /// string_to_level static variables.
140  static void AddLevel(LogLevel level, std::string text);
141 
142  /// The number of spaces indentation between different LogLevel enum values.
143  static int spc_per_lev_;
144 
145  /// The width (in characters) of the printed LOG(level) statements' prefixes.
146  /// This should be equal to or greater than the length of the longest LogLevel enum
147  /// value converted to a string (i.e. `LEV_WARN` is the longest with 11
148  /// characters).
149  static int field_width_;
150 };
151 
152 } // namespace cyclus
153 
154 #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:91
static LogLevel ToLogLevel(std::string text)
Converts a string into a corresponding LogLevel value.
Definition: logger.cc:59
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
A logging tool providing finer grained control over standard output for debugging and other purposes...
Definition: logger.h:70
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:86
Information helpful for simulation users and developers alike.
Definition: logger.h:55
std::ostringstream os
Definition: logger.h:111
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