All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Log.h
Go to the documentation of this file.
1 #ifndef LOG_H_
2 #define LOG_H_
3 
4 #include <iostream>
5 #include <mutex>
6 
7 #include "StringBuilder.h"
8 
9 #ifdef NOLOGGING
10 
11 #define FATAL(...) do{}while(false)
12 #define ERROR(...) do{}while(false)
13 #define WARN(...) do{}while(false)
14 #define INFO(...) do{}while(false)
15 #define DEBUG(...) do{}while(false)
16 #define TRACE(...) do{}while(false)
17 
18 #define FATALF(...) do{}while(false)
19 #define ERRORF(...) do{}while(false)
20 #define WARNF(...) do{}while(false)
21 #define INFOF(...) do{}while(false)
22 #define DEBUGF(...) do{}while(false)
23 #define TRACEF(...) do{}while(false)
24 
25 #define TRACEPOINT do{}while(false)
26 
27 #else // NOLOGGING
28 
29 #define LOG_LEVEL_FATAL 0
30 #define LOG_LEVEL_ERROR 1
31 #define LOG_LEVEL_WARN 2
32 #define LOG_LEVEL_INFO 3
33 #define LOG_LEVEL_DEBUG 4
34 #define LOG_LEVEL_TRACE 5
35 
36 
37 #if !defined(LOG_LEVEL)
38 #define LOG_LEVEL LOG_LEVEL_TRACE
39 #endif
40 
41 #if LOG_LEVEL >= LOG_LEVEL_FATAL
42 #define FATAL(...) ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
43  ::Aux::Log::LogLevel::fatal, __VA_ARGS__)
44 #define FATALF(...) ::Aux::Log::logF({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
45  ::Aux::Log::LogLevel::fatal, __VA_ARGS__)
46 #else
47 #define FATAL(...) do{}while(false)
48 #define FATALF(...) do{}while(false)
49 #endif
50 
51 #if LOG_LEVEL >= LOG_LEVEL_ERROR
52 #define ERROR(...) ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
53  ::Aux::Log::LogLevel::error, __VA_ARGS__)
54 #define ERRORF(...) ::Aux::Log::logF({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
55  ::Aux::Log::LogLevel::error, __VA_ARGS__)
56 #else
57 #define ERROR(...) do{}while(false)
58 #define ERRORF(...) do{}while(false)
59 #endif
60 
61 #if LOG_LEVEL >= LOG_LEVEL_WARN
62 #define WARN(...) ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
63  ::Aux::Log::LogLevel::warn, __VA_ARGS__)
64 #define WARNF(...) ::Aux::Log::logF({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
65  ::Aux::Log::LogLevel::warn, __VA_ARGS__)
66 #else
67 #define WARN(...) do{}while(false)
68 #define WARNF(...) do{}while(false)
69 #endif
70 
71 #if LOG_LEVEL >= LOG_LEVEL_INFO
72 #define INFO(...) ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
73  ::Aux::Log::LogLevel::info, __VA_ARGS__)
74 #define INFOF(...) ::Aux::Log::logF({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
75  ::Aux::Log::LogLevel::info, __VA_ARGS__)
76 #else
77 #define INFO(...) do{}while(false)
78 #define INFOF(...) do{}while(false)
79 #endif
80 
81 #if LOG_LEVEL >= LOG_LEVEL_DEBUG
82 #define DEBUG(...) ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
83  ::Aux::Log::LogLevel::debug, __VA_ARGS__)
84 #define DEBUGF(...) ::Aux::Log::logF({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
85  ::Aux::Log::LogLevel::debug, __VA_ARGS__)
86 #else
87 #define DEBUG(...) do{}while(false)
88 #define DEBUGF(...) do{}while(false)
89 #endif
90 
91 #if LOG_LEVEL >= LOG_LEVEL_TRACE
92 #define TRACE(...) ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
93  ::Aux::Log::LogLevel::trace, __VA_ARGS__)
94 #define TRACEF(...) ::Aux::Log::logF({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
95  ::Aux::Log::LogLevel::trace, __VA_ARGS__)
96 #define TRACEPOINT ::Aux::Log::log({__FILE__, __PRETTY_FUNCTION__, __LINE__},\
97  ::Aux::Log::LogLevel::trace, "tracepoint")
98 #else
99 #define TRACE(...) do{}while(false)
100 #define TRACEF(...) do{}while(false)
101 #define TRACEPOINT do{}while(false)
102 #endif
103 
104 #endif // NOLOGGING
105 
106 namespace Aux { namespace Log {
107 
108 struct Location {
109  const char* file;
110  const char* function;
111  const int line;
112 };
113 
114 enum class LogLevel {
115  trace,
116  debug,
117  info,
118  warn,
119  error,
120  fatal
121 };
122 
127 void setLogLevel(std::string logLevel);
128 
132 std::string getLogLevel();
133 
134 namespace Settings {
135 
138 
139 void setPrintTime(bool b);
140 bool getPrintTime();
141 
142 void setPrintLocation(bool b);
143 bool getPrintLocation();
144 
145 void setLogfile(const std::string& filename);
146 }
147 
148 namespace Impl {
149 void log(const Location& loc, LogLevel p, const std::string msg);
150 } //namespace impl
151 
152 template<typename...T>
153 void log(const Location& loc, LogLevel p, const T&...args) {
154  if(p >= Settings::getLogLevel()) {
155  std::stringstream stream;
156  printToStream(stream, args...);
157  Impl::log(loc, p, stream.str());
158  }
159 }
160 
161 template<typename...T>
162 void logF(const Location& loc, LogLevel p, const std::string& format, const T&...args) {
163  if(p >= Settings::getLogLevel()) {
164  std::stringstream stream;
165  printToStreamF(stream, format, args...);
166  Impl::log(loc, p, stream.str());
167  }
168 }
169 
170 
171 }} // namespace Aux::Log
172 
173 #endif
ValueType Log(const ValueType &x, const ValueType &base, ErrorCode *err=0)
Definition: ttmath.h:274
std::ostream & printToStream(std::ostream &stream, const T &...args)
Definition: StringBuilder.h:273
LogLevel getLogLevel()
Definition: Log.cpp:63
void log(const Location &loc, LogLevel p, const std::string msg)
Definition: Log.cpp:202
void logF(const Location &loc, LogLevel p, const std::string &format, const T &...args)
Definition: Log.h:162
bool getPrintLocation()
Definition: Log.cpp:70
std::string getLogLevel()
Definition: Log.cpp:30
const int line
Definition: Log.h:111
void setLogLevel(std::string logLevel)
Accept loglevel as string and set.
Definition: Log.cpp:12
void setPrintTime(bool b)
Definition: Log.cpp:66
const char * file
Definition: Log.h:109
LogLevel
Definition: Log.h:114
void log(const Location &loc, LogLevel p, const T &...args)
Definition: Log.h:153
void setLogLevel(LogLevel p)
Definition: Log.cpp:64
std::ostream & printToStreamF(std::ostream &stream, const std::string &format, const T &...args)
Definition: StringBuilder.h:286
Definition: Log.h:108
void setLogfile(const std::string &filename)
Definition: Log.cpp:72
bool getPrintTime()
Definition: Log.cpp:67
void setPrintLocation(bool b)
Definition: Log.cpp:69