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_TIME;
129 extern bool LOGGING_SHOW_WARN;
130 extern bool LOGGING_SHOW_ERR;
131 
132 // logging modes
137 inline void makeQuiet(){
138  LOGGING_SHOW_TRACE = false;
139  LOGGING_SHOW_DEBUG = false;
140  LOGGING_SHOW_INFO = false;
141  LOGGING_SHOW_TIME = false;
142  LOGGING_SHOW_WARN = false;
143  LOGGING_SHOW_ERR = true;
144 }
149 inline void makeSilent(){
150  LOGGING_SHOW_TRACE = false;
151  LOGGING_SHOW_DEBUG = false;
152  LOGGING_SHOW_INFO = false;
153  LOGGING_SHOW_TIME = false;
154  LOGGING_SHOW_WARN = false;
155  LOGGING_SHOW_ERR = false;
156 }
157 
162 inline void makeTime(){
163  LOGGING_SHOW_TRACE = false;
164  LOGGING_SHOW_DEBUG = false;
165  LOGGING_SHOW_INFO = false;
166  LOGGING_SHOW_TIME = true;
167  LOGGING_SHOW_WARN = false;
168  LOGGING_SHOW_ERR = true;
169 }
170 
175 inline void makeDefault(){
176  LOGGING_SHOW_TRACE = false;
177  LOGGING_SHOW_DEBUG = false;
178  LOGGING_SHOW_INFO = true;
179  LOGGING_SHOW_TIME = true;
180  LOGGING_SHOW_WARN = false;
181  LOGGING_SHOW_ERR = true;
182 }
183 
189 inline void makeVerbose(){
190  LOGGING_SHOW_TRACE = false;
191  LOGGING_SHOW_DEBUG = false;
192  LOGGING_SHOW_INFO = true;
193  LOGGING_SHOW_TIME = true;
194  LOGGING_SHOW_WARN = true;
195  LOGGING_SHOW_ERR = true;
196 }
201 inline void makeVerbose2(){
202  LOGGING_SHOW_TRACE = true;
203  LOGGING_SHOW_DEBUG = true;
204  LOGGING_SHOW_INFO = true;
205  LOGGING_SHOW_TIME = true;
206  LOGGING_SHOW_WARN = true;
207  LOGGING_SHOW_ERR = true;
208 }
209 
210 
211 //these standout when reading code
216 inline void TRACE(const std::string& message) {
217  if(!LOGGING_SHOW_TRACE) return;
218  get_logger().log(message, log_level::TRACE);
219 };
224 inline void DEBUG(const std::string& message) {
225  if(!LOGGING_SHOW_DEBUG) return;
226  get_logger().log(message, log_level::DEBUG);
227 };
232 inline void INFO(const std::string& message) {
233  if(!LOGGING_SHOW_INFO) return;
234  get_logger().log(message, log_level::INFO);
235 };
240 inline void TIME(const std::string& message){
241  if(!LOGGING_SHOW_TIME) return;
242  get_logger().log(message, log_level::TIME);
243 }
248 inline void WARN(const std::string& message) {
249  if(!LOGGING_SHOW_WARN) return;
250  get_logger().log(message, log_level::WARN);
251 };
256 inline void ERR(const std::string& message) {
257  if(!LOGGING_SHOW_ERR) return;
258  get_logger().log(message, log_level::ERR);
259 };
260 
261 
262 }
263 
264 #endif //__LOGGING_HPP__
Logger factory class can be used to build loggers.
Definition: logger_factory.hpp:9
Class providing the base for any logger.
Definition: logger.hpp:8
virtual void log(const std::string &message, const log_level level)
Handle a log entry considerings its level.
Definition: logger.hpp:34
logger_factory & get_factory()
Obtain a logger factory singleton instance.
Definition: logging.hpp:79
void INFO(const std::string &message)
Default info messages logging function.
Definition: logging.hpp:232
void makeVerbose()
Configure logging mode to make it verbose. Verbose mode means only info, warning and error messages w...
Definition: logging.hpp:189
void makeVerbose2()
Configure logging mode to make it verbose level 2. Verbose level 2 mode means all messages will be sh...
Definition: logging.hpp:201
std::string timestamp()
Obtain current timestamp with format: "yy/mm/dd HH:MM:SS.xxxxxx".
Definition: logging.hpp:45
void ERR(const std::string &message)
Default error messages logging function.
Definition: logging.hpp:256
void makeSilent()
Configure logging mode to make it silent. Silent mode means nothing will be shown.
Definition: logging.hpp:149
logger & get_logger(const logging_config_t &config={ {"type", "std_out"}, {"color", ""} })
Obtain a singleton logger through singleton factory.
Definition: logging.hpp:90
void TIME(const std::string &message)
Default time messages loggin function.
Definition: logging.hpp:240
void makeQuiet()
Configure logging mode to make it quiet. Quiet mode means only errors will be shown.
Definition: logging.hpp:137
void log(const std::string &message, const log_level level)
Log function wrapper for singleton logger.
Definition: logging.hpp:111
void TRACE(const std::string &message)
Default trace messages logging function.
Definition: logging.hpp:216
void configure(const logging_config_t &config)
Apply given configuration to current logger.
Definition: logging.hpp:102
void WARN(const std::string &message)
Default warning messages logging function.
Definition: logging.hpp:248
void makeDefault()
Configure logging mode to make it default. Default mode means only info and error messages will be sh...
Definition: logging.hpp:175
void DEBUG(const std::string &message)
Default debug messages logging function.
Definition: logging.hpp:224
void makeTime()
Configure logging mode to make it time. Time mode means only error and time messages will be shown.
Definition: logging.hpp:162