source: trunk/Packages/Common/DebugLog.pas

Last change on this file was 75, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File size: 3.2 KB
Line 
1unit DebugLog;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Generics.Collections, SyncObjs;
7
8type
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
46procedure Register;
47
48
49implementation
50
51resourcestring
52 SFileNameNotDefined = 'Filename not defined';
53
54procedure Register;
55begin
56 RegisterComponents('Common', [TDebugLog]);
57end;
58
59{ TDebugLog }
60
61procedure TDebugLog.SetMaxCount(const AValue: Integer);
62begin
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;
71end;
72
73procedure TDebugLog.Add(Text: string; Group: string = '');
74var
75 NewItem: TDebugLogItem;
76begin
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);
98end;
99
100procedure TDebugLog.WriteToFile(Text: string);
101var
102 LogFile: TFileStream;
103begin
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;
116end;
117
118constructor TDebugLog.Create(AOwner: TComponent);
119begin
120 inherited;
121 Items := TDebugLogItems.Create;
122 Lock := TCriticalSection.Create;
123 MaxCount := 100;
124 FileName := 'DebugLog.txt';
125 WriteToFileEnable := False;
126end;
127
128destructor TDebugLog.Destroy;
129begin
130 FreeAndNil(Items);
131 FreeAndNil(Lock);
132 inherited;
133end;
134
135end.
Note: See TracBrowser for help on using the repository browser.