BCLog.h
1 #ifndef __BCLOG__H
2 #define __BCLOG__H
3 
15 /*
16  * Copyright (C) 2007-2018, the BAT core developer team
17  * All rights reserved.
18  *
19  * For the licensing terms see doc/COPYING.
20  * For documentation see http://mpp.mpg.de/bat
21  */
22 
23 // ---------------------------------------------------------
24 
28 #define BCLOG_INTERNAL_OUT(t, s) \
29  do {\
30  BCLog:: t \
31  (std::string(__PRETTY_FUNCTION__) + ": " + s); \
32  } while (false)
33 
34 #define BCLOG_DEBUG(s) BCLOG_INTERNAL_OUT(OutDebug, s)
35 
36 #define BCLOG_DETAIL(s) BCLOG_INTERNAL_OUT(OutDetail, s)
37 
38 #define BCLOG_ERROR(s) BCLOG_INTERNAL_OUT(OutError, s)
39 
40 #define BCLOG_SUMMARY(s) BCLOG_INTERNAL_OUT(OutSummary, s)
41 
42 #define BCLOG_WARNING(s) BCLOG_INTERNAL_OUT(OutWarning, s)
43 
44 // ---------------------------------------------------------
45 
46 #include <fstream>
47 #include <string>
48 
49 // ---------------------------------------------------------
50 
51 class BCLog
52 {
53 public:
54 
55  // definition of log level
56 
59  enum LogLevel {
66  };
67 
73  BCLog();
74 
83  { return fMinimumLogLevelFile; };
84 
89  { return fMinimumLogLevelScreen; };
90 
94  static bool GetPrefix()
95  { return fPrefix; }
96 
104  static void SetLogLevelFile(BCLog::LogLevel loglevel)
105  { fMinimumLogLevelFile = loglevel; };
106 
110  static void SetLogLevelScreen(BCLog::LogLevel loglevel)
111  { fMinimumLogLevelScreen = loglevel; };
112 
117  static void SetLogLevel(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen)
118  { fMinimumLogLevelFile = loglevelfile; fMinimumLogLevelScreen = loglevelscreen; };
119 
123  static void SetLogLevel(BCLog::LogLevel loglevel)
124  { SetLogLevel(loglevel, loglevel); };
125 
128  static void SetPrefix(bool flag)
129  { fPrefix = flag; }
130 
140  static void OpenLog(const std::string& filename = "log.txt", BCLog::LogLevel loglevelfile = BCLog::debug, BCLog::LogLevel loglevelscreen = BCLog::summary);
141 
144  static bool IsOpen()
145  { return fOutputStream.is_open(); }
146 
149  static void CloseLog()
150  { fOutputStream.close(); }
151 
157  static void Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const std::string& message);
158 
159  static void Out(const std::string& message)
160  { Out(BCLog::fMinimumLogLevelFile, BCLog::fMinimumLogLevelScreen, message); }
161 
162  static void Out(BCLog::LogLevel loglevel, const std::string& message)
163  { Out(loglevel, loglevel, message); };
164 
165  static void OutError(const std::string& message)
166  { Out(error, message); };
167 
168  static void OutWarning(const std::string& message)
169  { Out(warning, message); };
170 
171  static void OutSummary(const std::string& message)
172  { Out(summary, message); };
173 
174  static void OutDetail(const std::string& message)
175  { Out(detail, message); };
176 
177  static void OutDebug(const std::string& message)
178  { Out(debug, message); };
179 
182  static void StartupInfo();
183 
186  static const std::string& GetVersion()
187  { return fVersion; };
188 
191  static std::string ToString(BCLog::LogLevel);
192 
194 private:
195 
198  static std::string fVersion;
199 
202  static BCLog::LogLevel fMinimumLogLevelFile;
203 
206  static BCLog::LogLevel fMinimumLogLevelScreen;
207 
210  static std::ofstream fOutputStream;
211 
214  static bool fFirstOutputDone;
215 
218  static bool fPrefix;
219 
220 };
221 
222 // ---------------------------------------------------------
223 
224 #endif
static bool GetPrefix()
Returns true if the loglevel is prefixed to every message.
Definition: BCLog.h:94
static BCLog::LogLevel GetLogLevelFile()
Returns the minimum log level for file output.
Definition: BCLog.h:82
Print everything, including debug info.
Definition: BCLog.h:60
static void CloseLog()
Closes the log file.
Definition: BCLog.h:149
static void SetLogLevel(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen)
Sets the minimum log level for file and screen output.
Definition: BCLog.h:117
static void SetLogLevel(BCLog::LogLevel loglevel)
Sets the minimum log level for file and screen output.
Definition: BCLog.h:123
Print only results summary, warnings, and errors.
Definition: BCLog.h:62
static void Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const std::string &message)
Writes string to the file and screen log if the log level is equal or greater than the minimum...
Definition: BCLog.cxx:70
LogLevel
Enumerator for the amount of details to put into the log file.
Definition: BCLog.h:59
Print all details of operation.
Definition: BCLog.h:61
static void SetLogLevelFile(BCLog::LogLevel loglevel)
Sets the minimum log level for file output.
Definition: BCLog.h:104
BCLog()
Constructor.
Definition: BCLog.cxx:36
static void StartupInfo()
Writes startup information onto screen and into a logfile.
Definition: BCLog.cxx:90
static bool IsOpen()
Definition: BCLog.h:144
Print nothing.
Definition: BCLog.h:65
static void SetLogLevelScreen(BCLog::LogLevel loglevel)
Sets the minimum log level for screen output.
Definition: BCLog.h:110
static const std::string & GetVersion()
Definition: BCLog.h:186
static void OpenLog(const std::string &filename="log.txt", BCLog::LogLevel loglevelfile=BCLog::debug, BCLog::LogLevel loglevelscreen=BCLog::summary)
Opens log file and sets minimum log levels for file and screen output.
Definition: BCLog.cxx:45
Print only warnings and errors.
Definition: BCLog.h:63
Print only errors.
Definition: BCLog.h:64
static void SetPrefix(bool flag)
Toggle if the loglevel is prefixed to every message.
Definition: BCLog.h:128
static std::string ToString(BCLog::LogLevel)
Converts a log level to a string.
Definition: BCLog.cxx:119
static BCLog::LogLevel GetLogLevelScreen()
Returns the minimum log level for screen output.
Definition: BCLog.h:88
A class for managing log messages.
Definition: BCLog.h:51