/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/core/include/RAT/Log.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/core/include/RAT/Log.hh Source File
Ratpac-two
Log.hh
1 
76 #ifndef __RAT_Log__
77 #define __RAT_Log__
78 
79 #include <TMap.h>
80 #include <TObjString.h>
81 #include <TObject.h>
82 
83 #include <RAT/FatalError.hh>
84 #include <RAT/json.hh>
85 #include <sstream>
86 #include <string>
87 #include <utility>
88 #include <vector>
89 
90 #include "dprintf.hpp"
91 #include "fileio.hpp"
92 #include "multiio.hpp"
93 #include "string_utilities.hpp"
94 #include "stringio.hpp"
95 
96 namespace RAT {
97 
98 class Log {
99  public:
101  enum Level {
102  WARN = 0,
103  INFO = 1,
104  DETAIL = 2,
106  DEBUG = 3
108  };
109 
116  static bool Init(std::string _filename, Level display = INFO, Level log = DETAIL);
117 
119  static int GetDisplayLevel() { return display_level; };
120 
122  static void SetDisplayLevel(Level level);
123 
125  static int GetLogLevel() { return log_level; };
126 
128  static void SetLogLevel(Level level);
129 
134  [[noreturn]] static void Die(std::string message, int return_code = 1);
135 
138  static void Assert(bool condition, std::string message, int return_code = 1);
139 
141  static const std::string GetLogBuffer();
142 
144  static void AddMacro(const std::string &contents) { macro += contents; };
146  static const std::string &GetMacro() { return macro; };
147 
149  static void AddObject(const std::string &name, TObject *obj) {
150  objects.push_back(std::pair<std::string, TObject *>(name, obj));
151  };
152 
155  static std::vector<std::pair<std::string, TObject *>> GetObjects() { return objects; };
156 
161  static void SetDBTraceState(bool state = true) { enable_dbtrace = state; };
162 
164  static bool GetDBTraceState() { return enable_dbtrace; };
165 
167  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
168  int value);
170  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
171  double value);
173  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
174  const std::string &value);
175 
177  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
178  const std::vector<int> &value);
180  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
181  const std::vector<double> &value);
183  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
184  const std::vector<std::string> &value);
185 
187  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
188  const json::Value &value);
189 
190  static TMap *GetDBTraceMap() { return dbtrace; };
191 
192  protected:
194  Log();
195 
197  inline static void AddDBEntry(const std::string &key, const std::string &value);
198 
205  static void SetupIO();
206 
209  static void ClearOMText(omtext *out);
210 
212  static omtext *outstreams[4];
213 
214  static std::string filename;
215  static oftext logfile;
216  static int display_level;
217  static int log_level;
219  static std::string macro;
221  static bool enable_dbtrace;
222  static TMap *dbtrace;
224  static std::vector<std::pair<std::string, TObject *>> objects;
225 };
226 
227 extern omtext warn;
228 extern omtext info;
229 extern omtext detail;
230 extern omtext debug;
231 
233 
234 void Log::AddDBEntry(const std::string &key, const std::string &value) {
235  if (enable_dbtrace && !dbtrace->FindObject(key.c_str()))
236  dbtrace->Add(new TObjString(key.c_str()), new TObjString(value.c_str()));
237 }
238 
239 // The parameter is type double so this can be used as a seed for
240 // to_ratdb_double_string()
241 inline std::string to_ratdb_float_string(double value) {
242  if (value == 0.0)
243  return std::string("0.0");
244  else
245  return ::to_string(value);
246 }
247 
248 inline std::string to_ratdb_double_string(double value) {
249  std::string str = to_ratdb_float_string(value);
250  size_t pos = str.find("d");
251  if (pos == std::string::npos) str += "d";
252  return str;
253 }
254 
255 inline std::string escape_ratdb_string(const std::string &value) {
256  if (value.find("\"") != std::string::npos) Log::Die("RATDB Trace Eror: value string with quotes inside!");
257  return "\"" + value + "\"";
258 }
259 
260 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field, int value) {
261  AddDBEntry(table + "[" + index + "]." + field, ::to_string(value));
262 }
263 
264 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field, double value) {
265  AddDBEntry(table + "[" + index + "]." + field, to_ratdb_double_string(value));
266 }
267 
268 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
269  const std::string &value) {
270  AddDBEntry(table + "[" + index + "]." + field, escape_ratdb_string(value));
271 }
272 
273 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
274  const std::vector<int> &value) {
275  std::string key = table + "[" + index + "]." + field;
276  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
277 
278  std::string str_value("[ ");
279  for (unsigned i = 0; i < value.size(); i++) {
280  str_value += ::to_string(value[i]) + ", ";
281  }
282  str_value += "]";
283 
284  AddDBEntry(key, str_value);
285 }
286 
287 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
288  const std::vector<double> &value) {
289  std::string key = table + "[" + index + "]." + field;
290  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
291 
292  std::string str_value("[ ");
293  for (unsigned i = 0; i < value.size(); i++) {
294  str_value += to_ratdb_double_string(value[i]) + ", ";
295  }
296  str_value += "]";
297 
298  AddDBEntry(key, str_value);
299 }
300 
301 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
302  const std::vector<std::string> &value) {
303  std::string key = table + "[" + index + "]." + field;
304  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
305 
306  std::string str_value("[ ");
307  for (unsigned i = 0; i < value.size(); i++) {
308  str_value += escape_ratdb_string(value[i]) + ", ";
309  }
310  str_value += "]";
311 
312  AddDBEntry(key, str_value);
313 }
314 
315 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
316  const json::Value &value) {
317  std::string key = table + "[" + index + "]." + field;
318  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
319 
320  std::stringstream ss;
321  json::Writer writer(ss);
322  writer.putValue(value);
323 
324  AddDBEntry(key, ss.str());
325 }
326 
327 } // namespace RAT
328 
329 #endif
Definition: Log.hh:98
static int log_level
Definition: Log.hh:217
static std::vector< std::pair< std::string, TObject * > > objects
Definition: Log.hh:224
static void AddDBEntry(const std::string &key, const std::string &value)
Inline functions.
Definition: Log.hh:234
static void SetDBTraceState(bool state=true)
Definition: Log.hh:161
static void AddObject(const std::string &name, TObject *obj)
Definition: Log.hh:149
static const std::string GetLogBuffer()
Definition: Log.cc:119
static bool GetDBTraceState()
Definition: Log.hh:164
static void Die(std::string message, int return_code=1)
Definition: Log.cc:90
static const std::string & GetMacro()
Definition: Log.hh:146
static void SetLogLevel(Level level)
Definition: Log.cc:85
static omtext * outstreams[4]
Definition: Log.hh:212
static std::vector< std::pair< std::string, TObject * > > GetObjects()
Definition: Log.hh:155
static int GetDisplayLevel()
Definition: Log.hh:119
static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field, int value)
Definition: Log.hh:260
Level
Definition: Log.hh:101
@ INFO
Definition: Log.hh:103
@ DETAIL
Definition: Log.hh:104
@ DEBUG
Definition: Log.hh:106
@ WARN
Definition: Log.hh:102
static TMap * dbtrace
Definition: Log.hh:222
static int GetLogLevel()
Definition: Log.hh:125
static std::string filename
Definition: Log.hh:214
static oftext logfile
Definition: Log.hh:215
static void AddMacro(const std::string &contents)
Definition: Log.hh:144
static std::string macro
Definition: Log.hh:219
static void Assert(bool condition, std::string message, int return_code=1)
Definition: Log.cc:95
static void SetupIO()
Definition: Log.cc:99
static bool enable_dbtrace
Definition: Log.hh:221
static void SetDisplayLevel(Level level)
Definition: Log.cc:80
static int display_level
Definition: Log.hh:216
static void ClearOMText(omtext *out)
Definition: Log.cc:114
Log()
Definition: Log.cc:59
static bool Init(std::string _filename, Level display=INFO, Level log=DETAIL)
Definition: Log.cc:63
Definition: json.hh:59
Definition: json.hh:428
Definition: fileio.hpp:22
Definition: multiio.hpp:18
Definition: CCCrossSecMessenger.hh:29