-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoggerUnit.pas
116 lines (83 loc) · 2.28 KB
/
LoggerUnit.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
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.
unit LoggerUnit;
interface
type
TLogLevel = (llError, llWarn, llInfo, llDebug);
ILogAppender = interface
procedure DoAppend(const Level: TLogLevel; const strMsg: string);
end;
TLogger = class
private
m_Appender: ILogAppender;
constructor FCreate;
public
constructor Create;
destructor Destroy; override;
class function GetInstance: TLogger;
class procedure FreeInstance; reintroduce;
procedure Log(const Level: TLogLevel; const strMsg: string);
procedure Error(const strMsg: string);
procedure Warn(const strMsg: string);
procedure Info(const strMsg: string);
procedure Debug(const strMsg: string);
property Appender: ILogAppender read m_Appender write m_Appender;
end;
implementation
uses
SysUtils;
var
g_Logger: TLogger = nil;
////////////////////////////////////////////////////////////////////////////////
// TLogger
constructor TLogger.Create;
begin
raise Exception.Create(ClassName + ' cannot be instaniated directly!');
end;
constructor TLogger.FCreate;
begin
inherited Create;
end;
destructor TLogger.Destroy;
begin
g_Logger := nil;
inherited;
end;
class function TLogger.GetInstance: TLogger;
begin
if (not Assigned(g_Logger)) then
g_Logger := TLogger.FCreate;
Result := g_Logger;
end;
class procedure TLogger.FreeInstance;
begin
g_Logger.Free;
end;
procedure TLogger.Log(const Level: TLogLevel; const strMsg: string);
begin
if (Assigned(m_Appender)) then
m_Appender.DoAppend(Level, strMsg);
end;
procedure TLogger.Error(const strMsg: string);
begin
Log(llError, strMsg);
end;
procedure TLogger.Warn(const strMsg: string);
begin
Log(llWarn, strMsg);
end;
procedure TLogger.Info(const strMsg: string);
begin
Log(llInfo, strMsg);
end;
procedure TLogger.Debug(const strMsg: string);
begin
Log(llDebug, strMsg);
end;
initialization
finalization
TLogger.FreeInstance;
end.