1 #pragma once
2 
3 #include <deque>
4 #include <list>
5 #include <string>
6 #include <fstream>
7 
8 enum _eLogLevel : uint32_t
9 {
10 	LOG_NORM = 0x0000001,
11 	LOG_STATUS = 0x0000002,
12 	LOG_ERROR = 0x0000004,
13 	LOG_DEBUG_INT = 0x0000008, //do not use directly, use _log.Debug(...) instead
14 	//
15 	LOG_ALL = 0xFFFFFFF //Used by web interface to retrieve all log types
16 };
17 enum _eDebugLevel : uint32_t
18 {
19 	DEBUG_NORM = 0x0000001,
20 	DEBUG_HARDWARE = 0x0000002,
21 	DEBUG_RECEIVED = 0x0000004,
22 	DEBUG_WEBSERVER = 0x0000008,
23 	DEBUG_EVENTSYSTEM = 0x0000010,
24 	DEBUG_PYTHON = 0x0000020,
25 	DEBUG_THREADIDS = 0x0000030,
26 	//
27 	DEBUG_ALL = 0xFFFFFFF
28 };
29 
30 class CLogger
31 {
32 public:
33 	struct _tLogLineStruct
34 	{
35 		time_t logtime;
36 		_eLogLevel level;
37 		std::string logmessage;
38 		_tLogLineStruct(const _eLogLevel nlevel, const std::string &nlogmessage);
39 	};
40 
41 	CLogger(void);
42 	~CLogger(void);
43 
44 	bool SetLogFlags(const std::string &sFlags);
SetLogFlags(const uint32_t iFlags)45 	void SetLogFlags(const uint32_t iFlags) {
46 		m_log_flags = iFlags;
47 	}
IsLogLevelEnabled(const _eLogLevel level)48 	bool IsLogLevelEnabled(const _eLogLevel level) {
49 		return (m_log_flags & level);
50 	}
51 	bool SetDebugFlags(const std::string &sFlags);
SetDebugFlags(const uint32_t iFlags)52 	void SetDebugFlags(const uint32_t iFlags) {
53 		m_debug_flags = iFlags;
54 	}
IsDebugLevelEnabled(const _eDebugLevel level)55 	bool IsDebugLevelEnabled(const _eDebugLevel level) {
56 		if (!(m_log_flags & LOG_DEBUG_INT))
57 			return false;
58 		return (m_debug_flags & level);
59 	}
60 
61 	void SetOutputFile(const char *OutputFile);
62 
63 	void Log(const _eLogLevel level, const std::string& sLogline);
64 	void Log(const _eLogLevel level, const char* logline, ...)
65 #ifdef __GNUC__
66 		__attribute__ ((format (printf, 3, 4)))
67 #endif
68 	;
69 	void Debug(const _eDebugLevel level, const std::string& sLogline);
70 	void Debug(const _eDebugLevel level, const char* logline, ...)
71 #ifdef __GNUC__
72 		__attribute__ ((format (printf, 3, 4)))
73 #endif
74 	;
75 	void LogSequenceStart();
76 	void LogSequenceAdd(const char* logline);
77 	void LogSequenceAddNoLF(const char* logline);
78 	void LogSequenceEnd(const _eLogLevel level);
79 
80 	void EnableLogTimestamps(const bool bEnableTimestamps);
81 	bool IsLogTimestampsEnabled();
82 
83 	void ForwardErrorsToNotificationSystem(const bool bDoForward);
84 
85 	std::list<_tLogLineStruct> GetLog(const _eLogLevel level, const time_t lastlogtime = 0);
86 	void ClearLog();
87 
88 	std::list<_tLogLineStruct> GetNotificationLogs();
89 	bool NotificationLogsEnabled();
90 private:
91 	uint32_t m_log_flags;
92 	uint32_t m_debug_flags;
93 
94 	std::mutex m_mutex;
95 	std::ofstream m_outputfile;
96 	std::map<_eLogLevel, std::deque<_tLogLineStruct> > m_lastlog;
97 	std::deque<_tLogLineStruct> m_notification_log;
98 	bool m_bInSequenceMode;
99 	bool m_bEnableLogTimestamps;
100 	bool m_bEnableLogThreadIDs;
101 	bool m_bEnableErrorsToNotificationSystem;
102 	time_t m_LastLogNotificationsSend;
103 	std::stringstream m_sequencestring;
104 };
105 extern CLogger _log;
106