OpenM++ runtime library (libopenm)
Loading...
Searching...
No Matches
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;
65
67 const bool isCreateError(void) noexcept override;
68
78 void init(
79 bool i_logToConsole,
80 const char * i_basePath,
81 bool i_logToFile,
82 bool i_useTimeStamp = false,
83 bool i_usePidStamp = false,
84 bool i_noMsgTime = false
85 ) noexcept;
86
88 void setRank(int i_rank, int i_worldSize) noexcept override;
89
90 protected:
91 recursive_mutex theMutex; // mutex to lock for log operations
92 bool isConsoleEnabled; // if true then log to console
93 bool isLastEnabled; // if true then last log enabled
94 bool isLastCreated; // if true then last log file created
95 bool isErrorLastCreate; // if true then last log file create failed
96 bool isStampedEnabled; // if true then stamped log enabled
97 bool isStampedCreated; // if true then stamped log file created
98 bool isErrorStampedCreate; // if true then stamped log file create failed
99 string tsPart; // log file name timestamp part: 2012_08_17_16_04_59_148
100 string pidPart; // log file name pid part: 1234
101 string lastPath; // last log file path: /var/log/openm.log
102 string stampedPath; // stamped log file path: /var/log/openm.2012_08_17_16_04_59_148.1234.log
103 bool isNoMsgTime; // if true then not prefix messages with date-time
104 string rankPrefix; // process rank log prefix
105 bool isMsgRank; // if true then prefix messages with process rank
106
107 static const size_t msgBufferSize = 32768;
108 char msgBuffer[msgBufferSize + 1]; // buffer to format message
109
110 protected:
111
113 bool doLogMsg(const char * i_msg, const char * i_extra) noexcept;
114
115 // create log file or truncate existing, return false on error
116 bool logFileCreate(const string & i_path) noexcept;
117
118 // log to console
119 virtual bool logToConsole(
120 const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
121 ) noexcept = 0;
122
123 // log to file, return false on error
124 virtual bool logToFile(
125 bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
126 ) noexcept = 0;
127
128 // write date-time and message to output stream, return false on error
129 bool writeToLog(
130 ostream & i_ost, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
131 );
132
133 private:
134 LogBase(const LogBase & i_log) = delete;
135 LogBase & operator=(const LogBase & i_log) = delete;
136 };
137
149 class Log : public LogBase, public ILog
150 {
151 public:
161 Log(
162 bool i_logToConsole = false,
163 const char * i_basePath = "openm.log",
164 bool i_logToFile = false,
165 bool i_useTimeStamp = false,
166 bool i_usePidStamp = false
167 );
168 ~Log(void) noexcept;
169
181 void init(
182 bool i_logToConsole,
183 const char * i_basePath,
184 bool i_logToFile,
185 bool i_useTimeStamp = false,
186 bool i_usePidStamp = false,
187 bool i_noMsgTime = false,
188 bool i_isLogSql = false
189 ) noexcept override;
190
192 const string lastLogPath(void) noexcept override;
193
195 const string stampedLogPath(void) noexcept override;
196
198 const string getMessage(const char * i_sourceMsg) noexcept override;
199
201 const list<string> getLanguages(void) noexcept override;
202
204 const unordered_map<string, string> getLanguageMessages(void) noexcept override;
205
207 void swapLanguageMessages(const list<string> & i_langLst, unordered_map<string, string> & io_msgMap) noexcept override;
208
210 void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
211
213 bool logFormatted(const char * i_format, ...) noexcept override;
214
216 void logErr(const exception & i_ex, const char * i_msg = nullptr) noexcept override;
217
219 void logSql(const char * i_sql) noexcept override;
220
221 private:
222 bool isSqlLog; // if true then log sql queries into last log
223 list<string> msgLangLst; // list of languages, in order of user prefrences
224 unordered_map<string, string> msgMap; // language-specific messages
225
226 // log to console and flush each line, return false on error
227 bool logToConsole(
228 const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
229 ) noexcept override;
230
231 // log to file, return false on error
232 bool logToFile(
233 bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
234 ) noexcept override;
235
236 private:
237 Log(const Log & i_log) = delete;
238 Log & operator=(const Log & i_log) = delete;
239 };
240
254 class TraceLog : public LogBase, public ITrace
255 {
256 public:
268 bool i_logToConsole = false,
269 const char * i_basePath = "",
270 bool i_logToFile = false,
271 bool i_useTimeStamp = false,
272 bool i_usePidStamp = false,
273 bool i_noMsgTime = false
274 ) : LogBase(i_logToConsole, i_basePath, i_logToFile, i_useTimeStamp, i_usePidStamp, i_noMsgTime)
275 { }
276
277 ~TraceLog(void) noexcept;
278
288 void init(
289 bool i_logToConsole,
290 const char * i_basePath,
291 bool i_logToFile,
292 bool i_useTimeStamp = false,
293 bool i_usePidStamp = false,
294 bool i_noMsgTime = false
295 ) noexcept override;
296
298 void logMsg(const char * i_msg, const char * i_extra = NULL) noexcept override;
299
301 bool logFormatted(const char * i_format, ...) noexcept override;
302
303 private:
304 ofstream lastSt; // last log output stream
305 ofstream stampedSt; // stamped log output stream
306
307 // log to console, return false on error
308 bool logToConsole(
309 const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
310 ) noexcept override;
311
312 // log to file, return false on error
313 bool logToFile(
314 bool i_isToStamped, const chrono::system_clock::time_point & i_msgTime, const char * i_msg, const char * i_extra = nullptr
315 ) noexcept override;
316
317 private:
318 TraceLog(const TraceLog & i_log) = delete;
319 TraceLog & operator=(const TraceLog & i_log) = delete;
320 };
321}
322
323#ifdef _WIN32
324#pragma warning(pop)
325#endif // _WIN32
326
327#endif // OM_LOG_H
Log base class: log to console and into log files.
Definition log.h:35
bool doLogMsg(const char *i_msg, const char *i_extra) noexcept
implement log message: log to console and log files
Definition log.cpp:198
const bool isEnabled(void) noexcept override
return true if log to console or to file enabled.
Definition log.cpp:174
~LogBase(void) noexcept
cleanup log resources
Definition log.cpp:76
const bool isCreateError(void) noexcept override
error at file create of last log or stamped log file
Definition log.cpp:186
const string timeStamp(void) noexcept override
return timestamp part of log file name: 2012_08_17_16_04_59_148.
Definition log.cpp:162
void setRank(int i_rank, int i_worldSize) noexcept override
use process rank as log message prefix
Definition log.cpp:138
log implementation class: log to console and into log files.
Definition log.h:150
const unordered_map< string, string > getLanguageMessages(void) noexcept override
get copy of language-specific messages
Definition log.cpp:396
~Log(void) noexcept
cleanup log resources
Definition log.cpp:302
const list< string > getLanguages(void) noexcept override
get list of language name for the messages, eg: (en-ca, en)
Definition log.cpp:383
bool logFormatted(const char *i_format,...) noexcept override
log message formatted with vsnprintf(), return false on error
Definition log.cpp:442
const string getMessage(const char *i_sourceMsg) noexcept override
get language-specific message by source non-translated message
Definition log.cpp:365
const string lastLogPath(void) noexcept override
if log to file enabled return "last" log file path.
Definition log.cpp:341
void logSql(const char *i_sql) noexcept override
log sql query
Definition log.cpp:478
void logErr(const exception &i_ex, const char *i_msg=nullptr) noexcept override
log exception
Definition log.cpp:469
const string stampedLogPath(void) noexcept override
if log to "stamped" file enabled return "stamped" log file path.
Definition log.cpp:353
void swapLanguageMessages(const list< string > &i_langLst, unordered_map< string, string > &io_msgMap) noexcept override
set language-specific messages and update list of languages
Definition log.cpp:409
void logMsg(const char *i_msg, const char *i_extra=NULL) noexcept override
log message
Definition log.cpp:420
trace log implementation class: model event log to console and into log files.
Definition log.h:255
~TraceLog(void) noexcept
cleanup trace log resources
Definition log.cpp:536
bool logFormatted(const char *i_format,...) noexcept override
log message formatted with vsnprintf(), return false on error
Definition log.cpp:583
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:267
void logMsg(const char *i_msg, const char *i_extra=NULL) noexcept override
log message
Definition log.cpp:574
OpenM++ common helper utilities.
openM++ namespace
Definition log.h:32
@ init
initial status
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:20
log public interface: log to console and into log files
Definition omLog.h:47
trace log public interface: model event log to console and into log files
Definition omLog.h:96