Electroneum
logging.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <ctime>
7 #include <iostream>
8 #include <sstream>
9 
10 #include "settings.h"
11 
12 namespace crow
13 {
14  enum class LogLevel
15  {
16 #ifndef ERROR
17  DEBUG = 0,
18  INFO,
19  WARNING,
20  ERROR,
21  CRITICAL,
22 #endif
23 
24  Debug = 0,
25  Info,
26  Warning,
27  Error,
28  Critical,
29  };
30 
31  class ILogHandler {
32  public:
33  virtual void log(std::string message, LogLevel level) = 0;
34  };
35 
36  class CerrLogHandler : public ILogHandler {
37  public:
38  void log(std::string message, LogLevel /*level*/) override {
39  std::cerr << message;
40  }
41  };
42 
43  class logger {
44 
45  private:
46  //
47  static std::string timestamp()
48  {
49  char date[32];
50  time_t t = time(0);
51 
52  tm my_tm;
53 
54 #ifdef _MSC_VER
55  gmtime_s(&my_tm, &t);
56 #else
57  gmtime_r(&t, &my_tm);
58 #endif
59 
60  size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm);
61  return std::string(date, date+sz);
62  }
63 
64  public:
65 
66 
67  logger(std::string prefix, LogLevel level) : level_(level) {
68  #ifdef CROW_ENABLE_LOGGING
69  stringstream_ << "(" << timestamp() << ") [" << prefix << "] ";
70  #endif
71 
72  }
73  ~logger() {
74  #ifdef CROW_ENABLE_LOGGING
75  if(level_ >= get_current_log_level()) {
76  stringstream_ << std::endl;
78  }
79  #endif
80  }
81 
82  //
83  template <typename T>
84  logger& operator<<(T const &value) {
85 
86  #ifdef CROW_ENABLE_LOGGING
87  if(level_ >= get_current_log_level()) {
88  stringstream_ << value;
89  }
90  #endif
91  return *this;
92  }
93 
94  //
95  static void setLogLevel(LogLevel level) {
96  get_log_level_ref() = level;
97  }
98 
99  static void setHandler(ILogHandler* handler) {
100  get_handler_ref() = handler;
101  }
102 
104  return get_log_level_ref();
105  }
106 
107  private:
108  //
110  {
111  static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL;
112  return current_level;
113  }
115  {
116  static CerrLogHandler default_handler;
117  static ILogHandler* current_handler = &default_handler;
118  return current_handler;
119  }
120 
121  //
122  std::ostringstream stringstream_;
124  };
125 }
126 
127 #define CROW_LOG_CRITICAL \
128  if (crow::logger::get_current_log_level() <= crow::LogLevel::Critical) \
129  crow::logger("CRITICAL", crow::LogLevel::Critical)
130 #define CROW_LOG_ERROR \
131  if (crow::logger::get_current_log_level() <= crow::LogLevel::Error) \
132  crow::logger("ERROR ", crow::LogLevel::Error)
133 #define CROW_LOG_WARNING \
134  if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning) \
135  crow::logger("WARNING ", crow::LogLevel::Warning)
136 #define CROW_LOG_INFO \
137  if (crow::logger::get_current_log_level() <= crow::LogLevel::Info) \
138  crow::logger("INFO ", crow::LogLevel::Info)
139 #define CROW_LOG_DEBUG \
140  if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug) \
141  crow::logger("DEBUG ", crow::LogLevel::Debug)
142 
static LogLevel get_current_log_level()
Definition: logging.h:103
LogLevel level_
Definition: logging.h:123
const uint32_t T[512]
Definition: groestl_tables.h:34
virtual void log(std::string message, LogLevel level)=0
Definition: logging.h:43
~logger()
Definition: logging.h:73
void log(std::string message, LogLevel) override
Definition: logging.h:38
static void setHandler(ILogHandler *handler)
Definition: logging.h:99
static void setLogLevel(LogLevel level)
Definition: logging.h:95
logger & operator<<(T const &value)
Definition: logging.h:84
#define CROW_LOG_LEVEL
Definition: settings.h:24
logger(std::string prefix, LogLevel level)
Definition: logging.h:67
static ILogHandler *& get_handler_ref()
Definition: logging.h:114
Definition: logging.h:36
time_t time
Definition: blockchain.cpp:89
static LogLevel & get_log_level_ref()
Definition: logging.h:109
static std::string timestamp()
Definition: logging.h:47
std::ostringstream stringstream_
Definition: logging.h:122
Definition: logging.h:31
LogLevel
Definition: logging.h:14
Definition: ci_map.h:7