Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnLogger.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnLogger.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <iostream>
16 #include <fstream>
17 #include <sstream>
18 
19 using namespace std;
20 
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()); }
25 
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;}
31 
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)
40 
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)
47 
48 #define DRWN_ASSERT(C) \
49  if (!(C)) { DRWN_LOG(DRWN_LL_FATAL, #C); }
50 
51 #define DRWN_ASSERT_MSG(C, M) \
52  if (!(C)) { DRWN_LOG(DRWN_LL_FATAL, #C << ": " << M); }
53 
54 #define DRWN_TODO DRWN_LOG_FATAL("not implemented yet");
55 
56 #define DRWN_DEPRECATED(C) \
57  {DRWN_LOG_WARNING_ONCE("deprecated code (" << __FILE__ << ", " << __LINE__ << ")"); C;}
58 
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)
63 
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; }
72 
73 // drwnLogLevel ---------------------------------------------------------------
75 
76 typedef enum _drwnLogLevel {
77  DRWN_LL_FATAL = 0,
78  DRWN_LL_ERROR,
79  DRWN_LL_WARNING,
80  DRWN_LL_MESSAGE,
81  DRWN_LL_STATUS,
82  DRWN_LL_VERBOSE,
83  DRWN_LL_METRICS,
84  DRWN_LL_DEBUG
85 } drwnLogLevel;
86 
87 // drwnLogger -----------------------------------------------------------------
94 {
95  public:
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);
106 
108  static void (*showProgressCallback)(const char *status, double p);
109 
110  private:
111  static ofstream _log;
112  static drwnLogLevel _logLevel;
113  static drwnLogLevel _lastMessageLevel;
114  static string _cmdLineString;
115 
116  static string _progressStatus;
117  static int _lastProgress;
118  static int _progressLimit;
119  static bool _bRunning;
120 
121  public:
122  drwnLogger();
123  ~drwnLogger();
124 
126  static void initialize(const char *filename,
127  bool bOverwrite = false);
129  static void initialize(const char *filename,
130  bool bOverwrite, drwnLogLevel level);
132  static inline void setLogLevel(drwnLogLevel level) {
133  _logLevel = level;
134  }
137  static inline drwnLogLevel setAtLeastLogLevel(drwnLogLevel level) {
138  const drwnLogLevel lastLevel = _logLevel;
139  if (_logLevel < level) _logLevel = level;
140  return lastLevel;
141  }
143  static inline drwnLogLevel getLogLevel() {
144  return _logLevel;
145  }
147  static inline bool checkLogLevel(drwnLogLevel level) {
148  return (_logLevel >= level);
149  }
150 
152  static void cacheCommandLine(int argc, char **argv);
153 
155  static void logMessage(drwnLogLevel level, const string& msg);
156 
157  // progress feedback
158  static void setRunning(bool bRunning) {
159  _bRunning = bRunning;
160  }
161  static bool isRunning() {
162  return _bRunning;
163  }
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);
167  }
168  static void setProgressStatus(const char *status) {
169  if (status == NULL) {
170  _progressStatus.clear();
171  } else {
172  _progressStatus = string(status);
173  }
174  if (showProgressCallback != NULL) {
175  showProgressCallback(_progressStatus.c_str(),
176  (double)_lastProgress / (double)_progressLimit);
177  }
178  }
179  static bool incrementProgress() {
180  if (_lastProgress < _progressLimit) {
181  _lastProgress += 1;
182  updateProgress((double)_lastProgress / (double)_progressLimit);
183  }
184  return _bRunning;
185  }
186  static bool updateProgress(double p) {
187  if (showProgressCallback != NULL) {
188  showProgressCallback(_progressStatus.c_str(), p);
189  }
190  return _bRunning;
191  }
192 };
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