| 1 | unit DebugLog;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Generics.Collections, SyncObjs;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TDebugLogAddEvent = procedure (Group: string; Text: string) of object;
|
|---|
| 10 |
|
|---|
| 11 | TDebugLogItem = class
|
|---|
| 12 | Time: TDateTime;
|
|---|
| 13 | Group: string;
|
|---|
| 14 | Text: string;
|
|---|
| 15 | end;
|
|---|
| 16 |
|
|---|
| 17 | TDebugLogItems = class(TObjectList<TDebugLogItem>)
|
|---|
| 18 | end;
|
|---|
| 19 |
|
|---|
| 20 | TNewItemEvent = procedure (NewItem: TDebugLogItem) of object;
|
|---|
| 21 |
|
|---|
| 22 | { TDebugLog }
|
|---|
| 23 |
|
|---|
| 24 | TDebugLog = class(TComponent)
|
|---|
| 25 | private
|
|---|
| 26 | FFileName: string;
|
|---|
| 27 | FMaxCount: Integer;
|
|---|
| 28 | FOnNewItem: TNewItemEvent;
|
|---|
| 29 | FWriteToFileEnable: Boolean;
|
|---|
| 30 | procedure SetMaxCount(const AValue: Integer);
|
|---|
| 31 | public
|
|---|
| 32 | Items: TDebugLogItems;
|
|---|
| 33 | Lock: TCriticalSection;
|
|---|
| 34 | procedure Add(Text: string; Group: string = '');
|
|---|
| 35 | procedure WriteToFile(Text: string);
|
|---|
| 36 | constructor Create(AOwner: TComponent); override;
|
|---|
| 37 | destructor Destroy; override;
|
|---|
| 38 | published
|
|---|
| 39 | property WriteToFileEnable: Boolean read FWriteToFileEnable
|
|---|
| 40 | write FWriteToFileEnable;
|
|---|
| 41 | property FileName: string read FFileName write FFileName;
|
|---|
| 42 | property MaxCount: Integer read FMaxCount write SetMaxCount;
|
|---|
| 43 | property OnNewItem: TNewItemEvent read FOnNewItem write FOnNewItem;
|
|---|
| 44 | end;
|
|---|
| 45 |
|
|---|
| 46 | procedure Register;
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | implementation
|
|---|
| 50 |
|
|---|
| 51 | resourcestring
|
|---|
| 52 | SFileNameNotDefined = 'Filename not defined';
|
|---|
| 53 |
|
|---|
| 54 | procedure Register;
|
|---|
| 55 | begin
|
|---|
| 56 | RegisterComponents('Common', [TDebugLog]);
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | { TDebugLog }
|
|---|
| 60 |
|
|---|
| 61 | procedure TDebugLog.SetMaxCount(const AValue: Integer);
|
|---|
| 62 | begin
|
|---|
| 63 | if FMaxCount = AValue then Exit;
|
|---|
| 64 | FMaxCount := AValue;
|
|---|
| 65 | try
|
|---|
| 66 | Lock.Acquire;
|
|---|
| 67 | if Items.Count > FMaxCount then Items.Count := AValue;
|
|---|
| 68 | finally
|
|---|
| 69 | Lock.Release;
|
|---|
| 70 | end;
|
|---|
| 71 | end;
|
|---|
| 72 |
|
|---|
| 73 | procedure TDebugLog.Add(Text: string; Group: string = '');
|
|---|
| 74 | var
|
|---|
| 75 | NewItem: TDebugLogItem;
|
|---|
| 76 | begin
|
|---|
| 77 | NewItem := TDebugLogItem.Create;
|
|---|
| 78 | NewItem.Time := Now;
|
|---|
| 79 | NewItem.Group := Group;
|
|---|
| 80 | NewItem.Text := Text;
|
|---|
| 81 |
|
|---|
| 82 | try
|
|---|
| 83 | Lock.Acquire;
|
|---|
| 84 | Items.Insert(0, NewItem);
|
|---|
| 85 | if Items.Count > MaxCount then begin
|
|---|
| 86 | Items.Delete(Items.Count - 1);
|
|---|
| 87 | end;
|
|---|
| 88 |
|
|---|
| 89 | if WriteToFileEnable then begin
|
|---|
| 90 | if Group <> '' then Group := Group + '[' + Group + '] ';
|
|---|
| 91 | WriteToFile(Group + Text);
|
|---|
| 92 | end;
|
|---|
| 93 | finally
|
|---|
| 94 | Lock.Release;
|
|---|
| 95 | end;
|
|---|
| 96 | if Assigned(FOnNewItem) then
|
|---|
| 97 | FOnNewItem(NewItem);
|
|---|
| 98 | end;
|
|---|
| 99 |
|
|---|
| 100 | procedure TDebugLog.WriteToFile(Text: string);
|
|---|
| 101 | var
|
|---|
| 102 | LogFile: TFileStream;
|
|---|
| 103 | begin
|
|---|
| 104 | if FileName = '' then raise Exception.Create(SFileNameNotDefined);
|
|---|
| 105 | try
|
|---|
| 106 | if ExtractFileDir(FileName) <> '' then
|
|---|
| 107 | ForceDirectories(ExtractFileDir(FileName));
|
|---|
| 108 | if FileExists(FileName) then LogFile := TFileStream.Create(FileName, fmOpenWrite)
|
|---|
| 109 | else LogFile := TFileStream.Create(FileName, fmCreate);
|
|---|
| 110 | LogFile.Seek(0, soFromEnd);
|
|---|
| 111 | Text := FormatDateTime('hh:nn:ss.zzz', Now) + ': ' + Text + LineEnding;
|
|---|
| 112 | LogFile.WriteBuffer(Text[1], Length(Text));
|
|---|
| 113 | finally
|
|---|
| 114 | FreeAndNil(LogFile);
|
|---|
| 115 | end;
|
|---|
| 116 | end;
|
|---|
| 117 |
|
|---|
| 118 | constructor TDebugLog.Create(AOwner: TComponent);
|
|---|
| 119 | begin
|
|---|
| 120 | inherited;
|
|---|
| 121 | Items := TDebugLogItems.Create;
|
|---|
| 122 | Lock := TCriticalSection.Create;
|
|---|
| 123 | MaxCount := 100;
|
|---|
| 124 | FileName := 'DebugLog.txt';
|
|---|
| 125 | WriteToFileEnable := False;
|
|---|
| 126 | end;
|
|---|
| 127 |
|
|---|
| 128 | destructor TDebugLog.Destroy;
|
|---|
| 129 | begin
|
|---|
| 130 | FreeAndNil(Items);
|
|---|
| 131 | FreeAndNil(Lock);
|
|---|
| 132 | inherited;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | end.
|
|---|