21 #define DRWN_LOG(L, M) \
22 if ((L) > drwnLogger::getLogLevel()) { } \
23 else { std::stringstream __s; if (L == DRWN_LL_FATAL) { __s << "(" << __FILE__ << ", " << __LINE__ << ") "; } \
24 __s << M; drwnLogger::logMessage(L, __s.str()); }
26 #define DRWN_LOG_ONCE(L, M) \
27 static bool _drwnLoggedMessage ## __LINE__ = false; \
28 if (((L) > drwnLogger::getLogLevel()) || _drwnLoggedMessage ## __LINE__) { } \
29 else { std::stringstream __s; if (L == DRWN_LL_FATAL) { __s << "(" << __FILE__ << ", " << __LINE__ << ") "; } \
30 __s << M; drwnLogger::logMessage(L, __s.str()); _drwnLoggedMessage ## __LINE__ = true;}
32 #define DRWN_LOG_FATAL(M) DRWN_LOG(DRWN_LL_FATAL, M)
33 #define DRWN_LOG_ERROR(M) DRWN_LOG(DRWN_LL_ERROR, M)
34 #define DRWN_LOG_WARNING(M) DRWN_LOG(DRWN_LL_WARNING, M)
35 #define DRWN_LOG_MESSAGE(M) DRWN_LOG(DRWN_LL_MESSAGE, M)
36 #define DRWN_LOG_STATUS(M) DRWN_LOG(DRWN_LL_STATUS, M)
37 #define DRWN_LOG_VERBOSE(M) DRWN_LOG(DRWN_LL_VERBOSE, M)
38 #define DRWN_LOG_METRICS(M) DRWN_LOG(DRWN_LL_METRICS, M)
39 #define DRWN_LOG_DEBUG(M) DRWN_LOG(DRWN_LL_DEBUG, M)
41 #define DRWN_LOG_ERROR_ONCE(M) DRWN_LOG_ONCE(DRWN_LL_ERROR, M)
42 #define DRWN_LOG_WARNING_ONCE(M) DRWN_LOG_ONCE(DRWN_LL_WARNING, M)
43 #define DRWN_LOG_MESSAGE_ONCE(M) DRWN_LOG_ONCE(DRWN_LL_MESSAGE, M)
44 #define DRWN_LOG_VERBOSE_ONCE(M) DRWN_LOG_ONCE(DRWN_LL_VERBOSE, M)
45 #define DRWN_LOG_METRICS_ONCE(M) DRWN_LOG_ONCE(DRWN_LL_METRICS, M)
46 #define DRWN_LOG_DEBUG_ONCE(M) DRWN_LOG_ONCE(DRWN_LL_DEBUG, M)
48 #define DRWN_ASSERT(C) \
49 if (!(C)) { DRWN_LOG(DRWN_LL_FATAL, #C); }
51 #define DRWN_ASSERT_MSG(C, M) \
52 if (!(C)) { DRWN_LOG(DRWN_LL_FATAL, #C << ": " << M); }
54 #define DRWN_TODO DRWN_LOG_FATAL("not implemented yet");
56 #define DRWN_DEPRECATED(C) \
57 {DRWN_LOG_WARNING_ONCE("deprecated code (" << __FILE__ << ", " << __LINE__ << ")"); C;}
59 #define DRWN_START_PROGRESS(S, M) drwnLogger::initProgress(S, M)
60 #define DRWN_INC_PROGRESS if (!drwnLogger::incrementProgress()) break;
61 #define DRWN_END_PROGRESS drwnLogger::initProgress()
62 #define DRWN_SET_PROGRESS(P) drwnLogger::updateProgress(P)
64 #define DRWN_PROGRESS_SPINNER(M) \
65 static int _drwnLoggerSpinner ## __LINE__ = 0; \
66 if (drwnLogger::getLogLevel() >= DRWN_LL_STATUS) { \
67 const char TABLE[] = "\\|/-"; \
68 std::stringstream __s; __s << M; __s << ' '; \
69 __s << TABLE[_drwnLoggerSpinner ## __LINE__]; \
70 drwnLogger::logMessage(DRWN_LL_STATUS, __s.str()); \
71 _drwnLoggerSpinner ## __LINE__ = (_drwnLoggerSpinner ## __LINE__ + 1) % 4; }
76 typedef enum _drwnLogLevel {
97 static void (*showFatalCallback)(
const char *message);
99 static void (*showErrorCallback)(
const char *message);
101 static void (*showWarningCallback)(
const char *message);
103 static void (*showStatusCallback)(
const char *message);
105 static void (*showMessageCallback)(
const char *message);
108 static void (*showProgressCallback)(
const char *status,
double p);
111 static ofstream _log;
112 static drwnLogLevel _logLevel;
113 static drwnLogLevel _lastMessageLevel;
114 static string _cmdLineString;
116 static string _progressStatus;
117 static int _lastProgress;
118 static int _progressLimit;
119 static bool _bRunning;
126 static void initialize(
const char *filename,
127 bool bOverwrite =
false);
129 static void initialize(
const char *filename,
130 bool bOverwrite, drwnLogLevel level);
138 const drwnLogLevel lastLevel = _logLevel;
139 if (_logLevel < level) _logLevel = level;
148 return (_logLevel >= level);
152 static void cacheCommandLine(
int argc,
char **argv);
155 static void logMessage(drwnLogLevel level,
const string& msg);
158 static void setRunning(
bool bRunning) {
159 _bRunning = bRunning;
161 static bool isRunning() {
164 static void initProgress(
const char *status = NULL,
int maxProgress = 100) {
165 _progressStatus = status == NULL ? string(
"") : string(status);
166 _lastProgress = 0; _progressLimit = maxProgress; updateProgress(0.0);
168 static void setProgressStatus(
const char *status) {
169 if (status == NULL) {
170 _progressStatus.clear();
172 _progressStatus = string(status);
174 if (showProgressCallback != NULL) {
175 showProgressCallback(_progressStatus.c_str(),
176 (double)_lastProgress / (
double)_progressLimit);
179 static bool incrementProgress() {
180 if (_lastProgress < _progressLimit) {
182 updateProgress((
double)_lastProgress / (
double)_progressLimit);
186 static bool updateProgress(
double p) {
187 if (showProgressCallback != NULL) {
188 showProgressCallback(_progressStatus.c_str(), p);
static drwnLogLevel setAtLeastLogLevel(drwnLogLevel level)
set the current verbosity level unless already at a lower level and return previous log level ...
Definition: drwnLogger.h:137
static bool checkLogLevel(drwnLogLevel level)
check whether current verbosity level is above given level
Definition: drwnLogger.h:147
static drwnLogLevel getLogLevel()
get the current verbosity level
Definition: drwnLogger.h:143
static void setLogLevel(drwnLogLevel level)
set the current verbosity level
Definition: drwnLogger.h:132
Message and error logging. This class is not thread-safe in the interest of not having to flush the l...
Definition: drwnLogger.h:93