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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
| TFileSizeList = class(TStringList)
private
FCritical: TCriticalSection;
function GetSize(const FileName: string): Integer;
procedure SetSize(const FileName: string; const Value: Integer);
function GetOldSize(const FileName: string): Integer;
function GetSizes(const Index: Integer): TFileSizeListItem;
function GetLastMessage(const Index: Integer): string;
procedure SetLastMessage(const Index: Integer; const Value: string);
public
constructor Create();
destructor Destroy(); override;
procedure Clear(); override;
function GetMessages(): string;
property Size[const FileName: string]: Integer read GetSize write SetSize;
property OldSize[const FileName: string]: Integer read GetOldSize;
property Sizes[const Index: Integer]: TFileSizeListItem read GetSizes;
property LastMessage[const Index: Integer]: string read GetLastMessage write SetLastMessage;
end;
TNotifyLogFileChangeThread = class(TThread)
private
FDirName: string;
FOnChange: TFileMessageEvent;
FOnIdle: TFileMessageEvent;
FFlash: Boolean;
FFlashCritical: TCriticalSection;
FFileSize: TFileSizeList;
FExcludeFile: TStringList;
procedure DoChange();
procedure DoIdle();
procedure ReadLastMessages();
protected
function GetFlash(): Boolean;
procedure SetFlash(Value: Boolean);
public
constructor Create(const DirName: string; const AExcludeFile: TStrings; AOnChange, AOnIdle: TFileMessageEvent);
destructor Destroy(); override;
procedure Execute; override;
class procedure ChangeFlash(Active: Boolean);
property OnChange: TFileMessageEvent read FOnChange write FOnChange;
property OnIdle: TFileMessageEvent read FOnIdle write FOnIdle;
property Flash: Boolean read GetFlash write SetFlash;
end;
{ TNotifyLogFileChangeThread}
constructor TNotifyLogFileChangeThread.Create(const DirName: string; const AExcludeFile: TStrings; AOnChange, AOnIdle: TFileMessageEvent);
var
I: Integer;
begin
inherited Create(True);
FFlashCritical := TCriticalSection.Create();
FreeOnTerminate := True;
FDirName := IncludeTrailingPathDelimiter(DirName);
FOnChange := AOnChange;
FOnIdle := AOnIdle;
FFileSize := TFileSizeList.Create();
FExcludeFile := TStringList.Create();
FExcludeFile.Sorted := True;
FExcludeFile.Duplicates := dupIgnore;
FExcludeFile.CaseSensitive := False;
if Assigned(AExcludeFile) then
for I := 0 to AExcludeFile.Count - 1 do
FExcludeFile.Add(AExcludeFile.Values[AExcludeFile.Names[I]]);
Resume();
end;
destructor TNotifyLogFileChangeThread.Destroy();
begin
if Assigned(FExcludeFile) then
begin
FExcludeFile.Free();
FExcludeFile := nil;
end;
if Assigned(FFileSize) then
begin
FFileSize.Free();
FFileSize := nil;
end;
if Assigned(FFlashCritical) then
begin
FFlashCritical.Free();
FFlashCritical := nil;
end;
end;
function TNotifyLogFileChangeThread.GetFlash(): Boolean;
begin
FFlashCritical.Acquire();
try
Result := FFlash;
FFlash := False;
finally
FFlashCritical.Release();
end;
end;
procedure TNotifyLogFileChangeThread.SetFlash(Value: Boolean);
begin
FFlashCritical.Acquire();
try
FFlash := Value;
finally
FFlashCritical.Release();
end;
end;
class procedure TNotifyLogFileChangeThread.ChangeFlash(Active: Boolean);
begin
_InternalThread.Flash := Active;
end;
function ShellProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall; // TFNHookProc
begin
TNotifyLogFileChangeThread.ChangeFlash(code = HSHELL_REDRAW);
Result := Ord(True);
end;
procedure TNotifyLogFileChangeThread.Execute();
var
HandleNotification: THandle;
Delay: Cardinal;
begin
ReadLastMessages;
Delay := 1000;
Sleep(Delay);
HandleNotification := FindFirstChangeNotification(PChar(FDirName), True, FILE_NOTIFY_CHANGE_SIZE);
try
while not Terminated do
begin
case WaitForSingleObject(HandleNotification, Delay) of
WAIT_OBJECT_0 :
begin
ReadLastMessages;
Synchronize(DoChange);
Delay := 1000;
FindNextChangeNotification(HandleNotification);
end;
WAIT_ABANDONED : Terminate;
WAIT_TIMEOUT :
begin
Synchronize(DoIdle);
Delay := 200;
end;
end;
end;
finally
FindCloseChangeNotification(HandleNotification);
end;
end;
procedure TNotifyLogFileChangeThread.DoChange();
begin
try
if Assigned(FOnChange) then
begin
FOnChange(Self, FFileSize.GetMessages());
end;
except
Exit;
end;
end;
procedure TNotifyLogFileChangeThread.DoIdle();
begin
try
if Assigned(FOnIdle) then
begin
FOnIdle(Self, FormatDateTime(' : dddd dd mmmm à hh:nn:ss', Now()));
end;
except
Exit;
end;
end;
procedure TNotifyLogFileChangeThread.ReadLastMessages();
procedure FillSizes();
var
SearchRec: TSearchRec;
SearcMask: string;
begin
FFileSize.BeginUpdate();
try
SearcMask := '*.log';
if FindFirst(FDirName+SearcMask, faAnyFile, SearchRec)= 0 then
begin
repeat
if FExcludeFile.IndexOf(SearchRec.Name) < 0 then
FFileSize.Size[SearchRec.Name] := TWin32FindData(SearchRec.FindData).nFileSizeLow; // Peu probable que cela dépasse 2Go, je ne gère pas High
until FindNext(SearchRec) <> 0;
end;
finally
FFileSize.EndUpdate();
end;
end;
function ReadLastMessage(const FileName: string; Index, Count: Integer): string;
var
F: File;
OldFM: Byte;
AmtTransferred: Integer;
begin
try
AssignFile(F, FDirName+FileName);
OldFM := FileMode;
FileMode := 0;
try
Reset(F, 1);
try
Seek(F, Index);
SetLength(Result, Count);
BlockRead(F, Result[1], Count, AmtTransferred);
SetLength(Result, AmtTransferred);
finally
CloseFile(F);
end;
finally
FileMode := OldFM;
end;
except
on E: Exception do Result := E.Message;
end;
end;
procedure CheckSizes();
var
ifs: Integer;
Item: TFileSizeListItem;
begin
for ifs := 0 to FFileSize.Count - 1 do
begin
Item := FFileSize.Sizes[ifs];
if Item.Size > Item.OldSize then
FFileSize.LastMessage[ifs] := ReadLastMessage(FFileSize[ifs], Item.OldSize, Item.Size-Item.OldSize-1);
end;
end;
begin
try
FillSizes();
CheckSizes();
except
on E: Exception do Exit;
end;
end;
{ TFileSizeList }
constructor TFileSizeList.Create();
begin
inherited Create();
FCritical := TCriticalSection.Create();
Sorted := True;
Duplicates := dupIgnore;
end;
destructor TFileSizeList.Destroy();
begin
inherited Destroy();
if Assigned(FCritical) then
begin
FCritical.Free();
FCritical := nil;
end;
end;
procedure TFileSizeList.Clear();
var
Index: Integer;
begin
FCritical.Acquire();
try
for Index := 0 to Count - 1 do
if Assigned(Objects[Index]) then
Dispose(PFileSizeListItem(Objects[Index]));
inherited Clear();
finally
FCritical.Release();
end;
end;
function TFileSizeList.GetSize(const FileName: string): Integer;
var
Index: Integer;
begin
Index := IndexOf(FileName);
if Index >= 0 then
Result := PFileSizeListItem(Objects[Index])^.Size
else
Result := -1;
end;
procedure TFileSizeList.SetSize(const FileName: string; const Value: Integer);
var
Index: Integer;
Item: PFileSizeListItem;
begin
FCritical.Acquire();
try
Index := IndexOf(FileName);
if Index >= 0 then
begin
PFileSizeListItem(Objects[Index])^.OldSize := PFileSizeListItem(Objects[Index])^.Size;
PFileSizeListItem(Objects[Index])^.Size := Value;
PFileSizeListItem(Objects[Index])^.LastMessage := '';
end else
begin
New(Item);
Item^.OldSize := Value;
Item^.Size := Value;
Item^.LastMessage := '';
AddObject(FileName, Pointer(Item));
end;
finally
FCritical.Release();
end;
end;
function TFileSizeList.GetOldSize(const FileName: string): Integer;
var
Index: Integer;
begin
Index := IndexOf(FileName);
if Index >= 0 then
Result := PFileSizeListItem(Objects[Index])^.OldSize
else
Result := -1;
end;
function TFileSizeList.GetSizes(const Index: Integer): TFileSizeListItem;
begin
Result := PFileSizeListItem(Objects[Index])^;
end;
function TFileSizeList.GetLastMessage(const Index: Integer): string;
begin
FCritical.Acquire();
try
if Index < Count then
Result := PFileSizeListItem(Objects[Index])^.LastMessage
else
Result := '';
finally
FCritical.Release();
end;
end;
procedure TFileSizeList.SetLastMessage(const Index: Integer; const Value: string);
begin
FCritical.Acquire();
try
if Index < Count then
PFileSizeListItem(Objects[Index])^.LastMessage := Value;
finally
FCritical.Release();
end;
end; |
Partager