Skip to content

Commit c3487f5

Browse files
- classe remodelada afim, para evitar concorrencia de gravação mesmo arquivo
1 parent 99137da commit c3487f5

1 file changed

Lines changed: 129 additions & 37 deletions

File tree

log/lazarus/Log.TextFile.pas

Lines changed: 129 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,174 @@
88
interface
99

1010
uses
11-
SysUtils, Classes;
11+
SysUtils, Classes, syncobjs;
1212

1313
type
1414

15-
{ TLog }
15+
{ TInfoLog }
1616

17-
TLog = class(TThread)
17+
TInfoLog = class
1818
private
1919
FFileName: string;
2020
FSection: string;
2121
FValue: string;
22+
public
23+
function WriteLog : boolean;
24+
published
25+
property FileName: string read FFileName write FFileName;
26+
property Section: string read FSection write FSection;
27+
property Value: string read FValue write FValue;
28+
end;
29+
30+
{ TLog }
31+
32+
TLog = class(TThread)
33+
private
34+
FInfo : TList;
35+
FCritical : TCriticalSection;
36+
FEvent : TSimpleEvent;
2237
protected
23-
procedure doLog;
2438
procedure Execute; override;
39+
procedure Clear;
2540
public
26-
constructor Create(ASection, AValue, AFileName: string);
27-
class procedure Log(ASection, AValue: string; AFileName: string = 'log.txt');
41+
constructor Create;
2842
destructor Destroy; override;
43+
44+
procedure Terminar;
45+
46+
procedure AddLog(AInfo : TInfoLog);
47+
48+
class procedure Log(ASection, AValue: string; AFileName: string = 'log.txt');
2949
end;
3050

3151
implementation
3252

53+
var
54+
vLogger : TLog;
55+
56+
{ TInfoLog }
57+
58+
function TInfoLog.WriteLog: boolean;
59+
var
60+
vLogFile: Text;
61+
vLogData: String;
62+
begin
63+
vLogData := FormatDateTime('dd/mm/yyyy hh:nn:ss', Now) + ' | ' +
64+
FSection + ' | ' + FValue;
65+
66+
Result := False;
67+
try
68+
AssignFile(vLogFile, FFileName);
69+
try
70+
if FileExists(FFileName) then
71+
Append(vLogFile)
72+
else
73+
Rewrite(vLogFile);
74+
75+
Writeln(vLogFile, vLogData);
76+
Result := True;
77+
finally
78+
CloseFile(vLogFile);
79+
end;
80+
except
81+
Result := False;
82+
end;
83+
end;
84+
3385
{ TLog }
3486

35-
constructor TLog.Create(ASection, AValue, AFileName: string);
87+
constructor TLog.Create;
3688
begin
37-
inherited Create(False);
3889
FreeOnTerminate := True;
39-
FFileName := AFileName;
40-
FSection := ASection;
41-
FValue := AValue;
90+
FInfo := TList.Create;
91+
FCritical := TCriticalSection.Create;
92+
FEvent := TSimpleEvent.Create;
93+
94+
inherited Create(False);
4295
end;
4396

4497
destructor TLog.Destroy;
4598
begin
46-
99+
Clear;
100+
FreeAndNil(FInfo);
101+
FreeAndNil(FCritical);
102+
FreeAndNil(FEvent);
47103
inherited Destroy;
48104
end;
49105

50-
procedure TLog.doLog;
51-
var
52-
FLogFile: Text;
53-
logdata: string;
54-
Confirma: boolean;
106+
procedure TLog.Terminar;
55107
begin
56-
logdata := FormatDateTime('dd/mm/yyyy hh:nn:ss', Now) + ' | ' +
57-
FSection + ' | ' + FValue;
108+
Terminate;
109+
FEvent.SetEvent;
110+
end;
58111

59-
Confirma := False;
60-
while not Confirma do
61-
try
62-
try
63-
AssignFile(FLogFile, FFileName);
64-
if FileExists(FFileName) then
65-
Append(FLogFile)
66-
else
67-
Rewrite(FLogFile);
112+
procedure TLog.AddLog(AInfo: TInfoLog);
113+
begin
114+
FCritical.Enter;
115+
FInfo.Add(AInfo);
116+
FCritical.Leave;
68117

69-
Writeln(FlogFile, logdata);
70-
Confirma := True;
71-
except
72-
Confirma := False;
118+
FEvent.SetEvent;
119+
end;
120+
121+
procedure TLog.Execute;
122+
var
123+
vInfo : TInfoLog;
124+
begin
125+
while not Terminated do
126+
begin
127+
FEvent.WaitFor(INFINITE);
128+
129+
if FInfo.Count > 0 then
130+
begin
131+
FCritical.Enter;
132+
vInfo := TInfoLog(FInfo.Items[0]);
133+
if vInfo.WriteLog then
134+
begin
135+
FreeAndNil(vInfo);
136+
FInfo.Delete(0);
137+
end;
138+
FCritical.Leave;
139+
end
140+
else
141+
begin
142+
FEvent.ResetEvent;
73143
end;
74-
finally
75-
CloseFile(FlogFile);
76144
end;
77145
end;
78146

79-
procedure TLog.Execute;
147+
procedure TLog.Clear;
148+
var
149+
vInfo : TInfoLog;
80150
begin
81-
Synchronize(@doLog);
151+
while FInfo.Count > 0 do
152+
begin
153+
vInfo := TInfoLog(FInfo.Items[FInfo.Count - 1]);
154+
FreeAndNil(vInfo);
155+
FInfo.Delete(FInfo.Count - 1);
156+
end;
82157
end;
83158

84159
class procedure TLog.Log(ASection, AValue: string; AFileName: string);
160+
var
161+
vInfo : TInfoLog;
85162
begin
86-
Create(ASection, AValue, AFileName);
163+
if vLogger = nil then
164+
vLogger := TLog.Create;
165+
166+
vInfo := TInfoLog.Create;
167+
vInfo.Section := ASection;
168+
vInfo.Value := AValue;
169+
vInfo.FileName := AFileName;
170+
171+
vLogger.AddLog(vInfo);
87172
end;
88173

174+
initialization
175+
vLogger := nil;
176+
177+
finalization
178+
if vLogger <> nil then
179+
vLogger.Terminar;
180+
89181
end.

0 commit comments

Comments
 (0)