ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Logger.hpp
Go to the documentation of this file.
1 
10 #ifndef RAD_LOGGER_HPP
11 #define RAD_LOGGER_HPP
12 
13 #include <cstring>
14 #include <sstream>
15 #include <stdio.h>
16 #include <string>
17 #include <sys/time.h>
18 
19 
20 namespace rad {
21 
25 inline static double GetTime() {
26  struct timeval tp;
27  gettimeofday(&tp, NULL);
28  return (tp.tv_sec + (tp.tv_usec / 1e6));
29 }
30 
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));
41  char tmp_mus[80];
42  snprintf(tmp_mus, sizeof(tmp_mus), "%.6f", (timestamp - seconds));
43  char iso_time[87];
44  snprintf(iso_time, sizeof(iso_time), "%s.%s", tmp_iso_time,
45  std::string(tmp_mus).substr(2).c_str());
46 
47  return iso_time;
48 }
49 
53 inline static std::string GetTimestamp() {
54  return rad::ConvertToIsoTime(rad::GetTime());
55 #if 0
56  char buffer[RAD_LOG_TIMESTAMP_MAXLEN];
57  char result[RAD_LOG_TIMESTAMP_MAXLEN];
58 
59  time_t rawTime;
60  time(&rawTime);
61 
62  struct tm *timeInfo;
63  timeInfo = localtime(&rawTime);
64 
65  strftime(buffer, RAD_LOG_TIMESTAMP_MAXLEN, "%Y-%m-%dT%H:%M:%S", timeInfo);
66 
67  struct timeval tv;
68  gettimeofday(&tv, nullptr /*obsolete*/);
69  snprintf(result, RAD_LOG_TIMESTAMP_MAXLEN, "%s.%6.6ld", buffer, tv.tv_usec);
70 
71  return result;
72 #endif
73 }
74 
75 
76 
77 /*
78  * Supported Log Levels.
79  */
80 enum LogLevel {
95 };
96 
98 const int LOG_MAX_LEN = 2048;
99 const int LOG_TIMESTAMP_MAXLEN = 100;
100 
101 /*
102  * Logger class.
103  */
104 class Logger {
105 public:
106  Logger() {};
107  inline virtual ~Logger();
108  inline std::ostringstream& Get(LogLevel level = LOG_LEVEL_INFO);
109 
110  inline static LogLevel& ReportingLevel();
111  inline static bool& EnableConsole();
112  inline static bool& EnableLogMon();
113  inline static bool IsEnabled();
114  inline static std::string& ModuleName();
115  inline static std::string& ProcName();
116  inline static std::string LevelToString(LogLevel level);
117  inline static LogLevel LevelFromString(const std::string &level);
118 
119 protected:
120  std::ostringstream mOutput;
121 
122 private:
123  Logger(const Logger &);
124  Logger &operator=(const Logger &);
125 };
126 
128  mOutput << std::endl;
129  if (Logger::EnableConsole() == true) {
130  fprintf(stdout, "%s %s %s %s", rad::GetTimestamp().c_str(), ModuleName().c_str(),
131  ModuleName().c_str(), mOutput.str().c_str());
132  fflush(stdout);
133  }
134 
135  if (Logger::EnableLogMon() == true) {
136  // @TODO add code to verbose the log somewhere
137  }
138 }
139 
140 std::ostringstream& Logger::Get(LogLevel level) {
141  mOutput << LevelToString(level) << " ";
142  return mOutput;
143 }
144 
146  static LogLevel reportingLevel = LOG_LEVEL_INFO;
147  return reportingLevel;
148 }
149 
151  static bool isConsoleEnabled = false;
152  return isConsoleEnabled;
153 }
154 
156  static bool isLogMonEnabled = false;
157  return isLogMonEnabled;
158 }
159 
161  return Logger::EnableConsole() == true || Logger::EnableLogMon() == true;
162 }
163 
164 std::string& Logger::ModuleName() {
165  static std::string modName = ""; // @TODO initialized to module name
166  return modName;
167 }
168 
169 std::string Logger::LevelToString(LogLevel level) {
170  static const char *const buffer[] = {
171  "ERROR", "WARNING", "STATE", "EVENT", "GUARD", "ACTION", "INFO",
172  "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4", "TRACE"};
173  return (level < LOG_LEVEL_ERROR || level > LOG_LEVEL_TRACE)
174  ? ""
175  : buffer[level];
176 }
177 
178 LogLevel Logger::LevelFromString(const std::string &level) {
179  if (level == LevelToString(rad::LOG_LEVEL_TRACE))
180  return rad::LOG_LEVEL_TRACE;
181  else if (level == LevelToString(rad::LOG_LEVEL_DEBUG4))
182  return rad::LOG_LEVEL_DEBUG4;
183  else if (level == LevelToString(rad::LOG_LEVEL_DEBUG3))
184  return rad::LOG_LEVEL_DEBUG3;
185  else if (level == LevelToString(rad::LOG_LEVEL_DEBUG2))
186  return rad::LOG_LEVEL_DEBUG2;
187  else if (level == LevelToString(rad::LOG_LEVEL_DEBUG1))
188  return rad::LOG_LEVEL_DEBUG1;
189  else if (level == LevelToString(rad::LOG_LEVEL_DEBUG))
190  return rad::LOG_LEVEL_DEBUG;
191  else if (level == LevelToString(rad::LOG_LEVEL_INFO))
192  return rad::LOG_LEVEL_INFO;
193  else if (level == LevelToString(rad::LOG_LEVEL_ACTION))
194  return rad::LOG_LEVEL_ACTION;
195  else if (level == LevelToString(rad::LOG_LEVEL_GUARD))
196  return rad::LOG_LEVEL_GUARD;
197  else if (level == LevelToString(rad::LOG_LEVEL_EVENT))
198  return rad::LOG_LEVEL_EVENT;
199  else if (level == LevelToString(rad::LOG_LEVEL_STATE))
200  return rad::LOG_LEVEL_STATE;
201  else if (level == LevelToString(rad::LOG_LEVEL_WARNING))
202  return rad::LOG_LEVEL_WARNING;
203  else if (level == LevelToString(rad::LOG_LEVEL_ERROR))
204  return rad::LOG_LEVEL_ERROR;
205  return rad::LOG_LEVEL_UNKNOWN;
206 }
207 
208 inline void LogTraceFunction(const LogLevel level,
209  const char* fileName,
210  const char* funcName,
211  const int lineNum) {
212  if (level <= Logger::ReportingLevel() && Logger::IsEnabled()) {
213  Logger().Get(level) << fileName << ":" << lineNum << " " << funcName;
214  }
215 }
216 
217 inline void LogFunction(const LogLevel level,
218  const char* funcName,
219  const std::string& a) {
220  if (level <= Logger::ReportingLevel() && Logger::IsEnabled()) {
221  Logger().Get(level) << a << " (" << __FUNCTION__ << ")";
222  }
223 }
224 
225 inline void LogStateFunction(const std::string& a,
226  const std::string& b) {
228  Logger().Get(LOG_LEVEL_STATE) << "from " << a << " to " << b;
229  }
230 }
231 
232 inline void LogEventFunction(const std::string& a) {
234  Logger().Get(LOG_LEVEL_EVENT) << a;
235  }
236 }
237 
238 } // namespace rad
239 
240 
241 /*
242  * Logging definitions
243  */
244 #ifndef RAD_LOG_MAX_LEVEL
245 #define RAD_LOG_MAX_LEVEL rad::LOG_LEVEL_TRACE
246 #endif
247 
248 #define RAD_LOG_SETLEVEL(levelName) \
249  rad::Logger::ReportingLevel() = rad::Logger::LevelFromString(levelName)
250 
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
255 
256 #define RAD_LOG(level) \
257  if (level > RAD_LOG_MAX_LEVEL) \
258  ; \
259  else if (level > rad::Logger::ReportingLevel()) \
260  ; \
261  else if (rad::Logger::IsEnabled() == false) \
262  ; \
263  else \
264  rad::Logger().Get(level) << __FILE__ << ":" << __LINE__ << " "
265 
266 #define RAD_LOG_ERROR() \
267  if (rad::LOG_LEVEL_ERROR > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
268  ; \
269  else \
270  rad::Logger().Get(rad::LOG_LEVEL_ERROR) << __FILE__ << ":" << __LINE__ << " "
271 
272 #define RAD_LOG_WARNING() \
273  if (rad::LOG_LEVEL_WARNING > rad::Logger::ReportingLevel() || \
274  !rad::Logger::IsEnabled()) \
275  ; \
276  else \
277  rad::Logger().Get(rad::LOG_LEVEL_WARNING) << __FILE__ << ":" << __LINE__ << " "
278 
279 #define RAD_LOG_INFO() \
280  if (rad::LOG_LEVEL_INFO > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
281  ; \
282  else \
283  rad::Logger().Get(rad::LOG_LEVEL_INFO)
284 
285 #define RAD_LOG_DEBUG() \
286  if (rad::LOG_LEVEL_DEBUG > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
287  ; \
288  else \
289  rad::Logger().Get(rad::LOG_LEVEL_DEBUG) << __FILE__ << ":" << __LINE__ << " "
290 
291 #define RAD_LOG_DEBUG1() \
292  if (rad::LOG_LEVEL_DEBUG1 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
293  ; \
294  else \
295  rad::Logger().Get(rad::LOG_LEVEL_DEBUG1) << __FILE__ << ":" << __LINE__ << " "
296 
297 #define RAD_LOG_DEBUG2() \
298  if (rad::LOG_LEVEL_DEBUG2 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
299  ; \
300  else \
301  rad::Logger().Get(rad::LOG_LEVEL_DEBUG2) << __FILE__ << ":" << __LINE__ << " "
302 
303 #define RAD_LOG_DEBUG3() \
304  if (rad::LOG_LEVEL_DEBUG3 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
305  ; \
306  else \
307  rad::Logger().Get(rad::LOG_LEVEL_DEBUG3) << __FILE__ << ":" << __LINE__ << " "
308 
309 #define RAD_LOG_DEBUG4() \
310  if (rad::LOG_LEVEL_DEBUG4 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
311  ; \
312  else \
313  rad::Logger().Get(rad::LOG_LEVEL_DEBUG4) << __FILE__ << ":" << __LINE__ << " "
314 
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__)
320 
321 
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