-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.pas
69 lines (54 loc) · 1.18 KB
/
logger.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
unit logger;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, RadioModule;
type
{ TTextLogger }
TTextLogger = class(TRadioLogger)
private
FFile: TFileStream;
constructor Create;
protected
procedure DoReport(const ALevel: TRadioLogLevel; const S: string); override;
public
destructor Destroy; override;
class procedure Start;
end;
implementation
const
LogLevel2Str: array[TRadioLogLevel] of string = ('verbose', 'info', 'warn', 'error', 'system');
{ TTextLogger }
constructor TTextLogger.Create;
var
P: string = '';
begin
inherited;
while True do
begin
try
FFile := TFileStream.Create('log' + P + '.txt', fmCreate);
P := FormatDateTime('-yyyy-mm-dd-hh:nn:ss.z', Now);
Break;
except
end;
end;
end;
procedure TTextLogger.DoReport(const ALevel: TRadioLogLevel; const S: string);
var
T: string;
begin
T := FormatDateTime('hh:nn:ss.z ', Now) + LogLevel2Str[ALevel] + ' ' + S + #13#10;
FFile.Write(T[1], Length(T));
end;
destructor TTextLogger.Destroy;
begin
FFile.Free;
inherited Destroy;
end;
class procedure TTextLogger.Start;
begin
FreeAndNil(FInstance);
FInstance := TTextLogger.Create;
end;
end.