/
log.cpp
714 lines (618 loc) · 16.6 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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
* 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/>.
*/
#include "de/Log"
#include "de/Time"
#include "de/Date"
#include "de/LogBuffer"
#include "de/Guard"
#include "de/Reader"
#include "de/Writer"
#include "de/FIFO"
#include "logtextstyle.h"
#include <QMap>
#include <QTextStream>
#include <QThread>
#include <QStringList>
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;
/// Unused entry arguments are stored here in the pool.
static FIFO<LogEntry::Arg> argPool;
LogEntry::Arg::Arg() : _type(IntegerArgument)
{
_data.intValue = 0;
}
LogEntry::Arg::~Arg()
{
clear();
}
void LogEntry::Arg::clear()
{
if(_type == StringArgument)
{
delete _data.stringValue;
_data.stringValue = 0;
_type = IntegerArgument;
}
}
void LogEntry::Arg::setValue(dint i)
{
clear();
_type = IntegerArgument;
_data.intValue = i;
}
void LogEntry::Arg::setValue(duint i)
{
clear();
_type = IntegerArgument;
_data.intValue = i;
}
void LogEntry::Arg::setValue(long int i)
{
clear();
_type = IntegerArgument;
_data.intValue = i;
}
void LogEntry::Arg::setValue(long unsigned int i)
{
clear();
_type = IntegerArgument;
_data.intValue = i;
}
void LogEntry::Arg::setValue(duint64 i)
{
clear();
_type = IntegerArgument;
_data.intValue = dint64(i);
}
void LogEntry::Arg::setValue(dint64 i)
{
clear();
_type = IntegerArgument;
_data.intValue = i;
}
void LogEntry::Arg::setValue(ddouble d)
{
clear();
_type = FloatingPointArgument;
_data.floatValue = d;
}
void LogEntry::Arg::setValue(void const *p)
{
clear();
_type = IntegerArgument;
_data.intValue = dint64(p);
}
void LogEntry::Arg::setValue(char const *s)
{
clear();
_type = StringArgument;
_data.stringValue = new String(s);
}
void LogEntry::Arg::setValue(String const &s)
{
clear();
_type = StringArgument;
// Ensure a deep copy of the string is taken.
_data.stringValue = new String(s.data(), s.size());
}
void LogEntry::Arg::setValue(LogEntry::Arg::Base const &arg)
{
switch(arg.logEntryArgType())
{
case IntegerArgument:
setValue(arg.asInt64());
break;
case FloatingPointArgument:
setValue(arg.asDouble());
break;
case StringArgument:
setValue(arg.asText()); // deep copy
break;
}
}
LogEntry::Arg &LogEntry::Arg::operator = (Arg const &other)
{
clear();
if(other._type == StringArgument)
{
setValue(*other._data.stringValue); // deep copy
}
else
{
_type = other._type;
_data = other._data;
}
return *this;
}
ddouble LogEntry::Arg::asNumber() const
{
if(_type == IntegerArgument)
{
return ddouble(_data.intValue);
}
else if(_type == FloatingPointArgument)
{
return _data.floatValue;
}
throw TypeError("Log::Arg::asNumber", "String argument cannot be used as a number");
}
String LogEntry::Arg::asText() const
{
if(_type == StringArgument)
{
return *_data.stringValue;
}
else if(_type == IntegerArgument)
{
return String::number(_data.intValue);
}
else if(_type == FloatingPointArgument)
{
return String::number(_data.floatValue);
}
throw TypeError("Log::Arg::asText", "Number argument cannot be used a string");
}
void LogEntry::Arg::operator >> (Writer &to) const
{
to << dbyte(_type);
switch(_type)
{
case IntegerArgument:
to << _data.intValue;
break;
case FloatingPointArgument:
to << _data.floatValue;
break;
case StringArgument:
to << *_data.stringValue;
break;
}
}
void LogEntry::Arg::operator << (Reader &from)
{
if(_type == StringArgument) delete _data.stringValue;
from.readAs<dbyte>(_type);
switch(_type)
{
case IntegerArgument:
from >> _data.intValue;
break;
case FloatingPointArgument:
from >> _data.floatValue;
break;
case StringArgument:
_data.stringValue = new String;
from >> *_data.stringValue;
break;
}
}
LogEntry::Arg *LogEntry::Arg::newFromPool()
{
Arg *arg = argPool.take();
if(arg) return arg;
// Need a new one.
return new LogEntry::Arg;
}
void LogEntry::Arg::returnToPool(Arg *arg)
{
arg->clear();
argPool.put(arg);
}
LogEntry::LogEntry() : _metadata(0), _sectionDepth(0), _disabled(true)
{}
LogEntry::LogEntry(duint32 metadata, String const §ion, int sectionDepth, String const &format, Args args)
: _metadata(metadata)
, _section(section)
, _sectionDepth(sectionDepth)
, _format(format)
, _disabled(false)
, _args(args)
{
if(!LogBuffer::appBuffer().isEnabled(metadata))
{
_disabled = true;
}
}
LogEntry::LogEntry(LogEntry const &other, Flags extraFlags)
: Lockable()
, ISerializable()
, _when(other._when)
, _metadata(other._metadata)
, _section(other._section)
, _sectionDepth(other._sectionDepth)
, _format(other._format)
, _defaultFlags(other._defaultFlags | extraFlags)
, _disabled(other._disabled)
{
DENG2_FOR_EACH_CONST(Args, i, other._args)
{
Arg *a = Arg::newFromPool();
*a = **i;
_args.append(a);
}
}
LogEntry::~LogEntry()
{
DENG2_GUARD(this);
// Put the arguments back to the shared pool.
for(Args::iterator i = _args.begin(); i != _args.end(); ++i)
{
Arg::returnToPool(*i);
}
}
LogEntry::Flags LogEntry::flags() const
{
return _defaultFlags;
}
String LogEntry::asText(Flags const &formattingFlags, int shortenSection) const
{
DENG2_GUARD(this);
/// @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.testFlag(Styled)) output << TEXT_STYLE_LOG_TIME;
output << _when.asText(Date::BuildNumberAndSecondsSinceStart) << " ";
if(!flags.testFlag(OmitDomain))
{
QChar dc = (_metadata & Resource? 'R' :
_metadata & Map? 'M' :
_metadata & Script? 'S' :
_metadata & GL? 'G' :
_metadata & Audio? 'A' :
_metadata & Input? 'I' :
_metadata & Network? 'N' : ' ');
if(_metadata & Dev)
{
if(dc != ' ')
dc = dc.toLower();
else
dc = '-'; // Generic developer message
}
if(!flags.testFlag(Styled))
{
output << dc;
}
else
{
output << _E(s)_E(F)_E(m) << dc << _E(.) << " ";
}
}
if(!flags.testFlag(OmitLevel))
{
if(!flags.testFlag(Styled))
{
char const *levelNames[] = {
"", // not used
"(vv)",
"(v)",
"",
"(i)",
"(WRN)",
"(ERR)",
"(!!!)"
};
output << qSetPadChar(' ') << qSetFieldWidth(5)
<< levelNames[level()] << qSetFieldWidth(0) << " ";
}
else
{
char const *levelNames[] = {
"", // not used
"XVerbose",
"Verbose",
"",
"Note!",
"Warning",
"ERROR",
"FATAL!"
};
output << "\t"
<< (level() >= LogEntry::Warning? TEXT_STYLE_MAJOR_SECTION :
level() <= LogEntry::Verbose? TEXT_STYLE_MINOR_SECTION :
TEXT_STYLE_SECTION)
<< levelNames[level()] << "\t";
}
}
}
// Section name.
if(!flags.testFlag(OmitSection) && !_section.empty())
{
if(flags.testFlag(Styled))
{
output << TEXT_MARK_INDENT
<< (level() >= LogEntry::Warning? TEXT_STYLE_MAJOR_SECTION :
level() <= LogEntry::Verbose? TEXT_STYLE_MINOR_SECTION :
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_MARK_INDENT
<< (level() >= LogEntry::Warning? TEXT_STYLE_MAJOR_MESSAGE :
level() <= LogEntry::Verbose? TEXT_STYLE_MINOR_MESSAGE :
TEXT_STYLE_MESSAGE);
}
// Message text with the arguments formatted.
if(_args.empty())
{
output << _format; // Verbatim.
}
else
{
String::PatternArgs patArgs;
DENG2_FOR_EACH_CONST(Args, i, _args) patArgs << *i;
output << _format % patArgs;
}
return result;
}
void LogEntry::operator >> (Writer &to) const
{
to << _when
<< _section
<< _format
<< duint32(_metadata)
<< dbyte(_sectionDepth)
<< duint32(_defaultFlags);
to.writeObjects(_args);
}
void LogEntry::operator << (Reader &from)
{
foreach(Arg *a, _args) delete a;
_args.clear();
from >> _when
>> _section
>> _format;
if(from.version() >= DENG2_PROTOCOL_1_14_0_BUILD_1099)
{
// This version adds context information to the entry.
from.readAs<duint32>(_metadata);
}
else
{
dbyte oldLevel;
from >> oldLevel;
_metadata = oldLevel; // lacks audience information
}
from.readAs<dbyte>(_sectionDepth)
.readAs<duint32>(_defaultFlags)
.readObjects<Arg>(_args);
}
QTextStream &operator << (QTextStream &stream, LogEntry::Arg const &arg)
{
switch(arg.type())
{
case LogEntry::Arg::IntegerArgument:
stream << arg.intValue();
break;
case LogEntry::Arg::FloatingPointArgument:
stream << arg.floatValue();
break;
case LogEntry::Arg::StringArgument:
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(duint32 metadata, String const &format, LogEntry::Args arguments)
{
if(!LogBuffer::appBuffer().isEnabled(metadata))
{
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(metadata, context, depth, format, arguments);
// Add it to the application's buffer. The buffer gets ownership.
LogBuffer::appBuffer().add(entry);
return *entry;
}
static internal::Logs &theLogs()
{
if(!logsPtr.get()) logsPtr.reset(new internal::Logs);
return *logsPtr;
}
Log &Log::threadLog()
{
internal::Logs &logs = theLogs();
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;
logs.insert(thread, theLog);
return *theLog;
}
else
{
return *found.value();
}
}
void Log::disposeThreadLog()
{
internal::Logs &logs = theLogs();
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(duint32 metadata, String const &format)
: _metadata(metadata)
{
// Automatically set the Generic domain.
if(!(_metadata & LogEntry::DomainMask))
{
_metadata |= LogEntry::Generic;
}
_disabled = !LogBuffer::appBufferExists() ||
!LogBuffer::appBuffer().isEnabled(_metadata);
if(!_disabled)
{
_format = format;
}
}
} // namespace de