OpenM++ runtime library (libopenm)
log.h
Go to the documentation of this file.
1 
5 // Copyright (c) 2013-2015 OpenM++
6 // This code is licensed under the MIT license (see LICENSE.txt for details)
7 
8 #ifndef OM_LOG_H
9 #define OM_LOG_H
10 
11 #include <chrono>
12 #include <mutex>
13 #include <thread>
14 #include <iomanip>
15 #include <fstream>
16 #include <sstream>
17 #include <iostream>
18 #include <cstdarg>
19 using namespace std;
20 
21 #include "libopenm/omLog.h"
22 #include "libopenm/omError.h"
24 #include "helper.h"
25 
26 #ifdef _WIN32
27 #pragma warning(push)
28 #pragma warning(disable : 4250)
29 #endif // _WIN32
30 
31 namespace openm
32 {
34  class LogBase : public virtual ILogBase
35  {
36  public:
47  LogBase(
48  bool i_logToConsole,
49  const char * i_basePath,
50  bool i_logToFile,
51  bool i_useTimeStamp,
52  bool i_usePidStamp,
53  bool i_noMsgTime
54  );
55  ~LogBase(void) noexcept;
56 
61  const string timeStamp(void) noexcept override;
62 
72  void init(
73  bool i_logToConsole,
74  const char * i_basePath,
75  bool i_logToFile,
76  bool i_useTimeStamp = false,
77  bool i_usePidStamp = false,
78  bool i_noMsgTime = false
79  ) noexcept;
80 
81  protected:
82  recursive_mutex theMutex; // mutex to lock for log operations
83  bool isConsoleEnabled; // if true then log to console
84  bool isLastEnabled; // if true then last log enabled
85  bool isLastCreated; // if true then last log file created
86  bool isStampedEnabled; // if true then last log enabled
87  bool isStampedCreated; // if true then last log file created
88  string tsPart; // log file name timestamp part: 2012_08_17_16_04_59_148
89  string pidPart; // log file name pid part: 1234
90  string lastPath; // last log file path: /var/log/openm.log
91  string stampedPath; // stamped log file path: /var/log/openm.2012_08_17_16_04_59_148.1234.log
92  bool isNoMsgTime; // if true then not prefix messages with date-time
93 
94  static const size_t msgBufferSize = 32768;
95  char msgBuffer[msgBufferSize + 1]; // buffer to format message
96 
97  protected:
98 
100  void doLogMsg(const char * i_msg, const char * i_extra) noexcept;
101 
102  // create log file or truncate existing, return false on error
103  bool logFileCreate(const string & i_path) noexcept;
104 
105  // log to console
106  bool logToConsole(
107  const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
108  ) noexcept;
109 
110  // log to file, return false on error
111  virtual bool logToFile(
112  bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
113  ) noexcept = 0;
114 
115  // write date-time and message to output stream, return false on error
116  void writeToLog(
117  ostream & i_ost, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
118  );
119 
120  private:
121  LogBase(const LogBase & i_log) = delete;
122  LogBase & operator=(const LogBase & i_log) = delete;
123  };
124 
136  class Log : public LogBase, public ILog
137  {
138  public:
148  Log(
149  bool i_logToConsole = false,
150  const char * i_basePath = "openm.log",
151  bool i_logToFile = false,
152  bool i_useTimeStamp = false,
153  bool i_usePidStamp = false
154  );
155  ~Log(void) noexcept;
156 
169  void init(
170  bool i_logToConsole,
171  const char * i_basePath,
172  bool i_logToFile,
173  bool i_useTimeStamp = false,
174  bool i_usePidStamp = false,
175  bool i_noMsgTime = false,
176  bool i_isLogSql = false
177  ) noexcept;
178 
180  const string lastLogPath(void) noexcept override;
181 
183  const string stampedLogPath(void) noexcept override;
184 
186  const string getMessage(const char * i_sourceMsg) noexcept override;
187 
189  const list<string> getLanguages(void) noexcept override;
190 
192  const unordered_map<string, string> getLanguageMessages(void) noexcept override;
193 
195  void swapLanguageMessages(const list<string> & i_langLst, unordered_map<string, string> & io_msgMap) noexcept override;
196 
198  void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
199 
201  void logFormatted(const char * i_format, ...) noexcept override;
202 
204  void logErr(const exception & i_ex, const char * i_msg = nullptr) noexcept;
205 
207  void logSql(const char * i_sql) noexcept;
208 
209  private:
210  bool isSqlLog; // if true then log sql queries into last log
211  list<string> msgLangLst; // list of languages, in order of user prefrences
212  unordered_map<string, string> msgMap; // language-specific messages
213 
214  // log to file, return false on error
215  bool logToFile(
216  bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
217  ) noexcept;
218 
219  private:
220  Log(const Log & i_log) = delete;
221  Log & operator=(const Log & i_log) = delete;
222  };
223 
237  class TraceLog : public LogBase, public ITrace
238  {
239  public:
251  bool i_logToConsole = false,
252  const char * i_basePath = "",
253  bool i_logToFile = false,
254  bool i_useTimeStamp = false,
255  bool i_usePidStamp = false,
256  bool i_noMsgTime = false
257  ) : LogBase(i_logToConsole, i_basePath, i_logToFile, i_useTimeStamp, i_usePidStamp, i_noMsgTime)
258  { }
259 
260  ~TraceLog(void) noexcept;
261 
271  void init(
272  bool i_logToConsole,
273  const char * i_basePath,
274  bool i_logToFile,
275  bool i_useTimeStamp = false,
276  bool i_usePidStamp = false,
277  bool i_noMsgTime = false
278  ) noexcept;
279 
281  void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
282 
284  void logFormatted(const char * i_format, ...) noexcept override;
285 
286  private:
287  ofstream lastSt; // last log output stream
288  ofstream stampedSt; // stamped log output stream
289 
290  // log to file, return false on error
291  bool logToFile(
292  bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
293  ) noexcept;
294 
295  private:
296  TraceLog(const TraceLog & i_log) = delete;
297  TraceLog & operator=(const TraceLog & i_log) = delete;
298  };
299 }
300 
301 #ifdef _WIN32
302 #pragma warning(pop)
303 #endif // _WIN32
304 
305 #endif // OM_LOG_H
Log base class: log to console and into log files.
Definition: log.h:34
trace log implementation class: model event log to console and into log files.
Definition: log.h:237
OpenM++: public interface for log and trace support.
openM++ namespace
Definition: argReader.cpp:16
base for log public interface
Definition: omLog.h:20
log implementation class: log to console and into log files.
Definition: log.h:136
OpenM++: public interface for errors and exceptions.
trace log public interface: model event log to console and into log files
Definition: omLog.h:87
TraceLog(bool i_logToConsole=false, const char *i_basePath="", bool i_logToFile=false, bool i_useTimeStamp=false, bool i_usePidStamp=false, bool i_noMsgTime=false)
create log instance.
Definition: log.h:250
log public interface: log to console and into log files
Definition: omLog.h:38
OpenM++ common helper utilities.
OpenM++ common helper utilities.