-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.pas
135 lines (111 loc) · 2.74 KB
/
Log.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
unit Log;
interface
uses
Console, Windows;
const
ServerColor = Console.Yellow;
ClientColor = Console.LightGreen;
ClientProxyColor = Console.Green;
ServerProxyColor = Console.Brown;
CRLF = #$D#$A;
procedure WriteLine(const Msg: String); overload;
procedure WriteLine(const Msg: String; const Args: array of const); overload;
procedure WriteLine(const Msg: String; Color: Byte); overload;
procedure WriteLine(const Msg: String; const Args: array of const; Color: Byte); overload;
procedure WriteError(const Msg: String); overload;
procedure WriteError(const Msg: String; const Args: array of const); overload;
procedure WriteToLogFile(const Msg: String);
procedure WriteToConsole(const Msg: String; Color: Byte);
var
LogToConsole: Boolean = False;
LogToFile: Boolean = False;
LogFileName: String = 'RtspProxy.log';
implementation
uses
SysUtils, Classes;
var
ConcoleLocker: TRTLCriticalSection;
ConcoleLockerInited: Boolean = False;
procedure Lock;
begin
if not ConcoleLockerInited then
begin
InitializeCriticalSection(ConcoleLocker);
ConcoleLockerInited := True;
end;
EnterCriticalSection(ConcoleLocker);
end;
procedure Unlock;
begin
LeaveCriticalSection(ConcoleLocker);
end;
procedure WriteToLogFile(const Msg: String);
var
S: String;
LogFile: THandle;
begin
S := Format('[%s] %s'#13#10, [FormatDateTime('dd-mm-yyyy hh:mm:ss:zzz', Now), Msg]);
if FileExists(LogFileName) then
begin
LogFile := FileOpen(LogFileName, fmOpenWrite);
FileSeek(LogFile, 0, soFromEnd);
end
else
LogFile := FileCreate(LogFileName);
FileWrite(LogFile, PChar(S)^, Length(S));
FileClose(LogFile);
end;
procedure WriteToConsole(const Msg: String; Color: Byte);
var
OldColor: Byte;
begin
OldColor := TextColor;
TextColor(Color);
WriteLn(Msg);
TextColor(OldColor);
end;
procedure WriteLine(const Msg: String);
begin
Lock;
try
if LogToConsole then
WriteLn(Msg);
if LogToFile then
WriteToLogFile(Msg);
finally
Unlock;
end;
end;
procedure WriteLine(const Msg: String; const Args: array of const);
begin
WriteLine(Format(Msg, Args));
end;
procedure WriteLine(const Msg: String; Color: Byte);
begin
Lock;
try
if LogToConsole then
WriteToConsole(Msg, Color);
if LogToFile then
WriteToLogFile(Msg);
finally
Unlock;
end;
end;
procedure WriteLine(const Msg: String; const Args: array of const; Color: Byte);
begin
WriteLine(Format(Msg, Args), Color);
end;
procedure WriteError(const Msg: String);
begin
WriteLine('ERROR: ' + Msg, Console.LightRed);
end;
procedure WriteError(const Msg: String; const Args: array of const);
begin
WriteError(Format(Msg, Args));
end;
initialization
finalization
if ConcoleLockerInited then
DeleteCriticalSection(ConcoleLocker);
end.