gaps-online-software 0.10
online software for the TOF system for the GAPS experiment
Loading...
Searching...
No Matches
logging.hpp
1#ifndef LOGGING_H_INCLUDED
2#define LOGGING_H_INCLUDED
3
4/*
5 * logging system
6 *
7 * it defines 6 macros which can be used for logging.
8 *
9 * In increasing severity:
10 *
11 * log_trace - most verbose log level for nitty-gritty details (e.g. inside a loop)
12 * log_debug - detailed information which should help debugging a program
13 * log_info - short, compressed information to tell the user about a programs status
14 * log_warn - alert the user about pitfalls
15 * log_error - an error occured. Most likely critical, however do NOT abort
16 * log_fatal - abort the program with a message giving some information about
17 * what went wrong
18 *
19 * log_trace, log_debug, log_info, log_warn will log to std::cout, log_error and log_fatal will log to std::cerr
20 * log_fatal also will call std::exit(EXIT_FAILURE)
21 *
22 * The definition of the macros is connected to the definition of the NDEBUG
23 * macro - if it is defined, all log_debug and log_trace statements will be compiled out.
24 * The NDEBUG macros is set in case a "Release" build is done with the CMake
25 * build system. For that case, have a look at the CMAKE_BUILD_TYPE variable
26 * which can be set to "Release" or "Debug".
27 *
28 * Use the macros as in the following example:
29 * .. doSomething();
30 * .. log_warn("There can be " << howMany << " dragons!");
31 * .. doSomethingElse();
32 *
33 * FIXME: logging to a file is not yet implemented
34 */
35
36
37#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE // Don't forget define SPDLOG_ACTIVE_LEVEL macro.
38
39#include <iostream>
40#include <sstream>
41#include <spdlog/spdlog.h>
42#include <spdlog/sinks/stdout_color_sinks.h>
43
44
46template<typename... Args>
47std::string concatenate_args(Args&&... args) {
48 std::ostringstream oss;
49 (oss << ... << args());
50 return oss.str();
51}
52
53
54// log_trace
55#ifndef NDEBUG
56
57#define log_trace(...) SPDLOG_TRACE(concatenate_args([&](){ std::ostringstream oss; oss << __VA_ARGS__; return oss.str(); }))
58
59#else //NDEBUG case, ompile it out
60#define log_trace(...) ((void) 0);
61#endif
62
63// log_debug
64#ifndef NDEBUG
65#define log_debug(...) SPDLOG_DEBUG(concatenate_args([&](){ std::ostringstream oss; oss << __VA_ARGS__; return oss.str(); }))
66
67#else //NDEBUG case, ompile it out
68#define log_debug(...) ((void) 0);
69#endif
70
71#define log_info(...) SPDLOG_INFO(concatenate_args([&](){ std::ostringstream oss; oss << __VA_ARGS__; return oss.str(); }))
72
73#define log_warn(...) SPDLOG_WARN(concatenate_args([&](){ std::ostringstream oss; oss << __VA_ARGS__; return oss.str(); }))
74
75
76// log_error
77#define log_error(...) SPDLOG_ERROR(concatenate_args([&](){ std::ostringstream oss; oss << __VA_ARGS__; return oss.str(); }))
78
79// log_fatal
80#define log_fatal(...) do { SPDLOG_ERROR(concatenate_args([&](){ std::ostringstream oss; oss << __VA_ARGS__; return oss.str(); })); \
81throw Gaps::FatalException(); \
82} while(0)
83
84namespace Gaps {
85 typedef spdlog::level::level_enum LOGLEVEL;
86
87 std::string severity_to_str (const LOGLEVEL& severity);
88
96 class FatalException : public std::exception
97 {
98 virtual const char* what() const throw()
99 {
100 return "Abort program due to a log_fatal(..) statement in the gaps code!";
101 }
102 };
103
109 void set_loglevel(LOGLEVEL severity);
110}
111
112
113
114#endif //include guard
Exception for log_fatal(..) macro.
Definition logging.hpp:97
‍**
Definition caraspace.hpp:8
void set_loglevel(LOGLEVEL severity)