-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloggerunit.pas
98 lines (77 loc) · 1.79 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
unit LoggerUnit;
//------------------------------------------------------------------------------
// Модуль журналирования
//------------------------------------------------------------------------------
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, syncobjs;
type
{ TLogger }
TLogger = class(TObject)
private
cs: TCriticalSection;
local_log: boolean;
public
constructor Create;
destructor Destroy; override;
procedure LogText(const st:string; const force_log:boolean = false);
procedure LogError(const err_code:integer; const log:string);
procedure startLog;
procedure pauseLog;
end;
var MyLogger: TLogger;
implementation
{ TLogger }
constructor TLogger.Create;
begin
inherited Create;
local_log := false;
cs := TCriticalSection.Create;
end;
destructor TLogger.Destroy;
begin
cs.Free;
inherited Destroy;
end;
procedure TLogger.LogText(const st: string; const force_log: boolean);
var f: textfile;
tmp,fnm: string;
begin
if local_log or force_log
then
begin
cs.Enter;
try
fnm := ExtractFilePath(ParamStr(0))+'log.txt';
AssignFile(f,fnm);
if FileExists(fnm)
then Append(f)
else Rewrite(f);
try
tmp := FormatDateTime('yyyy.mm.dd hh":"nn":"ss',now)+#9+st;
Writeln(f,tmp);
finally
CloseFile(f);
end;
finally
cs.Leave;
end;
end;
end;
procedure TLogger.LogError(const err_code: integer; const log: string);
begin
LogText('Код ошибки: '+IntToStr(err_code), false);
LogText(log, false);
end;
procedure TLogger.startLog;
begin
local_log := true;
end;
procedure TLogger.pauseLog;
begin
local_log := false;
end;
initialization
finalization
end.