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 static void cleanup();
59 bool reopen(
const std::string& fn);
65 return m_tocerr ? std::cerr : m_stream;
71 enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
72 LLDEB0=5, LLDEB1=6, LLDEB2=7};
101 bool loggingdate()
const {
115 std::recursive_mutex& getmutex() {
121 bool m_tocerr{
false};
122 bool m_logdate{
false};
123 int m_loglevel{LLERR};
124 std::string m_datefmt{
"%Y%m%d-%H%M%S"};
126 std::ofstream m_stream;
128 std::recursive_mutex m_mutex;
130 char m_datebuf[LOGGER_DATESIZE];
131 Logger(
const std::string& fn);
135#define LOGGER_PRT (Logger::getTheLog()->getstream())
139 std::unique_lock<std::recursive_mutex> lock(Logger::getTheLog()->getmutex())
144#ifndef LOGGER_LOCAL_LOGINC
145#define LOGGER_LOCAL_LOGINC 0
148#define LOGGER_LEVEL (Logger::getTheLog()->getloglevel() + \
151#define LOGGER_DATE (Logger::getTheLog()->loggingdate() ? \
152 Logger::getTheLog()->datestring() : "")
154#define LOGGER_DOLOG(L,X) LOGGER_PRT << LOGGER_DATE << ":" << L << ":" << \
155 __FILE__ << ":" << __LINE__ << "::" << X \
159#if LOGGER_STATICVERBOSITY >= 7
160#define LOGDEB2(X) { \
161 if (LOGGER_LEVEL >= Logger::LLDEB2) { \
163 LOGGER_DOLOG(Logger::LLDEB2, X); \
170#if LOGGER_STATICVERBOSITY >= 6
171#define LOGDEB1(X) { \
172 if (LOGGER_LEVEL >= Logger::LLDEB1) { \
174 LOGGER_DOLOG(Logger::LLDEB1, X); \
181#if LOGGER_STATICVERBOSITY >= 5
182#define LOGDEB0(X) { \
183 if (LOGGER_LEVEL >= Logger::LLDEB0) { \
185 LOGGER_DOLOG(Logger::LLDEB0, X); \
192#if LOGGER_STATICVERBOSITY >= 4
194 if (LOGGER_LEVEL >= Logger::LLDEB) { \
196 LOGGER_DOLOG(Logger::LLDEB, X); \
203#if LOGGER_STATICVERBOSITY >= 3
209 if (LOGGER_LEVEL >= Logger::LLINF) { \
211 LOGGER_DOLOG(Logger::LLINF, X); \
217#define LOGINFO LOGINF
219#if LOGGER_STATICVERBOSITY >= 2
221 if (LOGGER_LEVEL >= Logger::LLERR) { \
223 LOGGER_DOLOG(Logger::LLERR, X); \
230#if LOGGER_STATICVERBOSITY >= 1
232 if (LOGGER_LEVEL >= Logger::LLFAT) { \
234 LOGGER_DOLOG(Logger::LLFAT, X); \
240#define LOGFATAL LOGFAT
242#if defined(sun) || defined(_WIN32)
243#define LOGSYSERR(who, what, arg) { \
244 LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
245 ": " << strerror(errno) << '\n'); \
249inline char *_log_check_strerror_r(
int,
char *errbuf) {
return errbuf;}
250inline char *_log_check_strerror_r(
char *cp,
char *){
return cp;}
252#define LOGSYSERR(who, what, arg) { \
253 char buf[200]; buf[0] = 0; \
254 LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << ": " << \
255 _log_check_strerror_r(strerror_r(errno, buf, 200), buf) << '\n'); \
This is a singleton class.
Definition log.h:45
void setLogLevel(LogLevel level)
Set the log dynamic verbosity level.
Definition log.h:75
const char * datestring()
Call with log locked.
Definition log.cpp:67
void setloglevel(LogLevel level)
Set the log dynamic verbosity level.
Definition log.h:79
std::ostream & getstream()
Retrieve the output stream in case you need to write directly to it.
Definition log.h:64
static Logger * getTheLog(const std::string &fn=std::string())
Initialize logging to file name.
Definition log.cpp:80
int getloglevel() const
Retrieve the current log level.
Definition log.h:84
bool logisstderr() const
Logging to stderr ?
Definition log.h:92
bool reopen(const std::string &fn)
Close and reopen the output file.
Definition log.cpp:41
LogLevel
Log level values.
Definition log.h:71
void logthedate(bool onoff)
turn date logging on or off (default is off)
Definition log.h:97
void setdateformat(const std::string fmt)
Set the date format, as an strftime() format string.
Definition log.h:107
const std::string & getlogfilename() const
Retrieve current log file name.
Definition log.h:88