forked from danieleteti/loggerpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggerPro.ConsoleAppender.pas
150 lines (130 loc) · 4.51 KB
/
LoggerPro.ConsoleAppender.pas
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
{ <@abstract(The unit to include if you want to use @link(TLoggerProConsoleAppender))
@author(Daniele Teti)
@author(Fulgan - https://github.com/Fulgan) }
unit LoggerPro.ConsoleAppender;
interface
uses
Classes,
SysUtils,
LoggerPro,
SyncObjs;
type
/// <summary>TLoggerProConsoleAppender
/// This class creates a new console (if needed) when setup.
/// Warning: If the application is of type GUI and it is started from a console,
/// this will NOT log to the calling console but create a new one.
/// This is because the way cmd.exe works: if the application is of type GUI,
/// then is immediately detaches from cmd.exe which doesn't wait. This means that
/// there is never a console to attach to except if a new console is created
/// elsewhere in the app.
/// In case this class is used from a console, then there is no guarantee that
/// the messages will be
/// displayed in chronological order.
/// </summary>
TLoggerProConsoleAppender = class(TLoggerProAppenderBase)
strict private
class var FLock: TCriticalSection; // used to prevent syncroneous operations to run at the same time
class var FConsoleAllocated: Int64; // used to ensure one and only one console is created
class constructor Create; // allocate global vars
class destructor Destroy;
protected
procedure SetColor(const Color: Integer);
public
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
end;
// for some reason, AttachConsole has been left out of Winapi.windows.pas
function AttachConsole(PID: Cardinal): LongBool; stdcall;
implementation
{ TLoggerProConsoleAppender }
uses
Winapi.Windows,
Winapi.Messages;
// for some reason, AttachConsole has been left out of Winapi.windows.pas
const
ATTACH_PARENT_PROCESS = Cardinal(-1);
function AttachConsole; external kernel32 name 'AllocConsole';
const
{ FOREGROUND COLORS - CAN BE COMBINED }
FOREGROUND_BLUE = 1; { text color blue. }
FOREGROUND_GREEN = 2; { text color green }
FOREGROUND_RED = 4; { text color red }
FOREGROUND_INTENSITY = 8; { text color is intensified }
{ BACKGROUND COLORS - CAN BE COMBINED }
BACKGROUND_BLUE = $10; { background color blue }
BACKGROUND_GREEN = $20; { background color green }
BACKGROUND_RED = $40; { background color red. }
BACKGROUND_INTENSITY = $80; { background color is intensified }
procedure TLoggerProConsoleAppender.SetColor(const Color: Integer);
begin
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Color);
end;
procedure TLoggerProConsoleAppender.Setup;
begin
inherited;
if TInterlocked.read(TLoggerProConsoleAppender.FConsoleAllocated) < 2 then
begin
TLoggerProConsoleAppender.FLock.Enter;
try
if TInterlocked.Increment(TLoggerProConsoleAppender.FConsoleAllocated) = 1 then
begin
// Attempt to attach to the parent (if there is already a console allocated)
if not IsConsole then
begin
if not AttachConsole(ATTACH_PARENT_PROCESS) then
AllocConsole; // No console allocated, create a new one
end;
TInterlocked.Increment(TLoggerProConsoleAppender.FConsoleAllocated);
end;
finally
TLoggerProConsoleAppender.FLock.Leave;
end;
end;
end;
procedure TLoggerProConsoleAppender.TearDown;
begin
end;
procedure TLoggerProConsoleAppender.WriteLog(const aLogItem: TLogItem);
var
lText: string;
lColor: Integer;
begin
lColor := FOREGROUND_GREEN; // Avoid W1030
case aLogItem.LogType of
TLogType.Debug:
lColor := FOREGROUND_GREEN;
TLogType.Info:
lColor := FOREGROUND_BLUE or FOREGROUND_GREEN or FOREGROUND_RED;
TLogType.Warning:
lColor := FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_INTENSITY;
TLogType.Error:
lColor := FOREGROUND_RED or FOREGROUND_INTENSITY;
TLogType.Fatal:
lColor := FOREGROUND_RED or FOREGROUND_BLUE or FOREGROUND_INTENSITY;
end;
lText := FormatLog(aLogItem);
TLoggerProConsoleAppender.FLock.Enter;
try
SetColor(lColor);
Writeln(lText);
finally
TLoggerProConsoleAppender.FLock.Leave;
end;
end;
class constructor TLoggerProConsoleAppender.Create;
begin
TLoggerProConsoleAppender.FLock := TCriticalSection.Create;
TLoggerProConsoleAppender.FConsoleAllocated := 0;
end;
class destructor TLoggerProConsoleAppender.Destroy;
begin
// make sure all code
try
TLoggerProConsoleAppender.FLock.Enter;
FreeAndNil(TLoggerProConsoleAppender.FLock);
except
// No exception checking here or the app might blow up with a RTE 217
end;
end;
end.