-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDLog.pas
138 lines (116 loc) · 2.86 KB
/
DLog.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
unit DLog;
interface
uses
SysUtils, Classes, Windows, acStrUtils, acSysUtils, ISincronizacaoNotifierUnit;
type
TDataLog = class(TDataModule, ILog)
procedure DataModuleCreate(Sender: TObject);
private
logFile: TextFile;
FbaseDir: string;
procedure openLogFile;
procedure SetbaseDir(const Value: string);
public
paused: boolean;
property baseDir: string read FbaseDir write SetbaseDir;
function getLogFileName(logDate: TDateTime = -1): string;
procedure log(const mensagem: string; const classe: string = ''; newLine: boolean = true; timestamp: boolean = true);
procedure print(mensagem: string);
procedure printL(mensagem: string);
procedure printLine;
procedure step(text: string = '.');
procedure newLine;
procedure pause;
procedure resume;
end;
var
DataLog: TDataLog;
CritSectLog: TRTLCriticalSection;
implementation
{$R *.dfm}
{ TDataLog }
procedure TDataLog.log(const mensagem: string; const classe: string = ''; newLine: boolean = true; timestamp: boolean = true);
var
linha: string;
begin
if paused then exit;
linha := '';
linha := '[' + FormatDateTime('yyyy-mm-dd hh:nn:ss,zzz', now) + ']';
linha := linha + '[' + fillSpaces(classe, 20) + ']';
linha := linha + ' ' + mensagem;
EnterCriticalSection(CritSectLog);
if newLine then
Writeln(logFile, linha)
else
Write(logFile, linha);
Flush(logFile);
LeaveCriticalSection(CritSectLog);
end;
procedure TDataLog.step(text: string = '.');
begin
Write(logFile, text);
Flush(logFile);
end;
function TDataLog.getLogFileName(logDate: TDateTime = -1): string;
begin
if logDate = -1 then
logDate := date;
if not(DirectoryExists(baseDir)) then
CreateDir(baseDir);
result := baseDir + FormatDateTime('yyyy_mm_dd', logDate) + '.log';
end;
procedure TDataLog.pause;
begin
log('Pausing Log');
paused := true;
CloseFile(logFile);
end;
procedure TDataLog.print(mensagem: string);
begin
log(mensagem, '', false, false);
end;
procedure TDataLog.printL(mensagem: string);
begin
log(mensagem, '', true, false);
end;
procedure TDataLog.printLine;
begin
printL('======================================');
end;
procedure TDataLog.resume;
begin
openLogFile;
paused := false;
log('Resumed Log')
end;
procedure TDataLog.openLogFile;
var
fn: string;
begin
fn := getLogFileName;
AssignFile(logFile, fn);
if FileExists(fn) then
Append(logFile)
else
Rewrite(logFile);
end;
procedure TDataLog.DataModuleCreate(Sender: TObject);
begin
baseDir := getWindowsTempPath;
paused := true;
end;
procedure TDataLog.newLine;
begin
Writeln(logFile, '.');
Flush(logFile);
end;
procedure TDataLog.SetbaseDir(const Value: string);
begin
FbaseDir := IncludeTrailingPathDelimiter(Value);
resume;
end;
initialization
InitializeCriticalSection(CritSectLog);
finalization
DeleteCriticalSection(CritSectLog);
end.