/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/json.hh>
84 #include <sstream>
85 #include <string>
86 #include <utility>
87 #include <vector>
88 
89 #include "dprintf.hpp"
90 #include "fileio.hpp"
91 #include "multiio.hpp"
92 #include "string_utilities.hpp"
93 #include "stringio.hpp"
94 
95 namespace RAT {
96 
97 class Log {
98  public:
100  enum Level {
101  WARN = 0,
102  INFO = 1,
103  DETAIL = 2,
105  DEBUG = 3
107  };
108 
115  static bool Init(std::string _filename, Level display = INFO, Level log = DETAIL);
116 
118  static int GetDisplayLevel() { return display_level; };
119 
121  static void SetDisplayLevel(Level level);
122 
124  static int GetLogLevel() { return log_level; };
125 
127  static void SetLogLevel(Level level);
128 
131  [[noreturn]] static void Die(std::string message, int return_code = 1);
132 
136  static void Assert(bool condition, std::string message, int return_code = 1);
137 
139  static const std::string GetLogBuffer();
140 
142  static void AddMacro(const std::string &contents) { macro += contents; };
144  static const std::string &GetMacro() { return macro; };
145 
147  static void AddObject(const std::string &name, TObject *obj) {
148  objects.push_back(std::pair<std::string, TObject *>(name, obj));
149  };
150 
153  static std::vector<std::pair<std::string, TObject *>> GetObjects() { return objects; };
154 
159  static void SetDBTraceState(bool state = true) { enable_dbtrace = state; };
160 
162  static bool GetDBTraceState() { return enable_dbtrace; };
163 
165  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
166  int value);
168  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
169  double value);
171  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
172  const std::string &value);
173 
175  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
176  const std::vector<int> &value);
178  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
179  const std::vector<double> &value);
181  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
182  const std::vector<std::string> &value);
183 
185  inline static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
186  const json::Value &value);
187 
188  static TMap *GetDBTraceMap() { return dbtrace; };
189 
190  protected:
192  Log();
193 
195  inline static void AddDBEntry(const std::string &key, const std::string &value);
196 
203  static void SetupIO();
204 
207  static void ClearOMText(omtext *out);
208 
210  static omtext *outstreams[4];
211 
212  static std::string filename;
213  static oftext logfile;
214  static int display_level;
215  static int log_level;
217  static std::string macro;
219  static bool enable_dbtrace;
220  static TMap *dbtrace;
222  static std::vector<std::pair<std::string, TObject *>> objects;
223 };
224 
225 extern omtext warn;
226 extern omtext info;
227 extern omtext detail;
228 extern omtext debug;
229 
231 
232 void Log::AddDBEntry(const std::string &key, const std::string &value) {
233  if (enable_dbtrace && !dbtrace->FindObject(key.c_str()))
234  dbtrace->Add(new TObjString(key.c_str()), new TObjString(value.c_str()));
235 }
236 
237 // The parameter is type double so this can be used as a seed for
238 // to_ratdb_double_string()
239 inline std::string to_ratdb_float_string(double value) {
240  if (value == 0.0)
241  return std::string("0.0");
242  else
243  return ::to_string(value);
244 }
245 
246 inline std::string to_ratdb_double_string(double value) {
247  std::string str = to_ratdb_float_string(value);
248  size_t pos = str.find("d");
249  if (pos == std::string::npos) str += "d";
250  return str;
251 }
252 
253 inline std::string escape_ratdb_string(const std::string &value) {
254  if (value.find("\"") != std::string::npos) Log::Die("RATDB Trace Eror: value string with quotes inside!");
255  return "\"" + value + "\"";
256 }
257 
258 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field, int value) {
259  AddDBEntry(table + "[" + index + "]." + field, ::to_string(value));
260 }
261 
262 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field, double value) {
263  AddDBEntry(table + "[" + index + "]." + field, to_ratdb_double_string(value));
264 }
265 
266 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
267  const std::string &value) {
268  AddDBEntry(table + "[" + index + "]." + field, escape_ratdb_string(value));
269 }
270 
271 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
272  const std::vector<int> &value) {
273  std::string key = table + "[" + index + "]." + field;
274  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
275 
276  std::string str_value("[ ");
277  for (unsigned i = 0; i < value.size(); i++) {
278  str_value += ::to_string(value[i]) + ", ";
279  }
280  str_value += "]";
281 
282  AddDBEntry(key, str_value);
283 }
284 
285 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
286  const std::vector<double> &value) {
287  std::string key = table + "[" + index + "]." + field;
288  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
289 
290  std::string str_value("[ ");
291  for (unsigned i = 0; i < value.size(); i++) {
292  str_value += to_ratdb_double_string(value[i]) + ", ";
293  }
294  str_value += "]";
295 
296  AddDBEntry(key, str_value);
297 }
298 
299 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
300  const std::vector<std::string> &value) {
301  std::string key = table + "[" + index + "]." + field;
302  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
303 
304  std::string str_value("[ ");
305  for (unsigned i = 0; i < value.size(); i++) {
306  str_value += escape_ratdb_string(value[i]) + ", ";
307  }
308  str_value += "]";
309 
310  AddDBEntry(key, str_value);
311 }
312 
313 void Log::TraceDBAccess(const std::string &table, const std::string &index, const std::string &field,
314  const json::Value &value) {
315  std::string key = table + "[" + index + "]." + field;
316  if (!enable_dbtrace || dbtrace->FindObject(key.c_str())) return;
317 
318  std::stringstream ss;
319  json::Writer writer(ss);
320  writer.putValue(value);
321 
322  AddDBEntry(key, ss.str());
323 }
324 
325 } // namespace RAT
326 
327 #endif
Definition: Log.hh:97
static int log_level
Definition: Log.hh:215
static std::vector< std::pair< std::string, TObject * > > objects
Definition: Log.hh:222
static void AddDBEntry(const std::string &key, const std::string &value)
Inline functions.
Definition: Log.hh:232
static void SetDBTraceState(bool state=true)
Definition: Log.hh:159
static void AddObject(const std::string &name, TObject *obj)
Definition: Log.hh:147
static const std::string GetLogBuffer()
Definition: Log.cc:122
static bool GetDBTraceState()
Definition: Log.hh:162
static void Die(std::string message, int return_code=1)
Definition: Log.cc:90
static const std::string & GetMacro()
Definition: Log.hh:144
static void SetLogLevel(Level level)
Definition: Log.cc:85
static omtext * outstreams[4]
Definition: Log.hh:210
static std::vector< std::pair< std::string, TObject * > > GetObjects()
Definition: Log.hh:153
static int GetDisplayLevel()
Definition: Log.hh:118
static void TraceDBAccess(const std::string &table, const std::string &index, const std::string &field, int value)
Definition: Log.hh:258
Level
Definition: Log.hh:100
@ INFO
Definition: Log.hh:102
@ DETAIL
Definition: Log.hh:103
@ DEBUG
Definition: Log.hh:105
@ WARN
Definition: Log.hh:101
static TMap * dbtrace
Definition: Log.hh:220
static int GetLogLevel()
Definition: Log.hh:124
static std::string filename
Definition: Log.hh:212
static oftext logfile
Definition: Log.hh:213
static void AddMacro(const std::string &contents)
Definition: Log.hh:142
static std::string macro
Definition: Log.hh:217
static void Assert(bool condition, std::string message, int return_code=1)
Definition: Log.cc:95
static void SetupIO()
Definition: Log.cc:102
static bool enable_dbtrace
Definition: Log.hh:219
static void SetDisplayLevel(Level level)
Definition: Log.cc:80
static int display_level
Definition: Log.hh:214
static void ClearOMText(omtext *out)
Definition: Log.cc:117
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