-
Notifications
You must be signed in to change notification settings - Fork 19
/
FormRamLog.pas
216 lines (150 loc) · 6.15 KB
/
FormRamLog.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
UNIT FormRamLog;
{=============================================================================================================
Gabriel Moraru
2024.05
See Copyright.txt
--------------------------------------------------------------------------------------------------------------
Visual log (window).
More details in llRichLogUtils.pas
Usage:
It is CRITICAL to create the AppData object as soon as the application starts.
Prefferably in the DPR file before creating the main form!
DPR:
AppData:= TAppData.Create('MyCollApp');
OnLateInitialize:
AppData.Initilizing:= False;
AppDataEx is automatically destroyed by the Finalization section of this unit.
Tester:
c:\Myprojects\LightSaber\Demo\LightLog\
=============================================================================================================}
INTERFACE
{.$DENYPACKAGEUNIT ON} {Prevents unit from being placed in a package. https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Packages_(Delphi)#Naming_packages }
USES
Winapi.Messages, System.Classes, System.SysUtils,
Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls,
cbAppData, cbLogRam, cvLog, cvLogFilter, Vcl.Menus, Vcl.Grids;
TYPE
TfrmRamLog = class(TForm)
Log : TLogGrid;
Container : TPanel; { We use a container for all controls on this form so we can reparent them easily to another form }
btnClear : TButton;
chkLogOnError : TCheckBox;
chkShowTime : TCheckBox;
pnlBottom : TPanel;
trkLogVerb : TLogVerbFilter;
chkShowDate : TCheckBox;
PopupMenu: TPopupMenu;
mnuCopy: TMenuItem;
mnuCopyAll: TMenuItem;
mnuCopyFiltered: TMenuItem;
procedure btnClearClick (Sender: TObject);
procedure FormDestroy (Sender: TObject);
procedure chkLogOnErrorClick (Sender: TObject);
procedure FormClose (Sender: TObject; var Action: TCloseAction);
procedure chkShowTimeClick (Sender: TObject);
procedure chkShowDateClick (Sender: TObject);
procedure mnuCopyClick(Sender: TObject);
procedure mnuCopyAllClick(Sender: TObject);
private
procedure LoadSettings;
procedure SaveSettings;
public
class procedure CreateGlobalLog; static; // Would be nice to make this protected but we can't. All event handlers must be accesible/visible
procedure LateInitialize(VAR Msg: TMessage); message MSG_LateFormInit; // Called after the main form was fully initilized
end;
IMPLEMENTATION {$R *.dfm}
USES
cbLogUtils, cvINIFile, ccINIFile;
{-------------------------------------------------------------------------------------------------------------
FORM
-------------------------------------------------------------------------------------------------------------}
VAR FormLog: TfrmRamLog= NIL; // Accessible via AppData only
{ Create the Log form globally (to be used by the entire application) }
{ToDo 5: Find a way to create it automatically from AppData (interfaces?). We cannot do it from AppData itself because this form depends on my LightVisControls.dpk which is not available at this compilation point.
We could use something like:
TAppData = class
class procedure RegisterFormCreator(AFormCreator: TFunc<TForm>); static; // This class procedure would be called when we initialize the high-level package that contains the FormLog.
end;
But this method seems to brittle. }
class procedure TfrmRamLog.CreateGlobalLog;
begin
Assert(FormLog = NIL, 'Form log already created!');
VAR CreateBeforeMainForm:= Application.MainForm = NIL;
AppData.CreateForm(TfrmRamLog, FormLog, FALSE, flPosOnly, NIL, FALSE, CreateBeforeMainForm);
FormLog.Log.AssignExternalRamLog(AppData.RamLog); // We will read data from AppData's log
Assert(Application.MainForm <> FormLog, 'The Log should not be the MainForm!'); { Just in case: Make sure this is not the first form created }
end;
procedure TfrmRamLog.LateInitialize;
begin
LoadSettings;
chkLogOnError.Checked:= AppData.RamLog.ShowOnError;
chkShowTime.Checked := Log.ShowTime;
end;
procedure TfrmRamLog.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:= caHide; // This window is global (tied to AppData). We cannot close it.
end;
// Triggered by application shutdown
procedure TfrmRamLog.FormDestroy(Sender: TObject);
begin
Log.RamLog.UnregisterLogObserver; //ToDo: do I need this?
SaveSettings;
Container.Parent:= Self;
end;
procedure TfrmRamLog.SaveSettings;
begin
Assert(AppData <> NIL, 'AppData is gone already!');
// Save form position
if NOT cbAppData.AppData.Initializing
then cvINIFile.SaveForm(Self, flPosOnly); // We don't save anything if the start up was improper!
// Save Log verbosity
VAR IniFile := TIniFileEx.Create('Log Settings', AppData.IniFile);
try
IniFile.Write('ShowOnError', AppData.RamLog.ShowOnError);
IniFile.Write('ShowTime' , Log.ShowTime);
IniFile.Write('ShowDate' , Log.ShowDate);
IniFile.Write('Verbosity' , Ord(Log.Verbosity));
finally
FreeAndNil(IniFile);
end;
end;
procedure TfrmRamLog.LoadSettings;
begin
cvINIFile.LoadForm(Self, flPosOnly);
VAR IniFile := TIniFileEx.Create('Log Settings', AppData.IniFile);
try
AppData.RamLog.ShowOnError:= IniFile.Read('ShowOnError', TRUE);
Log.ShowTime := IniFile.Read('ShowTime', TRUE);
Log.ShowDate := IniFile.Read('ShowDate', TRUE);
Log.Verbosity := TLogVerbLvl(IniFile.Read('Verbosity', Ord(lvHints)));
chkShowDate.Checked:= Log.ShowDate;
chkShowTime.Checked:= Log.ShowTime;
finally
FreeAndNil(IniFile);
end;
end;
procedure TfrmRamLog.btnClearClick(Sender: TObject);
begin
Log.Clear;
end;
procedure TfrmRamLog.chkLogOnErrorClick(Sender: TObject);
begin
AppData.RamLog.ShowOnError:= chkLogOnError.Checked;
end;
procedure TfrmRamLog.chkShowDateClick(Sender: TObject);
begin
Log.ShowDate:= chkShowDate.Checked;
end;
procedure TfrmRamLog.chkShowTimeClick(Sender: TObject);
begin
Log.ShowTime:= chkShowTime.Checked;
end;
procedure TfrmRamLog.mnuCopyAllClick(Sender: TObject);
begin
Log.CopyAll;
end;
procedure TfrmRamLog.mnuCopyClick(Sender: TObject);
begin
Log.CopyCurLine;
end;
end.