10 #ifndef RAD_LOGGER_HPP
11 #define RAD_LOGGER_HPP
25 inline static double GetTime() {
27 gettimeofday(&tp, NULL);
28 return (tp.tv_sec + (tp.tv_usec / 1e6));
37 inline static std::string ConvertToIsoTime(
const double timestamp) {
38 char tmp_iso_time[80];
39 long seconds = timestamp;
40 strftime(tmp_iso_time, 80,
"%Y-%m-%dT%H:%M:%S", localtime(&seconds));
42 snprintf(tmp_mus,
sizeof(tmp_mus),
"%.6f", (timestamp - seconds));
44 snprintf(iso_time,
sizeof(iso_time),
"%s.%s", tmp_iso_time,
45 std::string(tmp_mus).substr(2).c_str());
53 inline static std::string GetTimestamp() {
54 return rad::ConvertToIsoTime(rad::GetTime());
56 char buffer[RAD_LOG_TIMESTAMP_MAXLEN];
57 char result[RAD_LOG_TIMESTAMP_MAXLEN];
63 timeInfo = localtime(&rawTime);
65 strftime(buffer, RAD_LOG_TIMESTAMP_MAXLEN,
"%Y-%m-%dT%H:%M:%S", timeInfo);
68 gettimeofday(&tv,
nullptr );
69 snprintf(result, RAD_LOG_TIMESTAMP_MAXLEN,
"%s.%6.6ld", buffer, tv.tv_usec);
115 inline static std::string&
ProcName();
130 fprintf(stdout,
"%s %s %s %s", rad::GetTimestamp().c_str(),
ModuleName().c_str(),
147 return reportingLevel;
151 static bool isConsoleEnabled =
false;
152 return isConsoleEnabled;
156 static bool isLogMonEnabled =
false;
157 return isLogMonEnabled;
165 static std::string modName =
"";
170 static const char *
const buffer[] = {
171 "ERROR",
"WARNING",
"STATE",
"EVENT",
"GUARD",
"ACTION",
"INFO",
172 "DEBUG",
"DEBUG1",
"DEBUG2",
"DEBUG3",
"DEBUG4",
"TRACE"};
209 const char* fileName,
210 const char* funcName,
213 Logger().
Get(level) << fileName <<
":" << lineNum <<
" " << funcName;
218 const char* funcName,
219 const std::string& a) {
221 Logger().
Get(level) << a <<
" (" << __FUNCTION__ <<
")";
226 const std::string& b) {
244 #ifndef RAD_LOG_MAX_LEVEL
245 #define RAD_LOG_MAX_LEVEL rad::LOG_LEVEL_TRACE
248 #define RAD_LOG_SETLEVEL(levelName) \
249 rad::Logger::ReportingLevel() = rad::Logger::LevelFromString(levelName)
251 #define RAD_LOG_SETMODNAME(name) rad::Logger::ModuleName() = name
252 #define RAD_LOG_SETPROCNAME(name) rad::Logger::ProcName() = name
253 #define RAD_LOG_TO_LOGMON(isEnabled) rad::Logger::EnableLogMon() = isEnabled
254 #define RAD_LOG_TO_CONSOLE(isEnabled) rad::Logger::EnableConsole() = isEnabled
256 #define RAD_LOG(level) \
257 if (level > RAD_LOG_MAX_LEVEL) \
259 else if (level > rad::Logger::ReportingLevel()) \
261 else if (rad::Logger::IsEnabled() == false) \
264 rad::Logger().Get(level) << __FILE__ << ":" << __LINE__ << " "
266 #define RAD_LOG_ERROR() \
267 if (rad::LOG_LEVEL_ERROR > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
270 rad::Logger().Get(rad::LOG_LEVEL_ERROR) << __FILE__ << ":" << __LINE__ << " "
272 #define RAD_LOG_WARNING() \
273 if (rad::LOG_LEVEL_WARNING > rad::Logger::ReportingLevel() || \
274 !rad::Logger::IsEnabled()) \
277 rad::Logger().Get(rad::LOG_LEVEL_WARNING) << __FILE__ << ":" << __LINE__ << " "
279 #define RAD_LOG_INFO() \
280 if (rad::LOG_LEVEL_INFO > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
283 rad::Logger().Get(rad::LOG_LEVEL_INFO)
285 #define RAD_LOG_DEBUG() \
286 if (rad::LOG_LEVEL_DEBUG > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
289 rad::Logger().Get(rad::LOG_LEVEL_DEBUG) << __FILE__ << ":" << __LINE__ << " "
291 #define RAD_LOG_DEBUG1() \
292 if (rad::LOG_LEVEL_DEBUG1 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
295 rad::Logger().Get(rad::LOG_LEVEL_DEBUG1) << __FILE__ << ":" << __LINE__ << " "
297 #define RAD_LOG_DEBUG2() \
298 if (rad::LOG_LEVEL_DEBUG2 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
301 rad::Logger().Get(rad::LOG_LEVEL_DEBUG2) << __FILE__ << ":" << __LINE__ << " "
303 #define RAD_LOG_DEBUG3() \
304 if (rad::LOG_LEVEL_DEBUG3 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
307 rad::Logger().Get(rad::LOG_LEVEL_DEBUG3) << __FILE__ << ":" << __LINE__ << " "
309 #define RAD_LOG_DEBUG4() \
310 if (rad::LOG_LEVEL_DEBUG4 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
313 rad::Logger().Get(rad::LOG_LEVEL_DEBUG4) << __FILE__ << ":" << __LINE__ << " "
315 #define RAD_LOG_STATE(a, b) rad::LogStateFunction(a, b)
316 #define RAD_LOG_EVENT(a) rad::LogEventFunction(a)
317 #define RAD_LOG_GUARD(a) rad::LogFunction(rad::LOG_LEVEL_GUARD, __FUNCTION__, a)
318 #define RAD_LOG_ACTION(a) rad::LogFunction(rad::LOG_LEVEL_ACTION, __FUNCTION__, a)
319 #define RAD_LOG_TRACE() rad::LogTraceFunction(rad::LOG_LEVEL_TRACE, __FILE__, __FUNCTION__, __LINE__)
322 #endif //__LOGGER_H__
static std::string & ProcName()
Definition: Logger.hpp:82
const int LOG_MAX_LEVEL
Definition: Logger.hpp:97
void LogEventFunction(const std::string &a)
Definition: Logger.hpp:232
void LogStateFunction(const std::string &a, const std::string &b)
Definition: Logger.hpp:225
Definition: Logger.hpp:85
Definition: Logger.hpp:88
static std::string LevelToString(LogLevel level)
Definition: Logger.hpp:169
Definition: Logger.hpp:84
static LogLevel LevelFromString(const std::string &level)
Definition: Logger.hpp:178
static bool & EnableLogMon()
Definition: Logger.hpp:155
Definition: Logger.hpp:104
std::ostringstream & Get(LogLevel level=LOG_LEVEL_INFO)
Definition: Logger.hpp:140
static LogLevel & ReportingLevel()
Definition: Logger.hpp:145
Logger()
Definition: Logger.hpp:106
Definition: Logger.hpp:86
static std::string & ModuleName()
Definition: Logger.hpp:164
LogLevel
Definition: Logger.hpp:80
Definition: Logger.hpp:90
virtual ~Logger()
Definition: Logger.hpp:127
Definition: Logger.hpp:91
Definition: Logger.hpp:89
const int LOG_MAX_LEN
Definition: Logger.hpp:98
static bool & EnableConsole()
Definition: Logger.hpp:150
std::ostringstream mOutput
Definition: Logger.hpp:120
void LogFunction(const LogLevel level, const char *funcName, const std::string &a)
Definition: Logger.hpp:217
static bool IsEnabled()
Definition: Logger.hpp:160
Definition: Logger.hpp:94
Definition: Logger.hpp:83
Definition: Logger.hpp:87
const int LOG_TIMESTAMP_MAXLEN
Definition: Logger.hpp:99
Definition: Logger.hpp:92
Definition: Logger.hpp:93
Definition: Logger.hpp:81
void LogTraceFunction(const LogLevel level, const char *fileName, const char *funcName, const int lineNum)
Definition: Logger.hpp:208