-
Notifications
You must be signed in to change notification settings - Fork 39
/
Logger.h
executable file
·335 lines (284 loc) · 7.99 KB
/
Logger.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**************************************************************
* Copyright (c) 2010-2014, Dynamic Network Services, Inc.
* Author - Jake Montgomery ([email protected])
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#pragma once
#include <string>
#include <stdio.h>
#include <cstdarg>
#include "LogTypes.h"
/**
* Provides common logging functionality, without specific knowledge of the
* log levels and types used by the application.
*
* This should be subclasses to provide the actual log levels and types. Note
* that, for performance reasons, most of the methods are not virtual.
*
* Note that the organization is intended to separate the Logger from the Log
* types and levels only to make upstreaming easier, while maintaining strict
* type safety for the Log::Level and Log::Type enums.
*
* It is thread safe, as long as only one is created. Only create one subclass
* per process.
*/
class Logger
{
public:
/**
* Extended time info
*
*/
struct TimeInfo
{
enum Type
{
None,
Real,
Mono
};
};
static const size_t MaxMessageLen = 1024;
/**
* Create a logger. Uses stderr for logging by default.
* May throw on memory error.
*/
Logger();
virtual ~Logger();
/**
* All logging goes to syslog. Stops file logging, if any.
* Note that teeLogToStderr uses the built in syslog facility, and is separate
* from the SetStdErr().
* Logging due to SetStdOut() and SetStdErr() continues.
*
* @param ident [in] - The name to use for syslogging.
* @param teeLogToStderr [in] - Should all logging also be sent to stdout?
*/
void LogToSyslog(const char *ident, bool teeLogToStderr);
/**
* All logging goes to the file. Stops syslog logging.
*
* @param logFilePath [in] - The path of the file to log to.
*
* @return bool - false if failed to open file.
*/
bool LogToFile(const char *logFilePath);
/**
* Sets the level for Optional() logging.
*
* @param logLevel [in] - The new level.
*/
//void SetLogLevel(Log::Level logLevel);
/**
* Enables or disables one specific type of logging.
*
* @param type
* @param enable
*/
void EnableLogType(Log::Type type, bool enable);
/**
* Checks if the given log type is currently enabled
*
* @param level
*
* @return bool
*/
bool LogTypeEnabled(Log::Type type);
/**
*
* Enables/disables throwing a LogException() for the given log type.
*
*
* @param type
* @param shouldThrow
*/
void ThrowOnLogType(Log::Type type, bool shouldThrow);
/**
* Checks if the given log type will throw an exception.
*
* @param type
*
* @return bool
*/
bool ThrowOnLogTypeEnabled(Log::Type type);
/**
* Gets the name of the given log type.
*
* @param type
*
* @return const char* - "unknown" on failure. Never null.
*/
const char* LogTypeToString(Log::Type type);
/**
* Gets the description of the given log type.
*
* @param type
*
* @return const char* - "" if there is no description. Never null.
*/
const char* LogTypeDescription(Log::Type type);
/**
* Gets the log type for the string.
*
* @param str
*
* @return Logger::Type - TypeCount on failure.
*/
Log::Type StringToLogType(const char *str);
/**
* Resets all log typed based on the given level
*
* @param level
*/
void SetLogLevel(Log::Level level);
/**
* Sets the given type to log to stdout in addition to normal logging. The
* message will be logged to stdout only if the log type is also enabled.
*
* @param type
* @param shouldOut
*/
void SetStdOut(Log::Type type, bool shouldOut);
/**
* Sets the all types in the level to log to stdout in addition to normal
* logging. The message will be logged to stdout only if the log type is also
* enabled.
*
* @param type
* @param shouldOut
*/
void SetStdOut(Log::Level level, bool shouldOut);
/**
* Sets the given type to log to stderr in addition to normal logging. The
* message will be logged to stdout only if the log type is also enabled.
*
* @param type
* @param shouldOut
*/
void SetStdErr(Log::Type type, bool shouldOut);
/**
* Sets the all types in the level to log to stdout in addition to normal
* logging. The message will be logged to stdout only if the log type is also
* enabled.
*
* @param type
* @param shouldOut
*/
void SetStdErr(Log::Level level, bool shouldOut);
/**
* Configure extended time info.
*
* @param type
*/
void SetExtendedTimeInfo(TimeInfo::Type type);
/**
* Turns on or off adding log type name to logging string.
*
* @param printTypeNames
*/
void SetPrintTypeNames(bool printTypeNames);
/**
* Gets the name of the level.
*
* @param level
*
* @return const char* - "unknown" on failure. Never null.
*/
const char* LogLevelToString(Log::Level level);
/**
* Gets the log level for the given string
*
* @param str
*
* @return Log::Level - Returns Log::LevelCount on failure
*/
Log::Level StringToLogLevel(const char *str);
/**
* Optionally logs a message depending on the setting for that type. No newline
* is needed.
*
* @param type
* @param format
*/
void Optional(Log::Type type, const char *format, ...) ATTR_FORMAT(printf, 3, 4);
/**
* Same as Optional(type, format, ...)
*
* @param type
* @param args
*/
void OptionalVa(Log::Type type, const char* format, va_list args);
/**
* Always logs message.
* No newline is needed.
*/
void Message(Log::Type type, const char* format, ...)ATTR_FORMAT(printf, 3, 4);
void MessageVa(Log::Type type, const char *format, va_list args);
/**
* shortcut for Message(Logger::Error,
*/
void LogError(const char* format, ...)ATTR_FORMAT(printf, 2, 3);
/**
* shortcut for Message(Logger::Warn,
*/
void LogWarn(const char* format, ...)ATTR_FORMAT(printf, 2, 3);
/**
* Logs the message, then the errno string for the error. Always logs as Error.
*
* @param errnum
* @param mgs
*/
void ErrnoError(int errnum, const char* mgs);
/**
* Logs a message and exits the program.
* If using syslog then this uses the LOG_CRIT level.
* No newline is needed.
*/
void Fatal(const char* format, ...)ATTR_FORMAT(printf, 2, 3);
protected:
struct TypeInfo
{
bool enabled;
bool throws;
int syslogPriority;
const char *logName; // for use in log file.
const char *name; // name to use when enabling/disabling.
const char *description; // Optional description of log type
bool toStdErr;
bool toStdOut;
};
struct LevelInfo
{
LevelInfo() : name(), types() { }
std::string name;
bool *types; // size is m_typeCount
};
// m_types is protected by m_settingsLock, and should only be modified by the
// derived class during construction.
TypeInfo *m_types; // The type info for each type
const size_t m_typeCount; // The size of m_types. Not locked.
// This will never change after construction
LevelInfo *m_levelsMap;
const size_t m_levelCount; // The size of m_levelsMap. Not locked.
// Copies m_levelsMap types from source to dest
void copyLevelTypes(Log::Level source, Log::Level dest);
// Sets all types in the log level to value
void setLevelTypes(Log::Level level, bool value);
private:
void closeSyslog();
void closeLogFile();
bool isTypeInLevel(Log::Type type, Log::Level level);
void logMsg(const TypeInfo *typeInfo, const char *format, va_list args);
bool isLogTypeValid(Log::Type type);
bool isLogLevelValid(Log::Level level);
//
// All these are protected by m_settingsLock
//
pthread_rwlock_t m_settingsLock;
FILE *m_logFile; // File to log to (if any)
std::string m_logFilePath; // The path of the current m_logFile.
bool m_useSyslog; // Use syslog
std::string m_ident; // Used with syslog
TimeInfo::Type m_extendedTimeInfo;
bool m_printTypeNames; // Add the type names to the log
};