/
log.h
509 lines (432 loc) · 15.9 KB
/
log.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_LOG_H
#define LIBDENG2_LOG_H
#include "../Time"
#include "../String"
#include "../Lockable"
#include "../Guard"
#include "../ISerializable"
#include <QList>
#include <vector>
#include <string>
#include <cstdlib>
/// Macro for accessing the local log of the current thread.
#define LOG() de::Log::threadLog()
/// Macro for accessing the local log of the current thread and entering
/// a new log section.
#define LOG_AS(sectionName) \
de::Log::Section __logSection = de::Log::Section(sectionName);
/**
* Macro for accessing the local log of the current thread and entering
* a new log section with a de::String variable based name.
*
* @param str Anything out of which a de::String can be constructed.
*
* @note Log::Section doesn't own the strings passed in; we have to
* ensure that the string exists in memory as long as the section (scope)
* is valid.
*/
#define LOG_AS_STRING(str) \
de::String __logSectionName = str; \
de::Block __logSectionUtf8 = __logSectionName.toUtf8(); \
LOG_AS(__logSectionUtf8.constData());
#define LOG_TRACE(str) de::LogEntryStager(de::LogEntry::TRACE, str)
#define LOG_DEBUG(str) de::LogEntryStager(de::LogEntry::DEBUG, str)
#define LOG_VERBOSE(str) de::LogEntryStager(de::LogEntry::VERBOSE, str)
#define LOG_MSG(str) de::LogEntryStager(de::LogEntry::MESSAGE, str)
#define LOG_INFO(str) de::LogEntryStager(de::LogEntry::INFO, str)
#define LOG_WARNING(str) de::LogEntryStager(de::LogEntry::WARNING, str)
#define LOG_ERROR(str) de::LogEntryStager(de::LogEntry::ERROR, str)
#define LOG_CRITICAL(str) de::LogEntryStager(de::LogEntry::CRITICAL, str)
#define LOG_AT_LEVEL(level, str) de::LogEntryStager(level, str)
#ifdef DENG2_DEBUG
/**
* Makes a developer-only TRACE level log entry. Only enabled in debug builds;
* use this for internal messages that are only useful to / understood by
* developers when debugging. (Note that parameters differ compared to the
* normal LOG_* macros.)
*/
# define LOG_DEV_TRACE(form, args) LOG_TRACE(form) << args
#else
# define LOG_DEV_TRACE(form, args)
#endif
#ifdef WIN32
# undef ERROR
#endif
namespace de {
class LogBuffer;
/**
* An entry to be stored in the log entry buffer. Log entries are created with
* Log::enter().
*
* Log entry arguments must be created before the entry itself is created. The
* LogEntryStager class is designed to help with this. Once an entry is
* inserted to the log buffer, no modifications may be done to it any more
* because another thread may need it immediately for flushing.
*
* @ingroup core
*/
class DENG2_PUBLIC LogEntry : public Lockable, public ISerializable
{
public:
/// Level of the log entry.
enum Level
{
/**
* Trace messages are intended for low-level debugging. They should be used
* to log which methods are entered and exited, and mark certain points within
* methods. Intended only for developers and debug builds.
*/
TRACE = 0,
/**
* Debug messages are intended for normal debugging. They should be enabled
* only in debug builds. An example of a debug message might be a printout of
* a ZIP archive's file count and size once an archive has been successfully
* opened. Intended only for developers and debug builds.
*/
DEBUG = 1,
/**
* Verbose messages should be used to log technical information that is only
* of interest to advanced users. An example of a verbose message could be
* the summary of all the defined object types during the launch of a game.
* Verbose messages should not be used for anything that produces a large
* number of log entries, such as an entry about reading the contents of a
* file within a ZIP archive (which would be suitable for the DEBUG level).
*/
VERBOSE = 2,
/**
* Normal log entries are intended for regular users. An example: message about
* which map is being loaded.
*/
MESSAGE = 3,
/**
* Info messages are intended for situations that are particularly noteworthy.
* An info message should be used for instance when a script has been stopped
* because of an uncaught exception occurred during its execution.
*/
INFO = 4,
/**
* Warning messages are reserved for recoverable error situations. A warning
* might be logged for example when the expected resource could not be found,
* and a fallback resource was used instead.
*/
WARNING = 5,
/**
* Error messages are intended for nonrecoverable errors. The error is grave
* enough to cause the shutting down of the current game, but the engine can
* still remain running.
*/
ERROR = 6,
/**
* Critical messages are intended for fatal errors that cause the engine to be
* shut down.
*/
CRITICAL = 7,
MAX_LOG_LEVELS
};
static String levelToText(Level level)
{
switch(level)
{
case TRACE: return "TRACE";
case DEBUG: return "DEBUG";
case VERBOSE: return "VERBOSE";
case MESSAGE: return "MESSAGE";
case INFO: return "INFO";
case WARNING: return "WARNING";
case ERROR: return "ERROR";
case CRITICAL: return "CRITICAL";
default: return "";
}
}
static Level textToLevel(String text)
{
for(int i = TRACE; i < MAX_LOG_LEVELS; ++i)
{
if(!levelToText(Level(i)).compareWithoutCase(text))
return Level(i);
}
throw Error("Log::textToLevel", "'" + text + "' is not a valid log level");
}
/**
* Argument for a log entry. The arguments of an entry are usually created
* automatically by LogEntryStager.
*
* @ingroup core
*/
class DENG2_PUBLIC Arg : public String::IPatternArg, public ISerializable
{
public:
/// The wrong type is used in accessing the value. @ingroup errors
DENG2_ERROR(TypeError);
enum Type {
INTEGER,
FLOATING_POINT,
STRING
};
/**
* Base class for classes that support adding to the arguments. Any
* class that is derived from this class may be used as an argument for
* log entries. In practice, all arguments are converted to either
* numbers (64-bit integer or double) or text strings.
*/
class Base {
public:
/// Attempted conversion from unsupported type.
DENG2_ERROR(TypeError);
public:
virtual ~Base() {}
virtual Type logEntryArgType() const = 0;
virtual dint64 asInt64() const {
throw TypeError("LogEntry::Arg::Base", "dint64 not supported");
}
virtual ddouble asDouble() const {
throw TypeError("LogEntry::Arg::Base", "ddouble not supported");
}
virtual String asText() const {
throw TypeError("LogEntry::Arg::Base", "String not supported");
}
};
public:
Arg() : _type(INTEGER) { _data.intValue = 0; }
Arg(dint i) : _type(INTEGER) { _data.intValue = i; }
Arg(duint i) : _type(INTEGER) { _data.intValue = i; }
Arg(long int i) : _type(INTEGER) { _data.intValue = i; }
Arg(long unsigned int i) : _type(INTEGER) { _data.intValue = i; }
Arg(duint64 i) : _type(INTEGER) { _data.intValue = dint64(i); }
Arg(dint64 i) : _type(INTEGER) { _data.intValue = i; }
Arg(ddouble d) : _type(FLOATING_POINT) { _data.floatValue = d; }
Arg(void const *p) : _type(INTEGER) { _data.intValue = dint64(p); }
Arg(char const *s) : _type(STRING) { _data.stringValue = new String(s); }
Arg(String const &s) : _type(STRING) { _data.stringValue = new String(s.data(), s.size()); }
Arg(Base const &arg);
Arg(Arg const &other);
~Arg();
inline Type type() const { return _type; }
inline dint64 intValue() const {
DENG2_ASSERT(_type == INTEGER);
return _data.intValue;
}
inline ddouble floatValue() const {
DENG2_ASSERT(_type == FLOATING_POINT);
return _data.floatValue;
}
inline QString stringValue() const {
DENG2_ASSERT(_type == STRING);
return *_data.stringValue;
}
// Implements String::IPatternArg.
ddouble asNumber() const;
String asText() const;
// Implements ISerializable.
void operator >> (Writer &to) const;
void operator << (Reader &from);
private:
Type _type;
union Data {
dint64 intValue;
ddouble floatValue;
String *stringValue;
} _data;
};
public:
enum Flag
{
/// In simple mode, only print the actual message contents,
/// without metadata.
Simple = 0x1,
/// Use escape sequences to format the entry with text styles
/// (for graphical output).
Styled = 0x2,
/// Omit the section from the entry text.
OmitSection = 0x4,
/// Indicate that the section is the same as on the previous line.
SectionSameAsBefore = 0x8,
/// Parts of the section can be abbreviated because they are clear
/// from the context (e.g., previous line).
AbbreviateSection = 0x10,
/// Entry is not from a local source. Could be used to mark entries
/// originating from a remote LogBuffer (over the network).
Remote = 0x20
};
Q_DECLARE_FLAGS(Flags, Flag)
/// The format string has incorrect syntax. @ingroup errors
DENG2_ERROR(IllegalFormatError);
typedef QList<Arg *> Args;
public:
/**
* Constructs a disabled log entry.
*/
LogEntry();
LogEntry(Level level, String const §ion, int sectionDepth, String const &format, Args args);
/**
* Copy constructor.
*
* @param other Log entry.
* @param extraFlags Additional flags to apply to the new entry.
*/
LogEntry(LogEntry const &other, Flags extraFlags = 0);
~LogEntry();
Flags flags() const;
/// Returns the timestamp of the entry.
Time when() const { return _when; }
Level level() const { return _level; }
/// Returns a reference to the entry's section part. Reference is valid
/// for the lifetime of the entry.
String const §ion() const { return _section; }
/// Returns the number of sub-sections in the entry's section part.
int sectionDepth() const { return _sectionDepth; }
/**
* Converts the log entry to a string.
*
* @param flags Flags that control how the text is composed.
* @param shortenSection Number of characters to cut from the beginning of the section.
* With AbbreviateSection this limits which portion of the
* section is subject to abbreviation.
*
* @return Composed textual representation of the entry.
*/
String asText(Flags const &flags = 0, int shortenSection = 0) const;
// Implements ISerializable.
void operator >> (Writer &to) const;
void operator << (Reader &from);
private:
void advanceFormat(String::const_iterator &i) const;
private:
Time _when;
Level _level;
String _section;
int _sectionDepth;
String _format;
Flags _defaultFlags;
bool _disabled;
Args _args;
};
QTextStream &operator << (QTextStream &stream, LogEntry::Arg const &arg);
Q_DECLARE_OPERATORS_FOR_FLAGS(LogEntry::Flags)
/**
* Provides means for adding log entries into the log entry buffer (LogBuffer).
* Each thread has its own Log instance. A thread's Log keeps track of the
* thread-local section stack.
*
* Note that there is only one LogBuffer where all the entries are collected.
*
* @ingroup core
*/
class DENG2_PUBLIC Log
{
public:
class DENG2_PUBLIC Section
{
public:
/**
* The Section does not take a copy of @c name, so whatever
* it's pointing to must exist while the Section exists.
*
* @param name Name of the log section.
*/
Section(char const *name);
~Section();
Log &log() const { return _log; }
private:
Log &_log;
char const *_name;
};
public:
Log();
virtual ~Log();
/**
* Begins a new section in the log. Sections can be nested.
*
* @param name Name of the section. No copy of this string is made,
* so it must exist while the section is in use.
*/
void beginSection(char const *name);
/**
* Ends the topmost section in the log.
*
* @param name Name of the topmost section.
*/
void endSection(char const *name);
/**
* Creates a new log entry with the default (MESSAGE) level.
*
* @param format Format template of the entry.
* @param arguments List of arguments. The entry is given ownership of
* each Arg instance.
*/
LogEntry &enter(String const &format, LogEntry::Args arguments = LogEntry::Args());
/**
* Creates a new log entry with the specified log entry level.
*
* @param level Level of the entry.
* @param format Format template of the entry.
* @param arguments List of arguments. The entry is given ownership of
* each Arg instance.
*/
LogEntry &enter(LogEntry::Level level, String const &format, LogEntry::Args arguments = LogEntry::Args());
public:
/**
* Returns the logger of the current thread.
*/
static Log &threadLog();
/**
* Deletes the current thread's log. Threads should call this before
* they quit.
*/
static void disposeThreadLog();
private:
typedef QList<char const *> SectionStack;
SectionStack _sectionStack;
LogEntry *_throwawayEntry;
};
/**
* Stages a log entry for insertion into LogBuffer. Instances of LogEntryStager
* are built on the stack.
*
* You should use the @c LOG_* macros instead of using LogEntryStager directly.
*/
class DENG2_PUBLIC LogEntryStager
{
public:
LogEntryStager(LogEntry::Level level, String const &format);
/// Appends a new argument to the entry.
template <typename ValueType>
inline LogEntryStager &operator << (ValueType const &v) {
if(!_disabled) {
// Args are created only if the level is enabled.
_args.append(new LogEntry::Arg(v));
}
return *this;
}
~LogEntryStager() {
if(!_disabled) {
// Ownership of the entries is transferred to the LogEntry.
LOG().enter(_level, _format, _args);
}
}
private:
bool _disabled;
LogEntry::Level _level;
String _format;
LogEntry::Args _args;
};
} // namespace de
#endif /* LIBDENG2_LOG_H */