rad 8.2.0
Loading...
Searching...
No Matches
simpleLogger.hpp
Go to the documentation of this file.
1
9
10#ifndef RAD_SIMPLE_LOGGER_HPP
11#define RAD_SIMPLE_LOGGER_HPP
12
13#include <rad/helper.hpp>
14#include <source_location>
15#include <syslog.h>
16
17namespace rad {
18
19/*
20 * Supported Log Levels.
21 */
38
40const int LOG_MAX_LEN = 2048;
41const int LOG_TIMESTAMP_MAXLEN = 100;
42
43/*
44 * SimpleLogger class.
45 */
47 public:
49 inline virtual ~SimpleLogger();
50 inline std::ostringstream& Get(const LogLevel level = LOG_LEVEL_INFO,
51 const std::source_location& location = std::source_location::current());
52
53 inline static LogLevel& ReportingLevel();
54 inline static bool& EnableConsole();
55 inline static bool& EnableLogMon();
56 inline static bool IsEnabled();
57 inline static std::string& ModuleName();
58 inline static std::string& ProcName();
59 inline static std::string& HostName();
60 inline static std::string LevelToString(LogLevel level);
61 inline static std::string LevelToStringLogMon(LogLevel level);
62 inline static LogLevel LevelFromString(const std::string& level);
63 inline static void OpenLogMon(const char* procname);
64 inline static void CloseLogMon();
65
66 protected:
67 std::ostringstream m_output;
69 std::source_location m_location;
70
71 private:
73 SimpleLogger& operator=(const SimpleLogger&);
74};
75
77 if (SimpleLogger::EnableConsole() == true) {
78 std::string level_name = LevelToString(m_level);
79 if (m_level == LOG_LEVEL_INFO) {
80 fprintf(stdout, "%s %s %s %s %s\n",
81 rad::GetTimestamp().c_str(),
82 ModuleName().c_str(),
83 ProcName().c_str(),
84 level_name.c_str(),
85 m_output.str().c_str());
86 } else {
87 fprintf(stdout, "%s %s %s %s %s:%d %s\n",
88 rad::GetTimestamp().c_str(),
89 ModuleName().c_str(),
90 ProcName().c_str(),
91 level_name.c_str(),
92 m_location.file_name(),
93 m_location.line(),
94 m_output.str().c_str());
95 }
96 fflush(stdout);
97 }
98
99 if (SimpleLogger::EnableLogMon() == true) {
100 syslog(LOG_INFO,
101 "Cref=%s:%d, Date=%s, HostName=%s, LogType=%s, "
102 "SourceID=%s, LogID=%s-%d, Message=%s, AppName=%s",
103 m_location.file_name(),
104 m_location.line(),
105 rad::GetTimestamp().c_str(),
106 HostName().c_str(),
108 ModuleName().c_str(),
109 m_location.function_name(),
110 m_location.line(),
111 m_output.str().c_str(),
112 ProcName().c_str());
113 }
114}
115
116std::ostringstream& SimpleLogger::Get(LogLevel level,
117 const std::source_location& location) {
118 m_level = level;
119 m_location = location;
120 return m_output;
121}
122
124 static LogLevel reporting_level = LOG_LEVEL_INFO;
125 return reporting_level;
126}
127
129 static bool is_console_enabled = false;
130 return is_console_enabled;
131}
132
134 static bool is_logmon_enabled = false;
135 return is_logmon_enabled;
136}
137
141
143 static std::string mod_name = "N/A";
144 return mod_name;
145}
146
148 static std::string proc_name = "N/A";
149 return proc_name;
150}
151
153 static std::string host_name = "N/A";
154 return host_name;
155}
156
158 static const char* const buffer[] = {"ERROR", "WARNING", "STATE", "EVENT", "GUARD",
159 "ACTION", "INFO", "DEBUG", "DEBUG1", "DEBUG2",
160 "DEBUG3", "DEBUG4", "TRACE"};
161 return (level < LOG_LEVEL_ERROR || level > LOG_LEVEL_TRACE) ? "" : buffer[level];
162}
163
165 static const char* const buffer[] = {"ERROR", "WARN", "INFO", "INFO", "INFO",
166 "INFO", "INFO", "DEBUG", "DEBUG", "DEBUG",
167 "DEBUG", "DEBUG", "TRACE"};
168 return (level < LOG_LEVEL_ERROR || level > LOG_LEVEL_TRACE) ? "" : buffer[level];
169}
170
171LogLevel SimpleLogger::LevelFromString(const std::string& level) {
172 if (level == LevelToString(rad::LOG_LEVEL_TRACE)) {
174 } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG4)) {
176 } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG3)) {
178 } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG2)) {
180 } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG1)) {
182 } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG)) {
184 } else if (level == LevelToString(rad::LOG_LEVEL_INFO)) {
185 return rad::LOG_LEVEL_INFO;
186 } else if (level == LevelToString(rad::LOG_LEVEL_ACTION)) {
188 } else if (level == LevelToString(rad::LOG_LEVEL_GUARD)) {
190 } else if (level == LevelToString(rad::LOG_LEVEL_EVENT)) {
192 } else if (level == LevelToString(rad::LOG_LEVEL_STATE)) {
194 } else if (level == LevelToString(rad::LOG_LEVEL_WARNING)) {
196 } else if (level == LevelToString(rad::LOG_LEVEL_ERROR)) {
198 }
200}
201
202void SimpleLogger::OpenLogMon(const char* procname) {
203 openlog(procname, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
204}
205
207 closelog();
208}
209
210inline void LogTraceFunction(const LogLevel level, const char* filename, const char* funcname,
211 const int linenum) {
213 SimpleLogger().Get(level) << filename << ":" << linenum << " " << funcname;
214 }
215}
216
217inline void LogFunction(const LogLevel level, const char* funcname, const std::string& a) {
219 SimpleLogger().Get(level) << a << " (" << __FUNCTION__ << ")";
220 }
221}
222
223inline void LogStateFunction(const std::string& a, const std::string& b) {
225 SimpleLogger().Get(LOG_LEVEL_STATE) << "from " << a << " to " << b;
226 }
227}
228
229inline void LogEventFunction(const std::string& a) {
232 }
233}
234
235} // namespace rad
236
237/*
238 * Logging definitions
239 */
240#ifndef RAD_LOG_MAX_LEVEL
241#define RAD_LOG_MAX_LEVEL rad::LOG_LEVEL_TRACE
242#endif
243
244#define RAD_LOG_SETLEVEL(levelName) \
245 rad::SimpleLogger::ReportingLevel() = rad::SimpleLogger::LevelFromString(levelName)
246
247#define RAD_LOG_SETMODNAME(name) rad::SimpleLogger::ModuleName() = name
248#define RAD_LOG_SETPROCNAME(name) rad::SimpleLogger::ProcName() = name
249#define RAD_LOG_SETHOSTNAME(name) rad::SimpleLogger::HostName() = name
250#define RAD_LOG_TO_LOGMON(is_enabled) rad::SimpleLogger::EnableLogMon() = is_enabled
251#define RAD_LOG_TO_CONSOLE(is_enabled) rad::SimpleLogger::EnableConsole() = is_enabled
252#define RAD_LOG_LOGMON_OPEN(ident) rad::SimpleLogger::OpenLogMon(ident)
253#define RAD_LOG_LOGMON_CLOSE() rad::SimpleLogger::CloseLogMon()
254
255#define RAD_LOG(level) \
256 if (level > RAD_LOG_MAX_LEVEL) \
257 ; \
258 else if (level > rad::SimpleLogger::ReportingLevel()) \
259 ; \
260 else if (rad::SimpleLogger::IsEnabled() == false) \
261 ; \
262 else \
263 rad::SimpleLogger().Get(level)
264
265#define RAD_LOG_ERROR() \
266 if (rad::LOG_LEVEL_ERROR > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
267 ; \
268 else \
269 rad::SimpleLogger().Get(rad::LOG_LEVEL_ERROR)
270
271#define RAD_LOG_WARNING() \
272 if (rad::LOG_LEVEL_WARNING > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
273 ; \
274 else \
275 rad::SimpleLogger().Get(rad::LOG_LEVEL_WARNING)
276
277#define RAD_LOG_INFO() \
278 if (rad::LOG_LEVEL_INFO > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
279 ; \
280 else \
281 rad::SimpleLogger().Get(rad::LOG_LEVEL_INFO)
282
283#define RAD_LOG_DEBUG() \
284 if (rad::LOG_LEVEL_DEBUG > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
285 ; \
286 else \
287 rad::SimpleLogger().Get(rad::LOG_LEVEL_DEBUG)
288
289#define RAD_LOG_DEBUG1() \
290 if (rad::LOG_LEVEL_DEBUG1 > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
291 ; \
292 else \
293 rad::SimpleLogger().Get(rad::LOG_LEVEL_DEBUG1)
294
295#define RAD_LOG_DEBUG2() \
296 if (rad::LOG_LEVEL_DEBUG2 > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
297 ; \
298 else \
299 rad::SimpleLogger().Get(rad::LOG_LEVEL_DEBUG2)
300
301#define RAD_LOG_DEBUG3() \
302 if (rad::LOG_LEVEL_DEBUG3 > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
303 ; \
304 else \
305 rad::SimpleLogger().Get(rad::LOG_LEVEL_DEBUG3)
306
307#define RAD_LOG_DEBUG4() \
308 if (rad::LOG_LEVEL_DEBUG4 > rad::SimpleLogger::ReportingLevel() || !rad::SimpleLogger::IsEnabled()) \
309 ; \
310 else \
311 rad::SimpleLogger().Get(rad::LOG_LEVEL_DEBUG4)
312
313// (void)("LCOV_EXCL_BR_LINE") has been added to skip branch coverage the macros.
314#define RAD_LOG_STATE(a, b) \
315 rad::LogStateFunction(a, b); \
316 (void)("LCOV_EXCL_BR_LINE")
317#define RAD_LOG_EVENT(a) \
318 rad::LogEventFunction(a); \
319 (void)("LCOV_EXCL_BR_LINE")
320#define RAD_LOG_GUARD(a) \
321 rad::LogFunction(rad::LOG_LEVEL_GUARD, __FUNCTION__, a); \
322 (void)("LCOV_EXCL_BR_LINE")
323#define RAD_LOG_ACTION(a) \
324 rad::LogFunction(rad::LOG_LEVEL_ACTION, __FUNCTION__, a); \
325 (void)("LCOV_EXCL_BR_LINE")
326#define RAD_LOG_TRACE() \
327 rad::LogTraceFunction(rad::LOG_LEVEL_TRACE, __FILE__, __FUNCTION__, __LINE__); \
328 (void)("LCOV_EXCL_BR_LINE")
329
330#endif // RAD_SIMPLE_LOGGER_HPP
Definition simpleLogger.hpp:46
static std::string & ProcName()
Definition simpleLogger.hpp:147
std::ostringstream m_output
Definition simpleLogger.hpp:67
SimpleLogger()
Definition simpleLogger.hpp:48
static bool & EnableLogMon()
Definition simpleLogger.hpp:133
static std::string LevelToStringLogMon(LogLevel level)
Definition simpleLogger.hpp:164
static void OpenLogMon(const char *procname)
Definition simpleLogger.hpp:202
static std::string & ModuleName()
Definition simpleLogger.hpp:142
static bool & EnableConsole()
Definition simpleLogger.hpp:128
static LogLevel LevelFromString(const std::string &level)
Definition simpleLogger.hpp:171
std::source_location m_location
Definition simpleLogger.hpp:69
static LogLevel & ReportingLevel()
Definition simpleLogger.hpp:123
static std::string & HostName()
Definition simpleLogger.hpp:152
std::ostringstream & Get(const LogLevel level=LOG_LEVEL_INFO, const std::source_location &location=std::source_location::current())
Definition simpleLogger.hpp:116
virtual ~SimpleLogger()
Definition simpleLogger.hpp:76
static std::string LevelToString(LogLevel level)
Definition simpleLogger.hpp:157
LogLevel m_level
Definition simpleLogger.hpp:68
static bool IsEnabled()
Definition simpleLogger.hpp:138
static void CloseLogMon()
Definition simpleLogger.hpp:206
Helper class header file.
Definition actionsApp.cpp:67
const int LOG_MAX_LEVEL
Definition simpleLogger.hpp:39
void LogFunction(const LogLevel level, const char *funcname, const std::string &a)
Definition simpleLogger.hpp:217
void LogTraceFunction(const LogLevel level, const char *filename, const char *funcname, const int linenum)
Definition simpleLogger.hpp:210
const int LOG_MAX_LEN
Definition simpleLogger.hpp:40
const int LOG_TIMESTAMP_MAXLEN
Definition simpleLogger.hpp:41
void LogStateFunction(const std::string &a, const std::string &b)
Definition simpleLogger.hpp:223
void LogEventFunction(const std::string &a)
Definition simpleLogger.hpp:229
LogLevel
Definition simpleLogger.hpp:22
@ LOG_LEVEL_EVENT
Definition simpleLogger.hpp:27
@ LOG_LEVEL_STATE
Definition simpleLogger.hpp:26
@ LOG_LEVEL_DEBUG
Definition simpleLogger.hpp:31
@ LOG_LEVEL_DEBUG3
Definition simpleLogger.hpp:34
@ LOG_LEVEL_GUARD
Definition simpleLogger.hpp:28
@ LOG_LEVEL_INFO
Definition simpleLogger.hpp:30
@ LOG_LEVEL_DEBUG2
Definition simpleLogger.hpp:33
@ LOG_LEVEL_TRACE
Definition simpleLogger.hpp:36
@ LOG_LEVEL_ACTION
Definition simpleLogger.hpp:29
@ LOG_LEVEL_WARNING
Definition simpleLogger.hpp:25
@ LOG_LEVEL_DEBUG4
Definition simpleLogger.hpp:35
@ LOG_LEVEL_DEBUG1
Definition simpleLogger.hpp:32
@ LOG_LEVEL_UNKNOWN
Definition simpleLogger.hpp:23
@ LOG_LEVEL_ERROR
Definition simpleLogger.hpp:24