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