-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPasfmt.Log.pas
234 lines (179 loc) · 6.22 KB
/
Pasfmt.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
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
unit Pasfmt.Log;
interface
type
TLogLevel = (llDebug, llInfo, llWarn, llError, llNone);
ILogger = interface
['{722CD5E4-CD6F-4DDA-85E7-C598AFB4E314}']
procedure Debug(Msg: string); overload;
procedure Debug(Msg: string; Args: array of const); overload;
procedure Warn(Msg: string); overload;
procedure Warn(Msg: string; Args: array of const); overload;
procedure Error(Msg: string); overload;
procedure Error(Msg: string; Args: array of const); overload;
procedure Info(Msg: string); overload;
procedure Info(Msg: string; Args: array of const); overload;
procedure SetLogLevel(LogLevel: TLogLevel);
end;
function Log: ILogger;
procedure FinalizeLog;
implementation
uses
ToolsAPI,
System.SysUtils,
Pasfmt.Settings;
type
TMessageWindowLogger = class(TNotifierObject, ILogger, IOTAMessageNotifier)
private
FGroup: IOTAMessageGroup;
FLogLevel: TLogLevel;
FNotifierIndex: Integer;
procedure Log(Msg: string; Prefix: string);
procedure EnsureGroup;
procedure MessageGroupAdded(const Group: IOTAMessageGroup);
procedure MessageGroupDeleted(const Group: IOTAMessageGroup);
public
constructor Create;
destructor Destroy; override;
procedure Cleanup;
procedure Debug(Msg: string); overload;
procedure Debug(Msg: string; Args: array of const); overload;
procedure Warn(Msg: string); overload;
procedure Warn(Msg: string; Args: array of const); overload;
procedure Error(Msg: string); overload;
procedure Error(Msg: string; Args: array of const); overload;
procedure Info(Msg: string); overload;
procedure Info(Msg: string; Args: array of const); overload;
procedure SetLogLevel(LogLevel: TLogLevel);
end;
//______________________________________________________________________________________________________________________
var
GLog: ILogger;
GFinalized: Boolean;
function Log: ILogger;
begin
if not Assigned(GLog) then begin
GLog := TMessageWindowLogger.Create;
GLog.SetLogLevel(PasfmtSettings.LogLevel);
end;
Result := GLog;
end;
//______________________________________________________________________________________________________________________
procedure FinalizeLog;
begin
GFinalized := True;
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.Cleanup;
begin
(BorlandIDEServices as IOTAMessageServices).RemoveNotifier(FNotifierIndex);
end;
constructor TMessageWindowLogger.Create;
begin
FLogLevel := llDebug;
FNotifierIndex := (BorlandIDEServices as IOTAMessageServices).AddNotifier(Self);
end;
//______________________________________________________________________________________________________________________
destructor TMessageWindowLogger.Destroy;
begin
if Assigned(FGroup) then begin
(BorlandIDEServices as IOTAMessageServices).RemoveMessageGroup(FGroup);
end;
inherited;
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.EnsureGroup;
begin
if not Assigned(FGroup) then begin
FGroup := (BorlandIDEServices as IOTAMessageServices).AddMessageGroup('Pasfmt');
(BorlandIDEServices as IOTAMessageServices).ClearMessageGroup(FGroup);
end;
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.Debug(Msg: string);
begin
if FLogLevel > llDebug then
Exit;
Log(Msg, 'DEBUG');
end;
procedure TMessageWindowLogger.Debug(Msg: string; Args: array of const);
begin
if FLogLevel > llDebug then
Exit;
Debug(Format(Msg, Args));
end;
procedure TMessageWindowLogger.Error(Msg: string);
begin
if FLogLevel > llError then
Exit;
Log(Msg, 'ERROR');
end;
procedure TMessageWindowLogger.Error(Msg: string; Args: array of const);
begin
if FLogLevel > llError then
Exit;
Error(Format(Msg, Args));
end;
procedure TMessageWindowLogger.Info(Msg: string);
begin
if FLogLevel > llInfo then
Exit;
Log(Msg, 'INFO');
end;
procedure TMessageWindowLogger.Info(Msg: string; Args: array of const);
begin
if FLogLevel > llInfo then
Exit;
Info(Format(Msg, Args));
end;
procedure TMessageWindowLogger.Warn(Msg: string);
begin
if FLogLevel > llWarn then
Exit;
Log(Msg, 'WARN');
end;
procedure TMessageWindowLogger.Warn(Msg: string; Args: array of const);
begin
if FLogLevel > llWarn then
Exit;
Warn(Format(Msg, Args));
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.Log(Msg: string; Prefix: string);
var
Dummy: Pointer;
begin
if GFinalized then begin
// When the IDE starts to close, the IDE's services become increasingly unreliable.
// In particular, the message box services start throwing AVs after a certain point in the closing process,
// so we can no longer use them to log messages. In this circumstance we just swallow the messages for now.
Exit;
end;
EnsureGroup;
(BorlandIDEServices as IOTAMessageServices).AddToolMessage('', Msg, Prefix, 0, 0, nil, Dummy, FGroup);
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.SetLogLevel(LogLevel: TLogLevel);
begin
FLogLevel := LogLevel;
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.MessageGroupAdded(const Group: IOTAMessageGroup);
begin
// No-op to implement interface
end;
//______________________________________________________________________________________________________________________
procedure TMessageWindowLogger.MessageGroupDeleted(const Group: IOTAMessageGroup);
begin
if Assigned(FGroup) and (Group = FGroup) then begin
// The user has closed the message group
FGroup := nil;
end;
end;
//______________________________________________________________________________________________________________________
initialization
finalization
if Assigned(GLog) then begin
TMessageWindowLogger(GLog).Cleanup;
GLog := nil;
end;
end.