-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSystem.LogEvents.pas
More file actions
354 lines (304 loc) · 8.38 KB
/
System.LogEvents.pas
File metadata and controls
354 lines (304 loc) · 8.38 KB
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
{ *************************************************************************** }
{ }
{ }
{ Copyright (C) Amarildo Lacerda }
{ }
{ https://github.com/amarildolacerda }
{ }
{ }
{ *************************************************************************** }
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ *************************************************************************** }
unit System.LogEvents;
interface
Uses {$IFDEF FMX} FMX.Forms, {$ELSE} VCL.Forms, {$ENDIF} System.Classes,
System.Generics.Collections, System.SysUtils,
System.SyncObjs, System.LogEvents.Progress;
type
TLogListItem = class
private
FonErro: TLogListEvent;
Finstancia: TObject;
FIdent: Integer;
FOnProgress: TLogProgressEvent;
procedure SetonErro(const Value: TLogListEvent);
procedure Setinstancia(const Value: TObject);
procedure SetIdent(const Value: Integer);
procedure SetOnProgress(const Value: TLogProgressEvent);
published
public
property Ident: Integer read FIdent write SetIdent;
property instancia: TObject read Finstancia write Setinstancia;
property OnErro: TLogListEvent read FonErro write SetonErro;
property OnProgress: TLogProgressEvent read FOnProgress write SetOnProgress;
end;
TLogListItems = class(TObjectList<TLogListItem>)
private
FLock: TCriticalSection;
FBuffer: AnsiString;
FMostraDataHora: boolean;
FEnabled: boolean;
FSyncronized: boolean;
procedure SetMostraDataHora(const Value: boolean);
procedure SetEnabled(const Value: boolean);
procedure DoProcessProc(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double = 0;
nInteracoes: Integer = 1);
procedure SetSyncronized(const Value: boolean);
public
property Enabled: boolean read FEnabled write SetEnabled;
property Syncronized: boolean read FSyncronized write SetSyncronized;
property MostraDataHora: boolean read FMostraDataHora
write SetMostraDataHora;
procedure register(sender: TObject; event: TLogListEvent;
identific: Integer); overload;
procedure register(sender: TObject; event: TLogProgressEvent;
identific: Integer); overload;
procedure unregister(sender: TObject);
procedure DoErro(sender: TObject; identific: Integer; s: string;
nInteracoes: Integer = 1);
procedure DoMsg(sender: TObject; identific: Integer; s: string;
nInteracoes: Integer = 1);
procedure DoProgress(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double = 0;
nInteracoes: Integer = 1);
procedure SetMax(FValue: Integer);
procedure Log(texto: string);
constructor create();
destructor Destroy; override;
procedure Run(proc: TProc);
end;
var
LogEvents: TLogListItems;
implementation
{$IF CompilerVersion>28.0}
uses System.Threading;
{$ENDIF}
procedure TLogListItem.SetIdent(const Value: Integer);
begin
FIdent := Value;
end;
procedure TLogListItem.Setinstancia(const Value: TObject);
begin
Finstancia := Value;
end;
procedure TLogListItem.SetonErro(const Value: TLogListEvent);
begin
FonErro := Value;
end;
procedure TLogListItem.SetOnProgress(const Value: TLogProgressEvent);
begin
FOnProgress := Value;
end;
{ TLogListItems }
constructor TLogListItems.create();
begin
inherited create;
FSyncronized := true;
FMostraDataHora := true;
FLock := TCriticalSection.create;
Enabled := true;
end;
destructor TLogListItems.Destroy;
begin
freeAndNil(FLock);
inherited;
end;
procedure TLogListItems.DoErro(sender: TObject; identific: Integer; s: string;
nInteracoes: Integer = 1);
begin // compatibilidade
DoMsg(sender, identific, s, nInteracoes);
end;
procedure TLogListItems.DoMsg(sender: TObject; identific: Integer; s: string;
nInteracoes: Integer);
var
it: TLogListItem;
i, x: Integer;
h, buf: AnsiString;
begin
Run(
procedure
var
i: Integer;
begin
h := '';
try
if not Enabled then
exit;
buf := FBuffer;
FBuffer := '';
x := 0;
if FMostraDataHora then
h := FormatDateTime('DD/MM/YY hh:mm:ss', now) + ' ';
FBuffer := '';
for i := 0 to Count - 1 do
begin
it := TLogListItem(items[i]);
if (identific = 0) or (it.Ident = identific) then
begin
if assigned(it.FonErro) then
begin
it.FonErro(sender, h + s + buf);
inc(x);
end;
end;
if nInteracoes = 0 then
continue;
if x >= nInteracoes then
break;
end;
finally
end;
end);
end;
procedure TLogListItems.DoProcessProc(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double = 0;
nInteracoes: Integer = 1);
var
i, x: Integer;
it: TLogListItem;
begin
try
if not Enabled then
exit;
FBuffer := '';
x := 0;
for i := 0 to Count - 1 do
begin
it := TLogListItem(items[i]);
if (identific = 0) or (it.Ident = identific) then
begin
if assigned(it.OnProgress) then
begin
it.FOnProgress(sender, ATipo, msg, APosition);
inc(x);
end;
end;
if nInteracoes = 0 then
continue;
if x >= nInteracoes then
break;
end;
finally
end;
end;
procedure TLogListItems.DoProgress(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double = 0;
nInteracoes: Integer = 1);
begin
Run(
procedure
begin
DoProcessProc(sender, identific, ATipo, msg, APosition, nInteracoes);
end);
end;
procedure TLogListItems.Log(texto: string);
begin
Run(
procedure
begin
DoErro(self, 0, texto, 1);
end);
end;
procedure TLogListItems.register(sender: TObject; event: TLogListEvent;
identific: Integer);
var
it: TLogListItem;
begin
it := TLogListItem.create;
Add(it);
it.instancia := sender;
it.OnErro := event;
it.Ident := identific;
end;
procedure TLogListItems.register(sender: TObject; event: TLogProgressEvent;
identific: Integer);
var
it: TLogListItem;
begin
it := TLogListItem.create;
Add(it);
it.instancia := sender;
it.OnProgress := event;
it.Ident := identific;
end;
procedure TLogListItems.Run(proc: TProc);
begin
if not FSyncronized then
begin
proc;
exit;
end
else
{$IF CompilerVersion>28.0}
TTask.create(
procedure
begin
try
TThread.Queue(nil,
procedure
begin
proc;
end);
except
on e: exception do
LogEvents.DoErro(nil, 0, e.message);
end;
end).start;
{$ELSE}
TThread.CreateAnonymousThread(
procedure
begin
try
TThread.Queue(nil,
procedure
begin
proc;
end);
except
on e: exception do
LogEvents.DoErro(nil, 0, e.message);
end;
end).start;
{$ENDIF}
end;
procedure TLogListItems.SetEnabled(const Value: boolean);
begin
FEnabled := Value;
end;
procedure TLogListItems.SetMax(FValue: Integer);
begin
end;
procedure TLogListItems.SetMostraDataHora(const Value: boolean);
begin
FMostraDataHora := Value;
end;
procedure TLogListItems.SetSyncronized(const Value: boolean);
begin
FSyncronized := Value;
end;
procedure TLogListItems.unregister(sender: TObject);
var
i: Integer;
begin
for i := Count - 1 downto 0 do
if TLogListItem(items[i]).instancia = sender then
delete(i);
end;
initialization
LogEvents := TLogListItems.create;
finalization
LogEvents.free;
end.