/
log.cpp
403 lines (351 loc) · 10.2 KB
/
log.cpp
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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2012 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/>.
*/
#include "de/Log"
#include "de/Time"
#include "de/Date"
#include "de/LogBuffer"
#include "de/Guard"
#include "logtextstyle.h"
#include <QMap>
#include <QTextStream>
#include <QThread>
#include <QStringList>
#include <QDebug>
namespace de {
char const *MAIN_SECTION = "";
/// If the section is longer than this, it will be alone on one line while
/// the rest of the entry continues after a break.
static int const LINE_BREAKING_SECTION_LENGTH = 35;
namespace internal {
/**
* @internal
* The logs table is lockable so that multiple threads can access their logs at
* the same time.
*/
class Logs : public QMap<QThread *, Log *>, public Lockable
{
public:
Logs() {}
~Logs() {
DENG2_GUARD(this);
// The logs are owned by the logs table.
foreach(Log *log, values()) {
delete log;
}
}
};
} // namespace internal
/// The logs table contains the log of each thread that uses logging.
static std::auto_ptr<internal::Logs> logsPtr;
LogEntry::LogEntry() : _level(TRACE), _sectionDepth(0), _disabled(true)
{}
LogEntry::LogEntry(Level level, String const §ion, int sectionDepth, String const &format, Args args)
: _level(level), _section(section), _sectionDepth(sectionDepth), _format(format), _disabled(false), _args(args)
{
if(!LogBuffer::appBuffer().isEnabled(level))
{
_disabled = true;
}
}
LogEntry::~LogEntry()
{
// The entry has ownership of its args.
for(Args::iterator i = _args.begin(); i != _args.end(); ++i)
{
delete *i;
}
}
String LogEntry::asText(Flags const &formattingFlags, int shortenSection) const
{
/// @todo This functionality belongs in an entry formatter class.
Flags flags = formattingFlags;
QString result;
QTextStream output(&result);
if(_defaultFlags & Simple)
{
flags |= Simple;
}
// In simple mode, skip the metadata.
if(!flags.testFlag(Simple))
{
// Begin with the timestamp.
if(flags & Styled) output << TEXT_STYLE_LOG_TIME;
output << _when.asText(Date::BuildNumberAndTime) << " ";
if(!flags.testFlag(Styled))
{
char const *levelNames[LogEntry::MAX_LOG_LEVELS] = {
"(...)",
"(deb)",
"(vrb)",
"",
"(inf)",
"(WRN)",
"(ERR)",
"(!!!)"
};
output << qSetPadChar(' ') << qSetFieldWidth(5) << levelNames[_level] <<
qSetFieldWidth(0) << " ";
}
else
{
char const *levelNames[LogEntry::MAX_LOG_LEVELS] = {
"Trace",
"Debug",
"Verbose",
"",
"Info",
"Warning",
"ERROR",
"FATAL!"
};
output << "\t"
<< (_level >= LogEntry::WARNING? TEXT_STYLE_LOG_BAD_LEVEL : TEXT_STYLE_LOG_LEVEL)
<< levelNames[_level] << "\t\r";
}
}
// Section name.
if(!flags.testFlag(OmitSection) && !_section.empty())
{
if(flags.testFlag(Styled))
{
output << TEXT_STYLE_SECTION;
}
// Process the section: shortening and possible abbreviation.
QString sect;
if(flags.testFlag(AbbreviateSection))
{
/*
* We'll split the section into parts, and then abbreviate some of
* the parts, trying not to lose too much information.
* @a shortenSection controls how much of the section can be
* abbreviated (num of chars from beginning).
*/
QStringList parts = _section.split(" > ");
int len = 0;
while(!parts.isEmpty())
{
if(!sect.isEmpty())
{
len += 3;
sect += " > ";
}
if(len + parts.first().size() >= shortenSection) break;
len += parts.first().size();
if(sect.isEmpty())
{
// Never abbreviate the first part.
sect += parts.first();
}
else
{
sect += "..";
}
parts.removeFirst();
}
// Append the remainer as-is.
sect += _section.mid(len);
}
else
{
if(shortenSection < _section.size())
{
sect = _section.right(_section.size() - shortenSection);
}
}
if(flags.testFlag(SectionSameAsBefore))
{
if(!shortenSection || sect.isEmpty())
{
output << "^ : ";
}
else
{
output << "^" << sect << ": ";
}
}
else
{
// If the section is very long, it's clearer to break the line here.
char const *separator = (sect.size() > LINE_BREAKING_SECTION_LENGTH? ":\n " : ": ");
output << sect << separator;
}
}
if(flags.testFlag(Styled))
{
output << TEXT_STYLE_MESSAGE;
}
// Message text with the arguments formatted.
if(_args.empty())
{
output << _format; // Verbatim.
}
else
{
Args::const_iterator arg = _args.begin();
DENG2_FOR_EACH_CONST(String, i, _format)
{
if(*i == '%')
{
if(arg == _args.end())
{
// Out of args.
throw IllegalFormatError("LogEntry::asText", "Ran out of arguments");
}
output << String::patternFormat(i, _format.end(), **arg);
++arg;
}
else
{
output << *i;
}
}
// Just append the rest of the arguments without special instructions.
for(; arg != _args.end(); ++arg)
{
output << **arg;
}
}
if(flags & Styled)
{
output << TEXT_STYLE_MESSAGE;
}
return result;
}
QTextStream &operator << (QTextStream &stream, LogEntry::Arg const &arg)
{
switch(arg.type())
{
case LogEntry::Arg::INTEGER:
stream << arg.intValue();
break;
case LogEntry::Arg::FLOATING_POINT:
stream << arg.floatValue();
break;
case LogEntry::Arg::STRING:
stream << arg.stringValue();
break;
}
return stream;
}
Log::Section::Section(char const *name) : _log(threadLog()), _name(name)
{
_log.beginSection(_name);
}
Log::Section::~Section()
{
_log.endSection(_name);
}
Log::Log() : _throwawayEntry(0)
{
_throwawayEntry = new LogEntry; // disabled LogEntry, so doesn't accept arguments
_sectionStack.push_back(MAIN_SECTION);
}
Log::~Log()
{
delete _throwawayEntry;
}
void Log::beginSection(char const *name)
{
_sectionStack.append(name);
}
void Log::endSection(char const *DENG2_DEBUG_ONLY(name))
{
DENG2_ASSERT(_sectionStack.back() == name);
_sectionStack.takeLast();
}
LogEntry &Log::enter(String const &format, LogEntry::Args arguments)
{
return enter(LogEntry::MESSAGE, format, arguments);
}
LogEntry &Log::enter(LogEntry::Level level, String const &format, LogEntry::Args arguments)
{
if(!LogBuffer::appBuffer().isEnabled(level))
{
DENG2_ASSERT(arguments.isEmpty());
// If the level is disabled, no messages are entered into it.
return *_throwawayEntry;
}
// Collect the sections.
String context;
String latest;
int depth = 0;
foreach(char const *i, _sectionStack)
{
if(i == latest)
{
// Don't repeat if it has the exact same name (due to recursive calls).
continue;
}
if(context.size())
{
context += " > ";
}
latest = i;
context += i;
++depth;
}
// Make a new entry.
LogEntry *entry = new LogEntry(level, context, depth, format, arguments);
// Add it to the application's buffer. The buffer gets ownership.
LogBuffer::appBuffer().add(entry);
return *entry;
}
Log &Log::threadLog()
{
if(!logsPtr.get()) logsPtr.reset(new internal::Logs);
internal::Logs& logs = *logsPtr;
DENG2_GUARD(logs);
// Each thread has its own log.
QThread *thread = QThread::currentThread();
internal::Logs::const_iterator found = logs.constFind(thread);
if(found == logs.constEnd())
{
// Create a new log.
Log* theLog = new Log;
qDebug() << "Log" << theLog << "created for thread" << thread;
logs.insert(thread, theLog);
return *theLog;
}
else
{
return *found.value();
}
}
void Log::disposeThreadLog()
{
if(!logsPtr.get()) logsPtr.reset(new internal::Logs);
internal::Logs& logs = *logsPtr;
DENG2_GUARD(logs);
QThread *thread = QThread::currentThread();
internal::Logs::iterator found = logs.find(thread);
if(found != logs.end())
{
delete found.value();
logs.remove(found.key());
}
}
LogEntryStager::LogEntryStager(LogEntry::Level level, String const &format) : _level(level)
{
_disabled = !LogBuffer::appBuffer().isEnabled(level);
if(!_disabled)
{
_format = format;
}
}
} // namespace de