-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogger.hpp
96 lines (75 loc) · 3.2 KB
/
Logger.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef __LIBSPEEDWIRE_LOGGER_HPP__
#define __LIBSPEEDWIRE_LOGGER_HPP__
#include <string>
#include <deque>
namespace libspeedwire {
/*! \file */
/**
* Enumeration describing the defined log levels.
*/
enum class LogLevel {
LOG_ERROR = 0x01, /**< error level */
LOG_WARNING = 0x02, /**< warning level */
LOG_INFO_0 = 0x04, /**< verbose level 0 */
LOG_INFO_1 = 0x08, /**< verbose level 1 */
LOG_INFO_2 = 0x10, /**< verbose level 2 */
LOG_INFO_3 = 0x20, /**< verbose level 3 */
};
/** Global scope operator for bitwise or'ing two LogLevel enum values */
inline LogLevel operator|(const LogLevel& op1, const LogLevel& op2) {
return (LogLevel)((int)op1 | (int)op2);
}
/** Global scope operator for bitwise and'ing two LogLevel enum values */
inline LogLevel operator&(const LogLevel& op1, const LogLevel& op2) {
return (LogLevel)((int)op1 & (int)op2);
}
/** Global scope operator for not equal comparison of two LogLevel enum values */
inline bool operator!=(const LogLevel& op1, const int& op2) {
return ((int)op1 != (int)op2);
}
/**
* Interface for routing log messages created by class Logger.
* Classes implementing this interface can route loger output to e.g. stdout, stderr, files, ...
*/
class ILogListener {
public:
/** Virtual destructor */
virtual ~ILogListener(void) {}
/**
* Method to output a single byte character message.
* @param msg The message string
* @param level The log level of the message string
*/
virtual void log_msg(const std::string& msg, const LogLevel& level) = 0;
/**
* Method to output a wide character message.
* @param msg The message string
* @param level The log level of the message string
*/
virtual void log_msg_w(const std::wstring& msg, const LogLevel& level) = 0;
};
/**
* Logger class.
* An instance of this classed can be instanciated for each module, for instance by declaring a static instance with local scope inside
* the source code of the module. The class supports different log levels (see class LogLevel) and the registration of log listeners
* (see interface ILogListener). Log listeners can route log output to different output means and also limit the output to the
* log levels defined during registration.
*/
class Logger {
public:
Logger(const char* moduleName);
Logger(const std::string& moduleName);
static void setLogListener(ILogListener* listener, LogLevel level);
void print(LogLevel level, const char* format, ...);
void print(LogLevel level, const wchar_t* format, ...);
private:
typedef struct {
ILogListener* listener;
LogLevel level;
} ListenerEntry;
static ListenerEntry* s_listener;
std::string m_module_name;
std::wstring m_module_name_w;
};
} // namespace libspeedwire
#endif