CYCLUS
logger.cc
Go to the documentation of this file.
1 #include "logger.h"
2 
3 #include <cstdio>
4 
5 namespace cyclus {
6 
7 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8 std::vector<std::string> Logger::level_to_string;
9 std::map<std::string, LogLevel> Logger::string_to_level;
10 LogLevel Logger::report_level = (Logger::Initialize(), LEV_ERROR);
11 bool Logger::no_agent = false;
12 bool Logger::no_mem = false;
13 
14 int Logger::spc_per_lev_ = 2;
15 int Logger::field_width_ = 6;
16 
17 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18 std::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());
39  fflush(stdout);
40 }
41 
42 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43 void 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 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79 void 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
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 LogLevel ToLogLevel(std::string text)
Converts a string into a corresponding LogLevel value.
Definition: logger.cc:59
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
Use to report questionable simulation state (use extremely sparingly)
Definition: logger.h:52
Code providing rudimentary logging capability for the Cyclus core.
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
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