Helios++
Helios software for LiDAR simulations
logging.hpp
Go to the documentation of this file.
1 #ifndef __LOGGING_HPP__
2 #define __LOGGING_HPP__
3 
4 //see https://gist.github.com/kevinkreiser/39f2e39273c625d96790
5 
6 #define LOGGING_LEVEL_ALL
7 /*
8 Test this with something like:
9 g++ -std=c++11 -x c++ -pthread -DLOGGING_LEVEL_ALL -DTEST_LOGGING logging.hpp -o logging_test
10 ./logging_test
11 */
12 
13 #include <string>
14 #include <stdexcept>
15 #include <iostream>
16 #include <fstream>
17 #include <sstream>
18 #include <mutex>
19 #include <unordered_map>
20 #include <memory>
21 #include <chrono>
22 #include <ctime>
23 #include <cstdlib>
24 #include <logging_common.hpp>
25 #include <logger.hpp>
26 #include <std_out_logger.hpp>
27 #include <file_logger.hpp>
28 #include <full_logger.hpp>
29 #include <logger_factory.hpp>
30 #include <logging_creation.hpp>
31 
38 namespace logging {
39 
40 //returns formated to: 'year/mo/dy hr:mn:sc.xxxxxx'
45 inline std::string timestamp() {
46  //get the time
47  std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
48  std::time_t tt = std::chrono::system_clock::to_time_t(tp);
49  std::tm gmt{};
50  #ifdef _OS_WINDOWS_
51  gmtime_s(&gmt, &tt);
52  #else
53  gmtime_r(&tt, &gmt);
54  #endif
55  std::chrono::duration<double> fractional_seconds =
56  (tp - std::chrono::system_clock::from_time_t(tt)) +
57  std::chrono::seconds(gmt.tm_sec);
58  //format the string
59  std::string buffer("year/mo/dy hr:mn:sc.xxxxxx");
60  sprintf(
61  &buffer.front(),
62  "%04d/%02d/%02d %02d:%02d:%09.6f",
63  gmt.tm_year + 1900,
64  gmt.tm_mon + 1,
65  gmt.tm_mday,
66  gmt.tm_hour,
67  gmt.tm_min,
68  fractional_seconds.count()
69  );
70  return buffer;
71  }
72 
73 
74 //statically get a factory
80  static logger_factory factory_singleton{};
81  return factory_singleton;
82 }
83 
84 //get at the singleton
91  const logging_config_t& config = { {"type", "std_out"}, {"color", ""} }
92 ){
93  static std::unique_ptr<logger> singleton(get_factory().produce(config));
94  return *singleton;
95 }
96 
97 //configure the singleton (once only)
102 inline void configure(const logging_config_t& config) {
103  get_logger(config);
104 }
105 
106 //statically log manually without the macros below
111 inline void log(const std::string& message, const log_level level) {
112  get_logger().log(message, level);
113 }
114 
115 //statically log manually without a level or maybe with a custom one
120 inline void log(const std::string& message) {
121  get_logger().log(message);
122 }
123 
124 // logging flags
125 extern bool LOGGING_SHOW_TRACE;
126 extern bool LOGGING_SHOW_DEBUG;
127 extern bool LOGGING_SHOW_INFO;
128 extern bool LOGGING_SHOW_WARN;
129 extern bool LOGGING_SHOW_ERR;
130 
131 // logging modes
136 inline void makeQuiet(){
137  LOGGING_SHOW_TRACE = false;
138  LOGGING_SHOW_DEBUG = false;
139  LOGGING_SHOW_INFO = false;
140  LOGGING_SHOW_WARN = false;
141  LOGGING_SHOW_ERR = true;
142 }
147 inline void makeSilent(){
148  LOGGING_SHOW_TRACE = false;
149  LOGGING_SHOW_DEBUG = false;
150  LOGGING_SHOW_INFO = false;
151  LOGGING_SHOW_WARN = false;
152  LOGGING_SHOW_ERR = false;
153 }
154 
159 inline void makeDefault(){
160  LOGGING_SHOW_TRACE = false;
161  LOGGING_SHOW_DEBUG = false;
162  LOGGING_SHOW_INFO = true;
163  LOGGING_SHOW_WARN = false;
164  LOGGING_SHOW_ERR = true;
165 }
166 
172 inline void makeVerbose(){
173  LOGGING_SHOW_TRACE = false;
174  LOGGING_SHOW_DEBUG = false;
175  LOGGING_SHOW_INFO = true;
176  LOGGING_SHOW_WARN = true;
177  LOGGING_SHOW_ERR = true;
178 }
183 inline void makeVerbose2(){
184  LOGGING_SHOW_TRACE = true;
185  LOGGING_SHOW_DEBUG = true;
186  LOGGING_SHOW_INFO = true;
187  LOGGING_SHOW_WARN = true;
188  LOGGING_SHOW_ERR = true;
189 }
190 
191 
192 //these standout when reading code
197 inline void TRACE(const std::string& message) {
198  if(!LOGGING_SHOW_TRACE) return;
199  get_logger().log(message, log_level::TRACE);
200 };
205 inline void DEBUG(const std::string& message) {
206  if(!LOGGING_SHOW_DEBUG) return;
207  get_logger().log(message, log_level::DEBUG);
208 };
213 inline void INFO(const std::string& message) {
214  if(!LOGGING_SHOW_INFO) return;
215  get_logger().log(message, log_level::INFO);
216 };
221 inline void WARN(const std::string& message) {
222  if(!LOGGING_SHOW_WARN) return;
223  get_logger().log(message, log_level::WARN);
224 };
229 inline void ERR(const std::string& message) {
230  if(!LOGGING_SHOW_ERR) return;
231  get_logger().log(message, log_level::ERR);
232 };
233 
234 
235 }
236 
237 #endif //__LOGGING_HPP__
virtual void log(const std::string &message, const log_level level)
Handle a log entry considerings its level.
Definition: logger.hpp:34
void log(const std::string &message, const log_level level)
Log function wrapper for singleton logger.
Definition: logging.hpp:111
Logger factory class can be used to build loggers.
Definition: logger_factory.hpp:9
Definition: logging.hpp:38
std::string timestamp()
Obtain current timestamp with format: "yy/mm/dd HH:MM:SS.xxxxxx".
Definition: logging.hpp:45
void DEBUG(const std::string &message)
Default debug messages logging function.
Definition: logging.hpp:205
void WARN(const std::string &message)
Default warning messages logging function.
Definition: logging.hpp:221
void ERR(const std::string &message)
Default error messages logging function.
Definition: logging.hpp:229
logger & get_logger(const logging_config_t &config={ {"type", "std_out"}, {"color", ""} })
Obtain a singleton logger through singleton factory.
Definition: logging.hpp:90
void INFO(const std::string &message)
Default info messages logging function.
Definition: logging.hpp:213
Class providing the base for any logger.
Definition: logger.hpp:8
void makeVerbose2()
Configure logging mode to make it verbose level 2. Verbose level 2 mode means all messages will be sh...
Definition: logging.hpp:183
logger_factory & get_factory()
Obtain a logger factory singleton instance.
Definition: logging.hpp:79
void makeDefault()
Configure logging mode to make it default. Default mode means only info and error messages will be sh...
Definition: logging.hpp:159
void configure(const logging_config_t &config)
Apply given configuration to current logger.
Definition: logging.hpp:102
void makeQuiet()
Configure logging mode to make it quiet. Quiet mode means only errors will be shown.
Definition: logging.hpp:136
void makeVerbose()
Configure logging mode to make it verbose. Verbose mode means only info, warning and error messages w...
Definition: logging.hpp:172
void makeSilent()
Configure logging mode to make it silent. Silent mode means nothing will be shown.
Definition: logging.hpp:147
void TRACE(const std::string &message)
Default trace messages logging function.
Definition: logging.hpp:197