Changeset 372 for Common/UDebugLog.pas


Ignore:
Timestamp:
Jun 4, 2012, 9:51:41 AM (12 years ago)
Author:
chronos
Message:
  • Modified: In Common package class TDebugLog redone with use of critical section locking.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/UDebugLog.pas

    r305 r372  
    66
    77uses
    8   Classes, SysUtils, FileUtil;
     8  Classes, SysUtils, FileUtil, SpecializedList, SyncObjs;
    99
    1010type
     
    2929    procedure SetMaxCount(const AValue: Integer);
    3030  public
    31     Items: TThreadList;
     31    Items: TListObject;
     32    Lock: TCriticalSection;
    3233    procedure Add(Group: string; Text: string);
    3334    procedure WriteToFile(Text: string);
     
    5758
    5859procedure TDebugLog.SetMaxCount(const AValue: Integer);
    59 var
    60   List: TList;
    61   I: Integer;
    6260begin
    6361  if FMaxCount = AValue then Exit;
    6462  FMaxCount := AValue;
    65   List := Items.LockList;
    66   if List.Count > 0 then begin
    67     for I := AValue to List.Count - 1 do
    68       TDebugLogItem(List[I]).Free;
    69     List.Count := AValue;
     63  try
     64    Lock.Acquire;
     65    if Items.Count > FMaxCount then Items.Count := AValue;
     66  finally
     67    Lock.Release;
    7068  end;
    71   Items.UnlockList;
    7269end;
    7370
    7471procedure TDebugLog.Add(Group: string; Text: string);
    7572var
    76   I: Integer;
    77   List: TList;
    7873  NewItem: TDebugLogItem;
    7974begin
     
    8378  NewItem.Text := Text;
    8479
    85   List := Items.LockList;
    86   List.Insert(0, NewItem);
    87   if List.Count > MaxCount then begin
    88     TDebugLogItem(List.Items[List.Count - 1]).Free;
    89     List.Delete(List.Count - 1);
    90   end;
    91   Items.UnlockList;
     80  try
     81    Lock.Acquire;
     82    Items.Insert(0, NewItem);
     83    if Items.Count > MaxCount then begin
     84      Items.Delete(Items.Count - 1);
     85    end;
    9286
    93   if WriteToFileEnable then begin
    94     if Group <> '' then Group := Group + '[' + Group + '] ';
    95     WriteToFile(Group + Text);
     87    if WriteToFileEnable then begin
     88      if Group <> '' then Group := Group + '[' + Group + '] ';
     89      WriteToFile(Group + Text);
     90    end;
     91  finally
     92    Lock.Release;
    9693  end;
    9794  if Assigned(FOnNewItem) then
     
    113110    LogFile.WriteBuffer(Text[1], Length(Text));
    114111  finally
    115     LogFile.Free;
     112    FreeAndNil(LogFile);
    116113  end;
    117114end;
     
    120117begin
    121118  inherited;
    122   Items := TThreadList.Create;
     119  Items := TListObject.Create;
     120  Lock := TCriticalSection.Create;
    123121  MaxCount := 100;
    124122  FileName := 'DebugLog.txt';
     
    127125
    128126destructor TDebugLog.Destroy;
    129 var
    130   List: TList;
    131   I: Integer;
    132127begin
    133   List := Items.LockList;
    134   for I := 0 to List.Count - 1 do
    135     TDebugLogItem(List[I]).Free;
    136128  Items.Free;
     129  Lock.Free;
    137130  inherited;
    138131end;
Note: See TracChangeset for help on using the changeset viewer.