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 
168  void init(
169  bool i_logToConsole,
170  const char * i_basePath,
171  bool i_logToFile,
172  bool i_useTimeStamp = false,
173  bool i_usePidStamp = false,
174  bool i_noMsgTime = false,
175  bool i_isLogSql = false
176  ) noexcept override;
177 
179  const string lastLogPath(void) noexcept override;
180 
182  const string stampedLogPath(void) noexcept override;
183 
185  const string getMessage(const char * i_sourceMsg) noexcept override;
186 
188  const list<string> getLanguages(void) noexcept override;
189 
191  const unordered_map<string, string> getLanguageMessages(void) noexcept override;
192 
194  void swapLanguageMessages(const list<string> & i_langLst, unordered_map<string, string> & io_msgMap) noexcept override;
195 
197  void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
198 
200  void logFormatted(const char * i_format, ...) noexcept override;
201 
203  void logErr(const exception & i_ex, const char * i_msg = nullptr) noexcept override;
204 
206  void logSql(const char * i_sql) noexcept override;
207 
208  private:
209  bool isSqlLog; // if true then log sql queries into last log
210  list<string> msgLangLst; // list of languages, in order of user prefrences
211  unordered_map<string, string> msgMap; // language-specific messages
212 
213  // log to file, return false on error
214  bool logToFile(
215  bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
216  ) noexcept override;
217 
218  private:
219  Log(const Log & i_log) = delete;
220  Log & operator=(const Log & i_log) = delete;
221  };
222 
236  class TraceLog : public LogBase, public ITrace
237  {
238  public:
250  bool i_logToConsole = false,
251  const char * i_basePath = "",
252  bool i_logToFile = false,
253  bool i_useTimeStamp = false,
254  bool i_usePidStamp = false,
255  bool i_noMsgTime = false
256  ) : LogBase(i_logToConsole, i_basePath, i_logToFile, i_useTimeStamp, i_usePidStamp, i_noMsgTime)
257  { }
258 
259  ~TraceLog(void) noexcept;
260 
270  void init(
271  bool i_logToConsole,
272  const char * i_basePath,
273  bool i_logToFile,
274  bool i_useTimeStamp = false,
275  bool i_usePidStamp = false,
276  bool i_noMsgTime = false
277  ) noexcept override;
278 
280  void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
281 
283  void logFormatted(const char * i_format, ...) noexcept override;
284 
285  private:
286  ofstream lastSt; // last log output stream
287  ofstream stampedSt; // stamped log output stream
288 
289  // log to file, return false on error
290  bool logToFile(
291  bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
292  ) noexcept override;
293 
294  private:
295  TraceLog(const TraceLog & i_log) = delete;
296  TraceLog & operator=(const TraceLog & i_log) = delete;
297  };
298 }
299 
300 #ifdef _WIN32
301 #pragma warning(pop)
302 #endif // _WIN32
303 
304 #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:236
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:249
log public interface: log to console and into log files
Definition: omLog.h:38
OpenM++ common helper utilities.
OpenM++ common helper utilities.