ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
csvlog.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Robert Markowski - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <ctime>
23 
28 #define LOG_T(...) if (CSVLog::getCurrentLogLevelConsole()<=CSVLog::_TRACE) \
29  CSVLog::getInstance().logTraceConsole(__VA_ARGS__); \
30  if (CSVLog::getInstance().hasFile() && CSVLog::getCurrentLogLevelFile()<=CSVLog::_TRACE) \
31  CSVLog::getInstance().logTraceFile(__VA_ARGS__)
32 
37 #define LOG_I(...) if (CSVLog::getCurrentLogLevelConsole()<=CSVLog::_INFO) \
38  CSVLog::getInstance().logInfoConsole(__VA_ARGS__); \
39  if (CSVLog::getInstance().hasFile() && CSVLog::getCurrentLogLevelFile()<=CSVLog::_INFO) \
40  CSVLog::getInstance().logInfoFile(__VA_ARGS__)
41 
46 #define LOG_W(...) if (CSVLog::getCurrentLogLevelConsole()<=CSVLog::_WARNING) \
47  CSVLog::getInstance().logWarningConsole(__VA_ARGS__); \
48  if (CSVLog::getInstance().hasFile() && CSVLog::getCurrentLogLevelFile()<=CSVLog::_WARNING) \
49  CSVLog::getInstance().logWarningFile(__VA_ARGS__)
50 
55 #define LOG_E(...) if (CSVLog::getCurrentLogLevelConsole()<=CSVLog::_ERROR) \
56  CSVLog::getInstance().logErrorConsole(__VA_ARGS__); \
57  if (CSVLog::getInstance().hasFile() && CSVLog::getCurrentLogLevelFile()<=CSVLog::_ERROR) \
58  CSVLog::getInstance().logErrorFile(__VA_ARGS__)
59 
64 #define LOG_SET_LVL_FROM_CONSOLE(...) CSVLog::getInstance().setLogLevelFromConsole()
65 
70 #define LOG_SET_LVL_T(...) CSVLog::getInstance().setLogLevel(CSVLog::_TRACE)
71 
76 #define LOG_SET_LVL_I(...) CSVLog::getInstance().setLogLevel(CSVLog::_INFO)
77 
82 #define LOG_SET_LVL_W(...) CSVLog::getInstance().setLogLevel(CSVLog::_WARNING)
83 
88 #define LOG_SET_LVL_E(...) CSVLog::getInstance().setLogLevel(CSVLog::_ERROR)
89 
94 #define LOG_SET_LVL_CONSOLE_T(...) CSVLog::getInstance().setLogLevelConsole(CSVLog::_TRACE)
95 
100 #define LOG_SET_LVL_CONSOLE_I(...) CSVLog::getInstance().setLogLevelConsole(CSVLog::_INFO)
101 
106 #define LOG_SET_LVL_CONSOLE_W(...) CSVLog::getInstance().setLogLevelConsole(CSVLog::_WARNING)
107 
112 #define LOG_SET_LVL_CONSOLE_E(...) CSVLog::getInstance().setLogLevelConsole(CSVLog::_ERROR)
113 
118 #define LOG_SET_LVL_FILE_T(...) CSVLog::getInstance().setLogLevelFile(CSVLog::_TRACE)
119 
124 #define LOG_SET_LVL_FILE_I(...) CSVLog::getInstance().setLogLevelFile(CSVLog::_INFO)
125 
130 #define LOG_SET_LVL_FILE_W(...) CSVLog::getInstance().setLogLevelFile(CSVLog::_WARNING)
131 
136 #define LOG_SET_LVL_FILE_E(...) CSVLog::getInstance().setLogLevelFile(CSVLog::_ERROR)
137 
138 
143 #define LOG_SET_SEPERATOR(char_seperator) CSVLog::getInstance().setSeperator(char_seperator)
144 
149 #define LOG_SET_MAXLINES_FILE(int_maxLines) CSVLog::getInstance().setMaxLinesFile(int_maxLines)
150 
155 #define LOG_SET_MAXLINES_BUFFER(int_maxLines) CSVLog::getInstance().setMaxLinesBuffer(int_maxLines)
156 
161 #define LOG_SET_TIMESTAMPVERSION(timestampversion_tsv) CSVLog::getInstance().setTimestampVersion(timestampversion_tsv)
162 
167 #define LOG_SET_LOWTHROUGHPUTMODE(bool_active) CSVLog::getInstance().setLowThroughputMode(bool_active)
168 
173 #define LOG_SET_MAXTIMEBETWEENWRITES(double_time_seconds) CSVLog::getInstance().setTimeBetweenWrites(double_time_seconds)
174 
179 #define LOG_INIT(string_filename) CSVLog::getInstance().init(string_filename)
180 
185 class CSVLog
186 {
187 public:
192  CSVLog();
193 
198  virtual ~CSVLog();
199 
205  static CSVLog& getInstance();
206 
207  enum LogLevel{
211  _ERROR
212  };
213 
216  _EPOCH
217  };
218 
224  void init(std::string filename);
225 
231  void setLogLevel(LogLevel lvl);
232 
237  void setLogLevelFromConsole();
238 
244  void setLogLevelFile(LogLevel lvl);
245 
251  void setLogLevelConsole(LogLevel lvl);
252 
259 
265  void setSeperator(char seperator);
266 
272  void setMaxLinesBuffer(int numLines);
273 
279  void setMaxLinesFile(int numLines);
280 
286  void setLowThroughputMode(bool b);
287 
293  void setTimeBetweenWrites(double time_seconds);
294 
301 
308 
315  void logTrace(const char *format, ...);
316 
323  void logTraceConsole(const char *format, ...);
324 
331  void logTraceFile(const char *format, ...);
332 
339  void logInfo(const char *format, ...);
340 
347  void logInfoConsole(const char *format, ...);
348 
355  void logInfoFile(const char *format, ...);
356 
363  void logWarning(const char *format, ...);
364 
371  void logWarningConsole(const char *format, ...);
372 
379  void logWarningFile(const char *format, ...);
380 
387  void logError(const char *format, ...);
388 
395  void logErrorConsole(const char *format, ...);
396 
403  void logErrorFile(const char *format, ...);
404 
411  bool hasFile();
412 
413 private:
414  CSVLog(const CSVLog&);
415  void operator=(CSVLog const&);
416 
423  bool _useFile;
426  std::string _filename;
427  std::string _currentFilename;
428  std::ostringstream os;
429 
430  std::clock_t _lastWriteTime;
431 
438  void writeLog(std::string msg, LogLevel lvl);
439 
447  void writeLogToConsole(std::string time, std::string msg, LogLevel lvl);
448 
456  void writeLogToBuffer(std::string time, std::string msg, LogLevel lvl);
457 
462  void writeBufferToFile();
463 
470  void logConsole(std::string msg, LogLevel lvl);
471 
478  void logFile(std::string msg, LogLevel lvl);
479 
485  std::string getCurrentTime();
486 
492  std::string getCurrentTimeEpoch();
493 
500  std::string lvlToString(LogLevel lvl);
501 
509 
516  std::string lvlToConsoleColorLinux(LogLevel lvl);
517 };
singleton class for logging on the console and in a file, only interact with it via the defined macro...
Definition: csvlog.h:186
void setLogLevelFromConsole()
set the log level via console input
Definition: csvlog.cpp:285
void logErrorConsole(const char *format,...)
log on error level in console
Definition: csvlog.cpp:155
void logInfoFile(const char *format,...)
log on info level in file
Definition: csvlog.cpp:128
std::clock_t _lastWriteTime
Definition: csvlog.h:430
std::string _filename
Definition: csvlog.h:426
LogLevel
Definition: csvlog.h:207
@ _TRACE
Definition: csvlog.h:208
@ _WARNING
Definition: csvlog.h:210
@ _INFO
Definition: csvlog.h:209
@ _ERROR
Definition: csvlog.h:211
std::string lvlToConsoleColorLinux(LogLevel lvl)
get console color from log level on linux
Definition: csvlog.cpp:401
double _maxTimeBetweenWrites
Definition: csvlog.h:422
void logConsole(std::string msg, LogLevel lvl)
generate log entry in console
Definition: csvlog.cpp:227
void logWarning(const char *format,...)
general log on warning level
Definition: csvlog.cpp:134
TimestampVersion
Definition: csvlog.h:214
@ _TIMEOFDAY
Definition: csvlog.h:215
@ _EPOCH
Definition: csvlog.h:216
TimestampVersion _timestampVersion
Definition: csvlog.h:425
void setLogLevelConsole(LogLevel lvl)
set log level only for console
Definition: csvlog.cpp:44
void logTraceFile(const char *format,...)
log on trace level in file
Definition: csvlog.cpp:112
void logWarningConsole(const char *format,...)
log on warning level in console
Definition: csvlog.cpp:139
int _linecountBuffer
Definition: csvlog.h:418
void operator=(CSVLog const &)
void writeBufferToFile()
writes content of buffer to file
Definition: csvlog.cpp:263
void writeLogToBuffer(std::string time, std::string msg, LogLevel lvl)
general log generator for buffer
Definition: csvlog.cpp:279
void logTrace(const char *format,...)
general log on trace level
Definition: csvlog.cpp:102
static LogLevel & getCurrentLogLevelConsole()
get current log level for console log
Definition: csvlog.cpp:89
void writeLog(std::string msg, LogLevel lvl)
general log generator method
Definition: csvlog.cpp:207
void logInfo(const char *format,...)
general log on info level
Definition: csvlog.cpp:118
virtual ~CSVLog()
Destroy the CSVLog object.
Definition: csvlog.cpp:184
std::string getCurrentTime()
get time of day as string
Definition: csvlog.cpp:373
void setTimeBetweenWrites(double time_seconds)
defines the max time between the buffer being written to the file in low throughput mode
Definition: csvlog.cpp:74
void setLogLevel(LogLevel lvl)
set the log level for file and console
Definition: csvlog.cpp:33
void setMaxLinesFile(int numLines)
define after how many lines a new file is created
Definition: csvlog.cpp:54
int _maxLinesBuffer
Definition: csvlog.h:420
CSVLog(const CSVLog &)
static CSVLog & getInstance()
Get the singleton object of CSVLog.
Definition: csvlog.cpp:25
void logError(const char *format,...)
general log on error level
Definition: csvlog.cpp:150
void setSeperator(char seperator)
set seperator for log in file
Definition: csvlog.cpp:64
std::string _currentFilename
Definition: csvlog.h:427
void setLowThroughputMode(bool b)
in low throughput mode, the buffer is written to the file after a certain time even if the max buffer...
Definition: csvlog.cpp:69
int _linecountFile
Definition: csvlog.h:419
std::ostringstream os
Definition: csvlog.h:428
int lvlToConsoleColor(LogLevel lvl)
get console color from log level on windows
std::string lvlToString(LogLevel lvl)
get string for log level
Definition: csvlog.cpp:318
bool hasFile()
check whether a file is available
Definition: csvlog.cpp:95
void writeLogToConsole(std::string time, std::string msg, LogLevel lvl)
general log generator for console
Definition: csvlog.cpp:396
void logTraceConsole(const char *format,...)
log on trace level in console
Definition: csvlog.cpp:107
void setLogLevelFile(LogLevel lvl)
set log level only for file
Definition: csvlog.cpp:39
bool _lowThroughputMode
Definition: csvlog.h:424
void logFile(std::string msg, LogLevel lvl)
generate log entry in file
Definition: csvlog.cpp:240
static LogLevel & getCurrentLogLevelFile()
get current log level for file log
Definition: csvlog.cpp:83
void init(std::string filename)
initialize log to file
Definition: csvlog.cpp:190
std::string getCurrentTimeEpoch()
get epoch as string
Definition: csvlog.cpp:387
char _seperator
Definition: csvlog.h:417
void logInfoConsole(const char *format,...)
log on info level in console
Definition: csvlog.cpp:123
void logErrorFile(const char *format,...)
log on error level in file
Definition: csvlog.cpp:160
void setTimestampVersion(TimestampVersion tsv)
set the time stamp version, time of day (0) or epoch (1)
Definition: csvlog.cpp:49
void logWarningFile(const char *format,...)
log on warning level in file
Definition: csvlog.cpp:144
bool _useFile
Definition: csvlog.h:423
CSVLog()
Construct a new CSVLog object.
Definition: csvlog.cpp:168
int _maxLinesFile
Definition: csvlog.h:421
void setMaxLinesBuffer(int numLines)
defines at how many lines in the buffer the buffer is written into a file
Definition: csvlog.cpp:59