forked from HeliumProject/Foundation
/
Log.cpp
928 lines (768 loc) · 19.5 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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#include "FoundationPch.h"
#include "Log.h"
#include "Platform/Assert.h"
#include "Platform/Locks.h"
#include "Platform/Thread.h"
#include "Platform/File.h"
#include "Platform/Console.h"
#include "Platform/Encoding.h"
#include "Foundation/String.h"
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <fstream>
#include <iostream>
#include <map>
#if HELIUM_OS_WIN
# include <crtdbg.h>
# include <shlobj.h>
#endif
using namespace Helium;
using namespace Helium::Log;
static uint32_t g_LogFileCount = 20;
static Helium::Mutex g_Mutex;
typedef std::map<std::string, File*> M_Files;
class FileManager
{
private:
M_Files m_Files;
public:
FileManager()
{
}
~FileManager()
{
M_Files::const_iterator itr = m_Files.begin();
M_Files::const_iterator end = m_Files.end();
for ( ; itr != end; ++itr )
{
itr->second->Close();
delete itr->second;
}
m_Files.clear();
}
bool Opened(const std::string& fileName, File* f)
{
return m_Files.insert( M_Files::value_type (fileName, f) ).second;
}
File* Find(const std::string& fileName)
{
M_Files::const_iterator found = m_Files.find(fileName.c_str());
if (found != m_Files.end())
{
return found->second;
}
else
{
return NULL;
}
}
void Close(const std::string& fileName)
{
M_Files::iterator found = m_Files.find( fileName );
if (found != m_Files.end())
{
found->second->Close();
delete found->second;
m_Files.erase( found );
}
else
{
HELIUM_BREAK();
}
}
};
FileManager g_FileManager;
struct OutputFile
{
Stream m_StreamType;
int m_RefCount;
ThreadId m_ThreadId;
OutputFile()
: m_StreamType( Streams::Normal )
, m_RefCount( 0 )
, m_ThreadId()
{
}
};
typedef std::map< std::string, OutputFile > M_OutputFile;
static M_OutputFile g_TraceFiles;
static uint32_t g_Streams = Streams::Normal | Streams::Warning | Streams::Error;
static Level g_Level = Levels::Default;
static int g_Indent = 0;
static ListenerSignature::Event g_LogEvent;
void Log::Statement::ApplyIndent( const char* string, std::string& output )
{
if ( m_Indent > 0 )
{
char m_IndentString[64] = TXT( "" );
if(m_Indent >= sizeof(m_IndentString))
{
m_Indent = sizeof(m_IndentString)-1;
}
for (int i=0; i<m_Indent; i++)
{
m_IndentString[i] = ' ';
}
m_IndentString[m_Indent] = '\0';
// insert the indtent string after newlines and before non-newlines
const char* pos = string;
char previous = '\n';
for ( ; *pos != '\0'; previous = *pos++ )
{
if ( *pos == '\r' )
{
continue;
}
if ( previous == '\n' && *pos != '\n' )
{
// start of a new line, apply m_Indent
output += m_IndentString;
}
// copy the char to the statement
output += *pos;
}
}
else
{
output = string;
}
}
void Log::AddListener(const ListenerSignature::Delegate& listener)
{
Helium::MutexScopeLock mutex (g_Mutex);
g_LogEvent.Add(listener);
}
void Log::RemoveListener(const ListenerSignature::Delegate& listener)
{
Helium::MutexScopeLock mutex (g_Mutex);
g_LogEvent.Remove(listener);
}
void Redirect(const std::string& fileName, const char* str, bool stampNewLine = true )
{
File* f = g_FileManager.Find(fileName);
if (f)
{
size_t length = 0;
size_t count = ( StringLength( str ) + 128 );
char* temp = (char*)alloca( sizeof(char) * count );
if ( stampNewLine )
{
#if defined(HELIUM_OS_WIN)
_timeb currentTime;
_ftime( ¤tTime );
#else
struct timeb currentTime;
ftime( ¤tTime );
#endif
uint32_t time = (uint32_t) currentTime.time;
uint32_t milli = currentTime.millitm;
uint32_t sec = time % 60; time /= 60;
uint32_t min = time % 60; time -= currentTime.timezone; time /= 60;
time += currentTime.dstflag ? 1 : 0;
uint32_t hour = time % 24;
length = StringPrint( temp, count, TXT("[%02d:%02d:%02d.%03d TID:%d] %s"), hour, min, sec, milli, Thread::GetCurrentId(), str );
}
else
{
length = StringPrint( temp, count, TXT("%s"), str );
}
f->Write( temp, length );
f->Flush();
}
}
bool AddFile( M_OutputFile& files, const std::string& fileName, Stream stream, ThreadId threadId, bool append )
{
Helium::MutexScopeLock mutex (g_Mutex);
M_OutputFile::iterator found = files.find( fileName );
if ( found != files.end() )
{
if ( found->second.m_StreamType != stream )
{
HELIUM_BREAK(); // trying to add the same file, but with a different stream type
}
if ( found->second.m_ThreadId != threadId )
{
HELIUM_BREAK(); // trying to add the same file with a different thread id
}
found->second.m_RefCount++; // another reference
}
else
{
if (fileName != TXT( "" ))
{
File* f = new File;
if ( f->Open( fileName.c_str(), FileModes::Write ) )
{
if ( append )
{
f->Seek( 0, SeekOrigins::End );
}
g_FileManager.Opened( fileName, f );
OutputFile info;
info.m_StreamType = stream;
info.m_RefCount = 1;
info.m_ThreadId = threadId;
files[ fileName ] = info;
return true;
}
else
{
delete f;
f = NULL;
}
}
}
return false;
}
void RemoveFile( M_OutputFile& files, const std::string& fileName )
{
Helium::MutexScopeLock mutex (g_Mutex);
M_OutputFile::iterator found = files.find( fileName );
if ( found != files.end() )
{
found->second.m_RefCount--;
if ( found->second.m_RefCount == 0 )
{
g_FileManager.Close( fileName );
files.erase( found );
}
}
}
bool Log::AddTraceFile( const std::string& fileName, Stream stream, ThreadId threadId, bool append )
{
return AddFile( g_TraceFiles, fileName, stream, threadId, append );
}
void Log::RemoveTraceFile( const std::string& fileName )
{
RemoveFile( g_TraceFiles, fileName );
}
void Log::Indent(int col)
{
if ( Thread::IsMain() )
{
g_Indent += (col < 0 ? 2 : col);
}
}
void Log::UnIndent(int col)
{
if ( Thread::IsMain() )
{
g_Indent -= (col < 0 ? 2 : col);
if (g_Indent < 0)
{
g_Indent = 0;
}
}
}
Level Log::GetLevel()
{
return g_Level;
}
void Log::SetLevel(Level level)
{
g_Level = level;
}
bool Log::IsStreamEnabled( Stream stream )
{
return ( g_Streams & stream ) == stream;
}
void Log::EnableStream( Stream stream, bool enable )
{
if ( enable )
{
g_Streams |= stream;
}
else
{
g_Streams &= ~stream;
}
}
ConsoleColor Log::GetStreamColor( Log::Stream stream )
{
switch (stream)
{
case Streams::Normal:
return ConsoleColors::None;
case Streams::Debug:
return ConsoleColors::Aqua;
case Streams::Profile:
return ConsoleColors::Green;
case Streams::Warning:
return ConsoleColors::Yellow;
case Streams::Error:
return ConsoleColors::Red;
default:
HELIUM_BREAK();
break;
}
return ConsoleColors::None;
}
void Log::LockMutex()
{
g_Mutex.Lock();
}
void Log::UnlockMutex()
{
g_Mutex.Unlock();
}
void Log::PrintString(const char* string, Stream stream, Level level, ConsoleColor color, int indent, char* output, uint32_t outputSize)
{
Helium::MutexScopeLock mutex (g_Mutex);
// check trace files
bool trace = false;
M_OutputFile::iterator itr = g_TraceFiles.begin();
M_OutputFile::iterator end = g_TraceFiles.end();
for( ; itr != end; ++itr )
{
if ( ( (*itr).second.m_StreamType & stream ) == stream
&& ( (*itr).second.m_ThreadId == ThreadId () || (*itr).second.m_ThreadId == Thread::GetCurrentId() ) )
{
trace = true;
}
}
// determine if we should be displayed
bool display = ( g_Streams & stream ) == stream && level <= g_Level;
// check for nothing to do
if ( trace || display || output )
{
if ( indent < 0 )
{
indent = g_Indent;
}
// the statement
Statement statement ( string, stream, level, indent );
// construct the print statement
ListenerArgs args ( statement );
// is this statement to be output via normal channels
if ( display )
{
// raise the printing event
g_LogEvent.Raise( args );
}
// only process this string if it was not handled by a handler
if ( !args.m_Skip )
{
// apply indentation
statement.m_String.clear();
statement.ApplyIndent( string, statement.m_String );
// output to screen window
if ( display )
{
// deduce the color if we were told to do so
if ( color == ConsoleColors::None )
{
color = GetStreamColor( stream );
}
// print the statement to the window
Helium::PrintString(color, stream == Streams::Error ? stderr : stdout, statement.m_String);
}
// send the text to the debugger, if no debugger nothing happens
#if HELIUM_OS_WIN
HELIUM_CONVERT_TO_WIDE( statement.m_String.c_str(), convertedStatement );
OutputDebugStringW( convertedStatement );
#endif
// output to trace file(s)
static bool stampNewLine = true;
itr = g_TraceFiles.begin();
end = g_TraceFiles.end();
for( ; itr != end; ++itr )
{
if ( ( (*itr).second.m_StreamType & stream ) == stream
&& ( (*itr).second.m_ThreadId == ThreadId () || (*itr).second.m_ThreadId == Thread::GetCurrentId() ) )
{
Redirect( (*itr).first, statement.m_String.c_str(), stampNewLine );
}
}
// update stampNewLine
if ( !statement.m_String.empty() )
{
stampNewLine = ( *statement.m_String.rbegin() == '\n' ) ? true : false ;
}
// output to buffer
if (output && outputSize > 0)
{
CopyString( output, outputSize - 1, statement.m_String.c_str() );
}
}
}
}
void Log::PrintStatement(const Statement& statement)
{
Helium::MutexScopeLock mutex (g_Mutex);
PrintString( statement.m_String.c_str(), statement.m_Stream, statement.m_Level, GetStreamColor( statement.m_Stream ), statement.m_Indent );
}
void Log::PrintStatements(const std::vector< Statement >& statements, uint32_t streamFilter)
{
Helium::MutexScopeLock mutex (g_Mutex);
std::vector< Statement >::const_iterator itr = statements.begin();
std::vector< Statement >::const_iterator end = statements.end();
for ( ; itr != end; ++itr )
{
if ( itr->m_Stream & streamFilter )
{
PrintStatement( *itr );
}
}
}
void Log::PrintColor(ConsoleColor color, const char* fmt, ...)
{
Helium::MutexScopeLock mutex (g_Mutex);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, fmt, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Normal, Levels::Default, color);
va_end(args);
}
void Log::Print(const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, fmt, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Normal, Levels::Default, Log::GetStreamColor( Streams::Normal ));
va_end(args);
}
void Log::Print(Level level, const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, fmt, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Normal, level, Log::GetStreamColor( Streams::Normal ));
va_end(args);
}
void Log::Debug(const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "DEBUG: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Debug, Levels::Default, Log::GetStreamColor( Streams::Debug ), 0);
va_end(args);
}
void Log::Debug(Level level, const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "DEBUG: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Debug, level, Log::GetStreamColor( Streams::Debug ), 0);
va_end(args);
}
void Log::Profile(const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "PROFILE: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Profile, Levels::Default, Log::GetStreamColor( Streams::Profile ), 0);
va_end(args);
}
void Log::Profile(Level level, const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "PROFILE: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Profile, level, Log::GetStreamColor( Streams::Profile ), 0);
va_end(args);
}
void Log::Warning(const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "WARNING: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Warning, Levels::Default, Log::GetStreamColor( Streams::Warning ), 0);
va_end(args);
}
void Log::Warning(Level level, const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "WARNING: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Warning, level, Log::GetStreamColor( Streams::Warning ), 0);
va_end(args);
}
void Log::Error(const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "ERROR: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Error, Levels::Default, Log::GetStreamColor( Streams::Error ), 0);
va_end(args);
}
void Log::Error(Level level, const char *fmt,...)
{
Helium::MutexScopeLock mutex (g_Mutex);
static char format[MAX_PRINT_SIZE];
StringPrint(format, TXT( "ERROR: " ));
AppendString(format, fmt);
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Error, level, Log::GetStreamColor( Streams::Error ), 0);
va_end(args);
}
Log::Heading::Heading(const char *fmt, ...)
{
Helium::MutexScopeLock mutex (g_Mutex);
// do a basic print
va_list args;
va_start(args, fmt);
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, fmt, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
PrintString(string, Streams::Normal, Levels::Default, Log::GetStreamColor( Streams::Normal ));
va_end(args);
// now indent
Indent();
}
Log::Heading::~Heading()
{
Helium::MutexScopeLock mutex (g_Mutex);
// unindent
UnIndent();
}
std::vector<std::string> g_OutlineState;
Log::Bullet::Bullet(const char *fmt, ...)
: m_Stream( Streams::Normal )
, m_Level( Log::Levels::Default )
, m_Valid( fmt != NULL )
{
if (m_Valid)
{
Helium::MutexScopeLock mutex (g_Mutex);
va_list args;
va_start(args, fmt);
CreateBullet( fmt, args );
va_end(args);
}
}
Log::Bullet::Bullet(Stream stream, Log::Level level, const char *fmt, ...)
: m_Stream( stream )
, m_Level( level )
, m_Valid( fmt != NULL )
{
if (m_Valid)
{
Helium::MutexScopeLock mutex (g_Mutex);
va_list args;
va_start(args, fmt);
CreateBullet( fmt, args );
va_end(args);
}
}
Log::Bullet::~Bullet()
{
if (m_Valid)
{
Helium::MutexScopeLock mutex (g_Mutex);
// this gates the output to the console for streams and levels that the user did not elect to see on in the console
bool print = ( ( g_Streams & m_Stream ) == m_Stream ) && ( m_Level <= g_Level );
if ( print )
{
// unindent
UnIndent();
if (g_Indent == 1)
{
UnIndent(1);
}
else
{
Indent(1);
}
}
g_OutlineState.pop_back();
}
}
void Log::Bullet::CreateBullet(const char *fmt, va_list args)
{
static char delims[] = { 'o', '*', '>', '-' };
// this gates the output to the console for streams and levels that the user did not elect to see on in the console
bool print = ( ( g_Streams & m_Stream ) == m_Stream ) && ( m_Level <= g_Level );
if ( print )
{
if (g_Indent == 0)
{
Indent(1);
}
else
{
UnIndent(1);
}
}
// build the format string
static char format[MAX_PRINT_SIZE];
if (g_Indent == 1)
{
format[0] = delims[ 0 ];
format[1] = ' ';
format[2] = '\0';
}
else
{
format[0] = delims[ (g_Indent-1 + sizeof(delims)) % sizeof(delims) ];
format[1] = ' ';
format[2] = '\0';
}
AppendString(format, fmt);
// format the bullet string
static char string[MAX_PRINT_SIZE];
int size = StringPrintArgs(string, format, args);
string[ sizeof(string)/sizeof(string[0]) - 1] = 0;
HELIUM_ASSERT(size >= 0);
// do the print and capture the output
static char output[MAX_PRINT_SIZE];
if (g_Indent == 1)
{
PrintString( TXT( "\n" ), m_Stream, m_Level, Log::GetStreamColor( m_Stream ), -1, output, sizeof( output ) );
}
PrintString( string, m_Stream, m_Level, Log::GetStreamColor( m_Stream ), -1, output, sizeof( output ) );
// push state
g_OutlineState.push_back( output );
if ( print )
{
// now indent
Indent();
}
}
std::string Log::GetOutlineState()
{
Helium::MutexScopeLock mutex (g_Mutex);
std::string state;
std::vector<std::string>::const_iterator itr = g_OutlineState.begin();
std::vector<std::string>::const_iterator end = g_OutlineState.end();
for ( ; itr != end; ++itr )
{
if ( itr == g_OutlineState.begin() )
{
state = *itr;
}
else
{
state += *itr;
}
}
return state;
}
Listener::Listener( uint32_t throttle, uint32_t* errorCount, uint32_t* warningCount, std::vector< Statement >* consoleOutput )
: m_Thread( Thread::GetCurrentId() )
, m_Throttle( throttle )
, m_WarningCount( warningCount )
, m_ErrorCount( errorCount )
, m_LogOutput( consoleOutput )
{
Start();
}
Listener::~Listener()
{
Stop();
}
void Listener::Start()
{
Log::AddListener( Log::ListenerSignature::Delegate( this, &Listener::Print ) );
}
void Listener::Stop()
{
Log::RemoveListener( Log::ListenerSignature::Delegate( this, &Listener::Print ) );
}
void Listener::Dump(bool stop)
{
if ( stop )
{
Stop();
}
if ( m_LogOutput )
{
Log::PrintStatements( *m_LogOutput );
}
}
uint32_t Listener::GetWarningCount()
{
return *m_WarningCount;
}
uint32_t Listener::GetErrorCount()
{
return *m_ErrorCount;
}
void Listener::Print( ListenerArgs& args )
{
if ( m_Thread == Thread::GetCurrentId() )
{
if ( args.m_Statement.m_Stream == Log::Streams::Warning && m_WarningCount )
{
(*m_WarningCount)++;
}
if ( args.m_Statement.m_Stream == Log::Streams::Error && m_ErrorCount )
{
(*m_ErrorCount)++;
}
if ( m_LogOutput )
{
m_LogOutput->push_back( args.m_Statement );
}
if ( m_Throttle & args.m_Statement.m_Stream )
{
args.m_Skip = true;
}
}
}