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