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>
19using 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
31namespace 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
64 const bool isEnabled(void) noexcept override { return isConsoleEnabled || isLastEnabled || isStampedEnabled; }
65
75 void init(
76 bool i_logToConsole,
77 const char * i_basePath,
78 bool i_logToFile,
79 bool i_useTimeStamp = false,
80 bool i_usePidStamp = false,
81 bool i_noMsgTime = false
82 ) noexcept;
83
85 void setRank(int i_rank, int i_worldSize) noexcept override;
86
87 protected:
88 recursive_mutex theMutex; // mutex to lock for log operations
89 bool isConsoleEnabled; // if true then log to console
90 bool isLastEnabled; // if true then last log enabled
91 bool isLastCreated; // if true then last log file created
92 bool isStampedEnabled; // if true then last log enabled
93 bool isStampedCreated; // if true then last log file created
94 string tsPart; // log file name timestamp part: 2012_08_17_16_04_59_148
95 string pidPart; // log file name pid part: 1234
96 string lastPath; // last log file path: /var/log/openm.log
97 string stampedPath; // stamped log file path: /var/log/openm.2012_08_17_16_04_59_148.1234.log
98 bool isNoMsgTime; // if true then not prefix messages with date-time
99 string rankPrefix; // process rank log prefix
100 bool isMsgRank; // if true then prefix messages with process rank
101
102 static const size_t msgBufferSize = 32768;
103 char msgBuffer[msgBufferSize + 1]; // buffer to format message
104
105 protected:
106
108 void doLogMsg(const char * i_msg, const char * i_extra) noexcept;
109
110 // create log file or truncate existing, return false on error
111 bool logFileCreate(const string & i_path) noexcept;
112
113 // log to console
114 virtual bool logToConsole(
115 const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
116 ) noexcept = 0;
117
118 // log to file, return false on error
119 virtual bool logToFile(
120 bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
121 ) noexcept = 0;
122
123 // write date-time and message to output stream, return false on error
124 void writeToLog(
125 ostream & i_ost, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
126 );
127
128 private:
129 LogBase(const LogBase & i_log) = delete;
130 LogBase & operator=(const LogBase & i_log) = delete;
131 };
132
144 class Log : public LogBase, public ILog
145 {
146 public:
156 Log(
157 bool i_logToConsole = false,
158 const char * i_basePath = "openm.log",
159 bool i_logToFile = false,
160 bool i_useTimeStamp = false,
161 bool i_usePidStamp = false
162 );
163 ~Log(void) noexcept;
164
176 void init(
177 bool i_logToConsole,
178 const char * i_basePath,
179 bool i_logToFile,
180 bool i_useTimeStamp = false,
181 bool i_usePidStamp = false,
182 bool i_noMsgTime = false,
183 bool i_isLogSql = false
184 ) noexcept override;
185
187 const string lastLogPath(void) noexcept override;
188
190 const string stampedLogPath(void) noexcept override;
191
193 const string getMessage(const char * i_sourceMsg) noexcept override;
194
196 const list<string> getLanguages(void) noexcept override;
197
199 const unordered_map<string, string> getLanguageMessages(void) noexcept override;
200
202 void swapLanguageMessages(const list<string> & i_langLst, unordered_map<string, string> & io_msgMap) noexcept override;
203
205 void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
206
208 void logFormatted(const char * i_format, ...) noexcept override;
209
211 void logErr(const exception & i_ex, const char * i_msg = nullptr) noexcept override;
212
214 void logSql(const char * i_sql) noexcept override;
215
216 private:
217 bool isSqlLog; // if true then log sql queries into last log
218 list<string> msgLangLst; // list of languages, in order of user prefrences
219 unordered_map<string, string> msgMap; // language-specific messages
220
221 // log to console and flush each line, return false on error
222 bool logToConsole(
223 const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
224 ) noexcept override;
225
226 // log to file, return false on error
227 bool logToFile(
228 bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
229 ) noexcept override;
230
231 private:
232 Log(const Log & i_log) = delete;
233 Log & operator=(const Log & i_log) = delete;
234 };
235
249 class TraceLog : public LogBase, public ITrace
250 {
251 public:
263 bool i_logToConsole = false,
264 const char * i_basePath = "",
265 bool i_logToFile = false,
266 bool i_useTimeStamp = false,
267 bool i_usePidStamp = false,
268 bool i_noMsgTime = false
269 ) : LogBase(i_logToConsole, i_basePath, i_logToFile, i_useTimeStamp, i_usePidStamp, i_noMsgTime)
270 { }
271
272 ~TraceLog(void) noexcept;
273
283 void init(
284 bool i_logToConsole,
285 const char * i_basePath,
286 bool i_logToFile,
287 bool i_useTimeStamp = false,
288 bool i_usePidStamp = false,
289 bool i_noMsgTime = false
290 ) noexcept override;
291
293 void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
294
296 void logFormatted(const char * i_format, ...) noexcept override;
297
298 private:
299 ofstream lastSt; // last log output stream
300 ofstream stampedSt; // stamped log output stream
301
302 // log to console, return false on error
303 bool logToConsole(
304 const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
305 ) noexcept override;
306
307 // log to file, return false on error
308 bool logToFile(
309 bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
310 ) noexcept override;
311
312 private:
313 TraceLog(const TraceLog & i_log) = delete;
314 TraceLog & operator=(const TraceLog & i_log) = delete;
315 };
316}
317
318#ifdef _WIN32
319#pragma warning(pop)
320#endif // _WIN32
321
322#endif // OM_LOG_H
Log base class: log to console and into log files.
Definition: log.h:35
void doLogMsg(const char *i_msg, const char *i_extra) noexcept
implement log message: log to console and log files
Definition: log.cpp:170
void init(bool i_logToConsole, const char *i_basePath, bool i_logToFile, bool i_useTimeStamp=false, bool i_usePidStamp=false, bool i_noMsgTime=false) noexcept
re-initialize log file name(s) and other log settings.
Definition: log.cpp:92
~LogBase(void) noexcept
cleanup log resources
Definition: log.cpp:74
const bool isEnabled(void) noexcept override
return true if log to console or to file enabled.
Definition: log.h:64
LogBase(bool i_logToConsole, const char *i_basePath, bool i_logToFile, bool i_useTimeStamp, bool i_usePidStamp, bool i_noMsgTime)
create log instance.
Definition: log.cpp:51
const string timeStamp(void) noexcept override
return timestamp part of log file name: 2012_08_17_16_04_59_148.
Definition: log.cpp:158
void setRank(int i_rank, int i_worldSize) noexcept override
use process rank as log message prefix
Definition: log.cpp:134
log implementation class: log to console and into log files.
Definition: log.h:145
trace log implementation class: model event log to console and into log files.
Definition: log.h:250
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:262
OpenM++ common helper utilities.
openM++ namespace
Definition: log.h:32
OpenM++: public interface for errors and exceptions.
OpenM++ common helper utilities.
OpenM++: public interface for log and trace support.
base for log public interface
Definition: omLog.h:19
virtual void logMsg(const char *i_msg, const char *i_extra=NULL) noexcept=0
log message
virtual void logFormatted(const char *i_format,...) noexcept=0
log message formatted with vsnprintf()
log public interface: log to console and into log files
Definition: omLog.h:43
trace log public interface: model event log to console and into log files
Definition: omLog.h:92