CYCLUS
Loading...
Searching...
No Matches
logger.cc
Go to the documentation of this file.
1#include "logger.h"
2
3#include <cstdio>
4
5namespace cyclus {
6
7// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8std::vector<std::string> Logger::level_to_string;
9std::map<std::string, LogLevel> Logger::string_to_level;
10LogLevel Logger::report_level = (Logger::Initialize(), LEV_ERROR);
11bool Logger::no_agent = false;
12bool Logger::no_mem = false;
13
14int Logger::spc_per_lev_ = 2;
15int Logger::field_width_ = 6;
16
17// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18std::ostringstream& Logger::Get(LogLevel level, std::string prefix) {
19 int ind_level = level - LEV_INFO1;
20 if (ind_level < 0) {
21 ind_level = 0;
22 }
23
24 int prefix_len = 6;
25 prefix = prefix.substr(0, prefix_len);
26 if (prefix.length() < prefix_len) {
27 prefix = prefix + std::string(prefix_len - prefix.length(), ' ');
28 }
29 os << ToString(level) << "(" << prefix << "):";
30 os << std::string(ind_level * spc_per_lev_, ' ');
31 return os;
32}
33
34// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36 os << std::endl;
37 // fprintf used to maintain thread safety
38 fprintf(stdout, "%s", os.str().c_str());
40}
41
42// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43void Logger::Initialize() {
44 Logger::AddLevel(LEV_ERROR, "LEV_ERROR");
45 Logger::AddLevel(LEV_WARN, "LEV_WARN");
46 Logger::AddLevel(LEV_INFO1, "LEV_INFO1");
47 Logger::AddLevel(LEV_INFO2, "LEV_INFO2");
48 Logger::AddLevel(LEV_INFO3, "LEV_INFO3");
49 Logger::AddLevel(LEV_INFO4, "LEV_INFO4");
50 Logger::AddLevel(LEV_INFO5, "LEV_INFO5");
51 Logger::AddLevel(LEV_DEBUG1, "LEV_DEBUG1");
52 Logger::AddLevel(LEV_DEBUG2, "LEV_DEBUG2");
53 Logger::AddLevel(LEV_DEBUG3, "LEV_DEBUG3");
54 Logger::AddLevel(LEV_DEBUG4, "LEV_DEBUG4");
55 Logger::AddLevel(LEV_DEBUG5, "LEV_DEBUG5");
56}
57
58// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60 if (string_to_level.count(text) > 0) {
61 return string_to_level[text];
62 } else {
63 return LEV_ERROR;
64 }
65}
66
67// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68std::string Logger::ToString(LogLevel level) {
69 std::string text;
70 try {
71 text = level_to_string.at((int)level);
72 } catch (...) {
73 text = "BAD_LEVEL";
74 }
75 return text;
76}
77
78// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79void Logger::AddLevel(LogLevel level, std::string text) {
80 // order of the following statements matters
81 Logger::string_to_level[text] = level;
82 text = text.substr(4);
83 text = std::string(field_width_ - text.size(), ' ') + text;
84 Logger::level_to_string.push_back(text);
85}
86
87} // namespace cyclus
static LogLevel ToLogLevel(std::string text)
Converts a string into a corresponding LogLevel value.
Definition logger.cc:59
std::ostringstream os
Definition logger.h:131
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 std::string ToString(LogLevel level)
Converts a LogLevel enum value into a corrsponding string.
Definition logger.cc:68
Code providing rudimentary logging capability for the Cyclus core.
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:50
@ LEV_DEBUG3
debugging information
Definition logger.h:60
@ LEV_DEBUG5
debugging information - most verbose
Definition logger.h:62
@ LEV_DEBUG4
debugging information
Definition logger.h:61
@ LEV_INFO3
Information helpful for simulation users and developers alike.
Definition logger.h:55
@ LEV_DEBUG1
debugging information - least verbose
Definition logger.h:58
@ LEV_INFO4
Information helpful for simulation users and developers alike.
Definition logger.h:56
@ LEV_WARN
Use to report questionable simulation state (use extremely sparingly)
Definition logger.h:52
@ LEV_INFO2
Information helpful for simulation users and developers alike.
Definition logger.h:54
@ LEV_INFO5
Information helpful for simulation users and developers alike - most verbose.
Definition logger.h:57
@ LEV_INFO1
Information helpful for simulation users and developers alike - least verbose.
Definition logger.h:53
@ LEV_DEBUG2
debugging information
Definition logger.h:59
@ LEV_ERROR
Use for errors that require agent code or input file modification (use extremely sparingly)
Definition logger.h:51
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters