OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1
9// ###########################################################################################################################################################################################################################################################################################################################
10
11#pragma once
12
13// OpenTwin header
15#include "OTCore/Flags.h"
16#include "OTCore/OTAssert.h"
17#include "OTCore/CoreTypes.h"
18#include "OTCore/Serializable.h"
19
20// std header
21#include <string>
22#include <ostream>
23#include <list>
24
26#define OT_LOG_TIME_FORMAT_STDSTRING "%Y-%m-%d %H:%M:%S"
27
30#define OT_GLOBAL_LOGFLAG_LOGEnabled
31
34#define OT_GLOBAL_LOGFLAG_LOG_IEnabled
35
38#define OT_GLOBAL_LOGFLAG_LOG_DEnabled
39
42#define OT_GLOBAL_LOGFLAG_LOG_WEnabled
43
46#define OT_GLOBAL_LOGFLAG_LOG_EEnabled
47
48#ifdef OT_GLOBAL_LOGFLAG_LOGEnabled
49
50#ifdef _DEBUG
51
57#define OT_LOG(___text, ___flags) ot::LogDispatcher::instance().dispatch(___text, __FUNCTION__, ___flags)
58
59#else
60
66#define OT_LOG(___text, ___flags) if (ot::LogDispatcher::mayLog(___flags)) { ot::LogDispatcher::instance().dispatch(___text, __FUNCTION__, ___flags); }
67
68#endif
69
70#ifdef OT_GLOBAL_LOGFLAG_LOG_IEnabled
71
75#define OT_LOG_I(___text) OT_LOG(___text, ot::INFORMATION_LOG)
76
81#define OT_LOG_IA(___text) OTAssert(0, ___text); OT_LOG(___text, ot::INFORMATION_LOG)
82
86#define OT_LOG_IAS(___text) OTAssert(0, ""); OT_LOG(___text, ot::INFORMATION_LOG)
87
88#else // ifdef OT_GLOBAL_LOGFLAG_LOG_IEnabled
89
93#define OT_LOG_I(___text)
94
99#define OT_LOG_IA(___text)
100
104#define OT_LOG_IAS(___text)
105
106#endif // ifdef OT_GLOBAL_LOGFLAG_LOG_IEnabled
107
108#ifdef OT_GLOBAL_LOGFLAG_LOG_DEnabled
109
113#define OT_LOG_D(___text) OT_LOG(___text, ot::DETAILED_LOG)
114
119#define OT_LOG_DA(___text) OTAssert(0, ___text); OT_LOG(___text, ot::DETAILED_LOG)
120
124#define OT_LOG_DAS(___text) OTAssert(0, ""); OT_LOG(___text, ot::DETAILED_LOG)
125
126#else // ifdef OT_GLOBAL_LOGFLAG_LOG_DEnabled
127
131#define OT_LOG_D(___text)
132
137#define OT_LOG_DA(___text)
138
142#define OT_LOG_DAS(___text)
143
144#endif // ifdef OT_GLOBAL_LOGFLAG_LOG_DEnabled
145
146#ifdef OT_GLOBAL_LOGFLAG_LOG_WEnabled
147
150#define OT_LOG_W(___text) OT_LOG(___text, ot::WARNING_LOG)
151
155#define OT_LOG_WA(___text) OTAssert(0, ___text); OT_LOG(___text, ot::WARNING_LOG)
156
159#define OT_LOG_WAS(___text) OTAssert(0, ""); OT_LOG(___text, ot::WARNING_LOG)
160
161#else // ifdef OT_GLOBAL_LOGFLAG_LOG_WEnabled
162
165#define OT_LOG_W(___text)
166
170#define OT_LOG_WA(___text)
171
174#define OT_LOG_WAS(___text)
175
176#endif // ifdef OT_GLOBAL_LOGFLAG_LOG_WEnabled
177
178#ifdef OT_GLOBAL_LOGFLAG_LOG_EEnabled
179
182#define OT_LOG_E(___text) OT_LOG(___text, ot::ERROR_LOG)
183
187#define OT_LOG_EA(___text) OTAssert(0, ___text); OT_LOG(___text, ot::ERROR_LOG)
188
191#define OT_LOG_EAS(___text) OTAssert(0, ""); OT_LOG(___text, ot::ERROR_LOG)
192
193#else // ifdef OT_GLOBAL_LOGFLAG_LOG_EEnabled
194
197#define OT_LOG_E(___text)
198
202#define OT_LOG_EA(___text)
203
206#define OT_LOG_EAS(___text)
207
208#endif // ifdef OT_GLOBAL_LOGFLAG_LOG_EEnabled
209
210#else // ifdef OT_GLOBAL_LOGFLAG_LOGEnabled
211
217#define OT_LOG(___text, ___flags)
218
222#define OT_LOG_I(___text)
223
228#define OT_LOG_IA(___text)
229
233#define OT_LOG_IAS(___text)
234
238#define OT_LOG_D(___text)
239
244#define OT_LOG_DA(___text)
245
249#define OT_LOG_DAS(___text)
250
253#define OT_LOG_W(___text)
254
258#define OT_LOG_WA(___text)
259
262#define OT_LOG_WAS(___text)
263
266#define OT_LOG_E(___text)
267
271#define OT_LOG_EA(___text)
272
275#define OT_LOG_EAS(___text)
276
277#endif // ifdef OT_GLOBAL_LOGFLAG_LOGEnabled
278
279#pragma warning (disable: 4251)
280
281namespace ot {
282
300
302
303 OT_CORE_API_EXPORT void addLogFlagsToJsonArray(const LogFlags& _flags, JsonArray& _flagsArray, JsonAllocator& _allocator);
304
306
307}
309
310namespace ot {
311
314 public:
315 LogMessage();
316 LogMessage(const std::string& _serviceName, const std::string& _functionName, const std::string& _text, const LogFlags& _flags = LogFlags(ot::INFORMATION_LOG));
317 LogMessage(const LogMessage& _other);
318 virtual ~LogMessage();
319
320 LogMessage& operator = (const LogMessage& _other);
321
322 void setServiceName(const std::string& _serviceName) { m_serviceName = _serviceName; };
323 const std::string& getServiceName(void) const { return m_serviceName; };
324
325 void setFunctionName(const std::string& _functionName) { m_functionName = _functionName; };
326 const std::string& getFunctionName(void) const { return m_functionName; };
327
328 void setText(const std::string& _text) { m_text = _text; };
329 const std::string& getText(void) const { return m_text; };
330
331 void setFlags(const LogFlags& _flags) { m_flags = _flags; };
332 const LogFlags& getFlags(void) const { return m_flags; };
333
335 void setLocalSystemTime(const std::string& _timeString) { m_localSystemTime = _timeString; };
336
339 const std::string& getLocalSystemTime(void) const { return m_localSystemTime; };
340
342 void setGlobalSystemTime(const std::string& _timeString) { m_globalSystemTime = _timeString; };
343
346 const std::string& getGlobalSystemTime(void) const { return m_globalSystemTime; };
347
349 void setCurrentTimeAsLocalSystemTime(void);
350
352 void setCurrentTimeAsGlobalSystemTime(void);
353
354 void setUserName(const std::string& _userName) { m_userName = _userName; };
355 const std::string& getUserName(void) const { return m_userName; };
356
357 void setProjectName(const std::string& _projectName) { m_projectName = _projectName; };
358 const std::string& getProjectName(void) const { return m_projectName; };
359
363 virtual void addToJsonObject(JsonValue& _object, JsonAllocator& _allocator) const override;
364
368 virtual void setFromJsonObject(const ConstJsonObject& _object) override;
369
370 private:
371 friend OT_CORE_API_EXPORT std::ostream& operator << (std::ostream& _stream, const LogMessage& _msg);
372
373 std::string m_serviceName;
374 std::string m_functionName;
375 std::string m_text;
376 LogFlags m_flags;
377 std::string m_localSystemTime;
378 std::string m_globalSystemTime;
379 std::string m_userName;
380 std::string m_projectName;
381 };
382
386 OT_CORE_API_EXPORT std::ostream& operator << (std::ostream& _stream, const LogMessage& _msg);
387
388 // ######################################################################################################################################################
389
390 // ######################################################################################################################################################
391
392 // ######################################################################################################################################################
393
396 public:
397 AbstractLogNotifier() : m_deleteLater(false) {};
399
402 void setDeleteLogNotifierLater(bool _deleteLater = true) { m_deleteLater = _deleteLater; };
403
405 bool getDeleteLogNotifierLater(void) const { return m_deleteLater; };
406
408 virtual void log(const LogMessage& _message) = 0;
409
410 private:
411 bool m_deleteLater;
412 };
413
414 // ######################################################################################################################################################
415
416 // ######################################################################################################################################################
417
418 // ######################################################################################################################################################
419
422 public:
424
426 virtual void log(const LogMessage& _message) override;
427 };
428
429 // ######################################################################################################################################################
430
431 // ######################################################################################################################################################
432
433 // ######################################################################################################################################################
434
438 public:
439 LogNotifierFileWriter(const std::string& _serviceName);
440 virtual ~LogNotifierFileWriter();
441
443 virtual void log(const LogMessage& _message) override;
444
445 private:
446 std::ofstream* m_stream;
447 };
448
449 // ######################################################################################################################################################
450
451 // ######################################################################################################################################################
452
453 // ######################################################################################################################################################
454
458 public:
459 static LogDispatcher& instance(void);
460 static LogDispatcher& initialize(const std::string& _serviceName, bool _addCoutReceiver = false);
461 static inline bool mayLog(LogFlag _flags) { return ((_flags & LogDispatcher::instance().m_logFlags.data()) == _flags); };
462 static void addFileWriter(void);
463
464 // #################################################################################
465
466 // Setter/Getter
467
468 void setLogFlag(LogFlag _flag, bool _active = true) { m_logFlags.setFlag(_flag, _active); };
469 void setLogFlags(LogFlags _flags) { m_logFlags = _flags; };
470 LogFlags logFlags(void) const { return m_logFlags; };
471
472 void setServiceName(const std::string& _name) { m_serviceName = _name; };
473 const std::string& serviceName(void) const { return m_serviceName; };
474
475 void setUserName(const std::string& _name) { m_userName = _name; };
476 const std::string& getUserName(void) const { return m_userName; };
477
478 void setProjectName(const std::string& _name) { m_projectName = _name; };
479 const std::string& getProjectName(void) const { return m_projectName; };
480
484 void addReceiver(AbstractLogNotifier* _receiver);
485
486 // #################################################################################
487
494 void dispatch(const std::string& _text, const std::string& _functionName = std::string(), const LogFlags& _logFlags = LogFlags(ot::INFORMATION_LOG));
495
500 void dispatch(const LogMessage& _message);
501
502 private:
503 void applyEnvFlag(const std::string& _str);
504
505 LogFlags m_logFlags;
506 std::string m_serviceName;
507
508 std::list<AbstractLogNotifier*> m_messageReceiver;
509
510 std::string m_userName;
511 std::string m_projectName;
512
514 virtual ~LogDispatcher();
515
516 LogDispatcher(LogDispatcher&) = delete;
517 LogDispatcher& operator = (LogDispatcher&) = delete;
518 };
519
520}
#define OT_CORE_API_EXPORT
Dll import.
Definition CoreAPIExport.h:8
#define OT_ADD_FLAG_FUNCTIONS(___enumName)
Will add the default bitwise operations for the provided 32 bit bitfield. Use this at the bottom of t...
Definition Flags.h:60
#define OT_DECL_NODEFAULT(___class)
Removes the default copy constructor and assignment operator.
Definition OTClassHelper.h:22
#define OT_DECL_NOCOPY(___class)
Removes the default copy constructor and assignment operator.
Definition OTClassHelper.h:14
Used to receive every log message that is generated.
Definition Logger.h:395
virtual ~AbstractLogNotifier()
Definition Logger.h:398
virtual void log(const LogMessage &_message)=0
Called when the a log message was created.
AbstractLogNotifier()
Definition Logger.h:397
bool getDeleteLogNotifierLater(void) const
Returns true if the delete later mode is set.
Definition Logger.h:405
void setDeleteLogNotifierLater(bool _deleteLater=true)
Will set the delete later flag. If delete later is set, the creator keeps ownership of this object ev...
Definition Logger.h:402
This class is used to manage flags. Don't forget to add OT_ADD_FLAG_FUNCTIONS and the bottom of your ...
Definition Flags.h:91
JSON Array value.
Definition JSON.h:116
The LogDispatcher dispatches generated log messages to all registered log notifiers....
Definition Logger.h:457
const std::string & getUserName(void) const
Definition Logger.h:476
const std::string & serviceName(void) const
Definition Logger.h:473
void setLogFlag(LogFlag _flag, bool _active=true)
Definition Logger.h:468
void setUserName(const std::string &_name)
Definition Logger.h:475
const std::string & getProjectName(void) const
Definition Logger.h:479
void setServiceName(const std::string &_name)
Definition Logger.h:472
static bool mayLog(LogFlag _flags)
Definition Logger.h:461
LogFlags logFlags(void) const
Definition Logger.h:470
void setProjectName(const std::string &_name)
Definition Logger.h:478
void setLogFlags(LogFlags _flags)
Definition Logger.h:469
Contains information about the origin and the content of a log message.
Definition Logger.h:313
void setProjectName(const std::string &_projectName)
Definition Logger.h:357
const std::string & getServiceName(void) const
Definition Logger.h:323
const std::string & getProjectName(void) const
Definition Logger.h:358
void setText(const std::string &_text)
Definition Logger.h:328
void setFunctionName(const std::string &_functionName)
Definition Logger.h:325
const std::string & getFunctionName(void) const
Definition Logger.h:326
const LogFlags & getFlags(void) const
Definition Logger.h:332
const std::string & getUserName(void) const
Definition Logger.h:355
void setServiceName(const std::string &_serviceName)
Definition Logger.h:322
void setUserName(const std::string &_userName)
Definition Logger.h:354
void setLocalSystemTime(const std::string &_timeString)
Definition Logger.h:335
const std::string & getText(void) const
Definition Logger.h:329
void setFlags(const LogFlags &_flags)
Definition Logger.h:331
const std::string & getLocalSystemTime(void) const
String representation of the system timestamp at message creation. OT_LOG_TIME_FORMAT_STDSTRING conta...
Definition Logger.h:339
void setGlobalSystemTime(const std::string &_timeString)
Definition Logger.h:342
const std::string & getGlobalSystemTime(void) const
String representation of the system timestamp at when the message was received by the Logger Service....
Definition Logger.h:346
Definition Logger.h:435
Used to write created log messages to std cout in a way a human could read it.
Definition Logger.h:421
virtual ~LogNotifierStdCout()
Definition Logger.h:423
The Serializable class is the default interface of serializable objects.
Definition Serializable.h:17
void operator<<(const testing::internal::Secret &, int)
Definition Connector.h:8
OT_CORE_API_EXPORT void addLogFlagsToJsonArray(const LogFlags &_flags, JsonArray &_flagsArray, JsonAllocator &_allocator)
Definition Logger.cpp:34
OT_CORE_API_EXPORT LogFlags logFlagsFromJsonArray(const ConstJsonArray &_flagsArray)
Definition Logger.cpp:45
Flags< LogFlag > LogFlags
Definition Logger.h:301
rapidjson::GenericArray< true, rapidjson::GenericValue< rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > > > ConstJsonArray
Read only JSON Array.
Definition JSON.h:42
rapidjson::Value JsonValue
Writable JSON value.
Definition JSON.h:27
rapidjson::GenericObject< true, rapidjson::GenericValue< rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > > > ConstJsonObject
Read only JSON Object.
Definition JSON.h:35
LogFlag
Log message verbouse level.
Definition Logger.h:284
@ ALL_LOG_FLAGS
Mask used to set all incoming and outgoing message log flags.
Definition Logger.h:298
@ ONEWAY_TLS_INBOUND_MESSAGE_LOG
Queue endpoint log.
Definition Logger.h:292
@ QUEUED_INBOUND_MESSAGE_LOG
Execute endpoint log.
Definition Logger.h:291
@ ERROR_LOG
Warning log.
Definition Logger.h:289
@ DETAILED_LOG
Information log (few logs)
Definition Logger.h:287
@ INFORMATION_LOG
No log flags.
Definition Logger.h:286
@ WARNING_LOG
Detailed log (more logs)
Definition Logger.h:288
@ INBOUND_MESSAGE_LOG
Error log.
Definition Logger.h:290
@ ALL_INCOMING_MESSAGE_LOG_FLAGS
Mask used to set all general log flags.
Definition Logger.h:295
@ NO_LOG
Definition Logger.h:285
@ OUTGOING_MESSAGE_LOG
OneWay-TLS endpoint log.
Definition Logger.h:293
@ ALL_OUTGOING_MESSAGE_LOG_FLAGS
Mask used to set all incoming message log flags.
Definition Logger.h:296
@ ALL_GENERAL_LOG_FLAGS
Message out log.
Definition Logger.h:294
@ ALL_MESSAGE_LOG_FLAGS
Mask used to set all outgoing message log flags.
Definition Logger.h:297
rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > JsonAllocator
Allocator used for writing to JSON values.
Definition JSON.h:30
OT_CORE_API_EXPORT std::ostream & operator<<(std::ostream &_stream, const LogMessage &_msg)
Writes the log message in a typical "log line" format to the provided output stream.
Definition Logger.cpp:147