source: trunk/Packages/Common/UDebugLog.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 3.1 KB
Line 
1unit UDebugLog;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, SpecializedList, SyncObjs;
9
10type
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
45procedure Register;
46
47implementation
48
49resourcestring
50 SFileNameNotDefined = 'Filename not defined';
51
52procedure Register;
53begin
54 RegisterComponents('Common', [TDebugLog]);
55end;
56
57{ TDebugLog }
58
59procedure TDebugLog.SetMaxCount(const AValue: Integer);
60begin
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;
69end;
70
71procedure TDebugLog.Add(Text: string; Group: string = '');
72var
73 NewItem: TDebugLogItem;
74begin
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);
96end;
97
98procedure TDebugLog.WriteToFile(Text: string);
99var
100 LogFile: TFileStream;
101begin
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;
114end;
115
116constructor TDebugLog.Create(AOwner: TComponent);
117begin
118 inherited;
119 Items := TListObject.Create;
120 Lock := TCriticalSection.Create;
121 MaxCount := 100;
122 FileName := 'DebugLog.txt';
123 WriteToFileEnable := False;
124end;
125
126destructor TDebugLog.Destroy;
127begin
128 Items.Free;
129 Lock.Free;
130 inherited;
131end;
132
133end.
134
Note: See TracBrowser for help on using the repository browser.