17 #ifndef _LOG_H_X_INCLUDED_ 18 #define _LOG_H_X_INCLUDED_ 25 #ifndef LOGGER_THREADSAFE 26 #define LOGGER_THREADSAFE 1 36 #ifndef LOGGER_STATICVERBOSITY 37 #define LOGGER_STATICVERBOSITY 5 40 #define LOGGER_DATESIZE 100 55 bool reopen(
const std::string& fn);
61 return m_tocerr ? std::cerr : m_stream;
67 enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
68 LLDEB0=5, LLDEB1=6, LLDEB2=7};
97 bool loggingdate()
const {
110 #if LOGGER_THREADSAFE 111 std::recursive_mutex& getmutex() {
117 bool m_tocerr{
false};
118 bool m_logdate{
false};
119 int m_loglevel{LLERR};
120 std::string m_datefmt{
"%Y%m%d-%H%M%S"};
122 std::ofstream m_stream;
123 #if LOGGER_THREADSAFE 124 std::recursive_mutex m_mutex;
126 char m_datebuf[LOGGER_DATESIZE];
127 Logger(
const std::string& fn);
132 #define LOGGER_PRT (Logger::getTheLog()->getstream()) 134 #if LOGGER_THREADSAFE 135 #define LOGGER_LOCK \ 136 std::unique_lock<std::recursive_mutex> lock(Logger::getTheLog()->getmutex()) 141 #ifndef LOGGER_LOCAL_LOGINC 142 #define LOGGER_LOCAL_LOGINC 0 145 #define LOGGER_LEVEL (Logger::getTheLog()->getloglevel() + \ 148 #define LOGGER_DATE (Logger::getTheLog()->loggingdate() ? \ 149 Logger::getTheLog()->datestring() : "") 151 #define LOGGER_DOLOG(L,X) LOGGER_PRT << LOGGER_DATE << ":" << L << ":" << \ 152 __FILE__ << ":" << __LINE__ << "::" << X \ 156 #if LOGGER_STATICVERBOSITY >= 7 157 #define LOGDEB2(X) { \ 158 if (LOGGER_LEVEL >= Logger::LLDEB2) { \ 160 LOGGER_DOLOG(Logger::LLDEB2, X); \ 167 #if LOGGER_STATICVERBOSITY >= 6 168 #define LOGDEB1(X) { \ 169 if (LOGGER_LEVEL >= Logger::LLDEB1) { \ 171 LOGGER_DOLOG(Logger::LLDEB1, X); \ 178 #if LOGGER_STATICVERBOSITY >= 5 179 #define LOGDEB0(X) { \ 180 if (LOGGER_LEVEL >= Logger::LLDEB0) { \ 182 LOGGER_DOLOG(Logger::LLDEB0, X); \ 189 #if LOGGER_STATICVERBOSITY >= 4 190 #define LOGDEB(X) { \ 191 if (LOGGER_LEVEL >= Logger::LLDEB) { \ 193 LOGGER_DOLOG(Logger::LLDEB, X); \ 200 #if LOGGER_STATICVERBOSITY >= 3 205 #define LOGINF(X) { \ 206 if (LOGGER_LEVEL >= Logger::LLINF) { \ 208 LOGGER_DOLOG(Logger::LLINF, X); \ 214 #define LOGINFO LOGINF 216 #if LOGGER_STATICVERBOSITY >= 2 217 #define LOGERR(X) { \ 218 if (LOGGER_LEVEL >= Logger::LLERR) { \ 220 LOGGER_DOLOG(Logger::LLERR, X); \ 227 #if LOGGER_STATICVERBOSITY >= 1 228 #define LOGFAT(X) { \ 229 if (LOGGER_LEVEL >= Logger::LLFAT) { \ 231 LOGGER_DOLOG(Logger::LLFAT, X); \ 237 #define LOGFATAL LOGFAT 239 #if defined(sun) || defined(_WIN32) 240 #define LOGSYSERR(who, what, arg) { \ 241 LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \ 242 ": " << strerror(errno) << std::endl); \ 244 #else // not WINDOWS or sun 246 inline char *_log_check_strerror_r(
int,
char *errbuf) {
return errbuf;}
247 inline char *_log_check_strerror_r(
char *cp,
char *){
return cp;}
249 #define LOGSYSERR(who, what, arg) { \ 250 char buf[200]; buf[0] = 0; \ 251 LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \ 252 ": " << _log_check_strerror_r( \ 253 strerror_r(errno, buf, 200), buf) << std::endl); \ 256 #endif // not windows This is a singleton class.
Definition: log.h:45
void setdateformat(const std::string fmt)
Set the date format, as an strftime() format string.
Definition: log.h:103
LogLevel
Log level values.
Definition: log.h:67
int getloglevel() const
Retrieve the current log level.
Definition: log.h:80
std::ostream & getstream()
Retrieve the output stream in case you need to write directly to it.
Definition: log.h:60
void logthedate(bool onoff)
turn date logging on or off (default is off)
Definition: log.h:93
const std::string & getlogfilename() const
Retrieve current log file name.
Definition: log.h:84
bool reopen(const std::string &fn)
Close and reopen the output file.
Definition: log.cpp:36
void setLogLevel(LogLevel level)
Set the log dynamic verbosity level.
Definition: log.h:71
static Logger * getTheLog(const std::string &fn=std::string())
Initialize logging to file name.
Definition: log.cpp:75
bool logisstderr() const
Logging to stderr ?
Definition: log.h:88
void setloglevel(LogLevel level)
Set the log dynamic verbosity level.
Definition: log.h:75
const char * datestring()
Call with log locked.
Definition: log.cpp:62