|
8 | 8 | interface |
9 | 9 |
|
10 | 10 | uses |
11 | | - SysUtils, Classes; |
| 11 | + SysUtils, Classes, syncobjs; |
12 | 12 |
|
13 | 13 | type |
14 | 14 |
|
15 | | - { TLog } |
| 15 | + { TInfoLog } |
16 | 16 |
|
17 | | - TLog = class(TThread) |
| 17 | + TInfoLog = class |
18 | 18 | private |
19 | 19 | FFileName: string; |
20 | 20 | FSection: string; |
21 | 21 | 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; |
22 | 37 | protected |
23 | | - procedure doLog; |
24 | 38 | procedure Execute; override; |
| 39 | + procedure Clear; |
25 | 40 | public |
26 | | - constructor Create(ASection, AValue, AFileName: string); |
27 | | - class procedure Log(ASection, AValue: string; AFileName: string = 'log.txt'); |
| 41 | + constructor Create; |
28 | 42 | 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'); |
29 | 49 | end; |
30 | 50 |
|
31 | 51 | implementation |
32 | 52 |
|
| 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 | + |
33 | 85 | { TLog } |
34 | 86 |
|
35 | | -constructor TLog.Create(ASection, AValue, AFileName: string); |
| 87 | +constructor TLog.Create; |
36 | 88 | begin |
37 | | - inherited Create(False); |
38 | 89 | 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); |
42 | 95 | end; |
43 | 96 |
|
44 | 97 | destructor TLog.Destroy; |
45 | 98 | begin |
46 | | - |
| 99 | + Clear; |
| 100 | + FreeAndNil(FInfo); |
| 101 | + FreeAndNil(FCritical); |
| 102 | + FreeAndNil(FEvent); |
47 | 103 | inherited Destroy; |
48 | 104 | end; |
49 | 105 |
|
50 | | -procedure TLog.doLog; |
51 | | -var |
52 | | - FLogFile: Text; |
53 | | - logdata: string; |
54 | | - Confirma: boolean; |
| 106 | +procedure TLog.Terminar; |
55 | 107 | begin |
56 | | - logdata := FormatDateTime('dd/mm/yyyy hh:nn:ss', Now) + ' | ' + |
57 | | - FSection + ' | ' + FValue; |
| 108 | + Terminate; |
| 109 | + FEvent.SetEvent; |
| 110 | +end; |
58 | 111 |
|
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; |
68 | 117 |
|
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; |
73 | 143 | end; |
74 | | - finally |
75 | | - CloseFile(FlogFile); |
76 | 144 | end; |
77 | 145 | end; |
78 | 146 |
|
79 | | -procedure TLog.Execute; |
| 147 | +procedure TLog.Clear; |
| 148 | +var |
| 149 | + vInfo : TInfoLog; |
80 | 150 | 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; |
82 | 157 | end; |
83 | 158 |
|
84 | 159 | class procedure TLog.Log(ASection, AValue: string; AFileName: string); |
| 160 | +var |
| 161 | + vInfo : TInfoLog; |
85 | 162 | 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); |
87 | 172 | end; |
88 | 173 |
|
| 174 | +initialization |
| 175 | + vLogger := nil; |
| 176 | + |
| 177 | +finalization |
| 178 | + if vLogger <> nil then |
| 179 | + vLogger.Terminar; |
| 180 | + |
89 | 181 | end. |
0 commit comments