summaryrefslogtreecommitdiff
path: root/extras/tiopf/gui/tiLogToGUI.pas
blob: 9acd5ff179c741265b70399dac6fa248779b6312 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
  Log to a window above the application's main form, but only if
  the -lv parameter is passed on the command line
  
  This in normally controlled by the tiLogReg unit.

  *** NOTE ***
  If you application doesn't terminate when you activated LogToGUI, it probably
  means a TThread.WaitFor deadlock occured. In that case, call ReleaseLog()
  after fpgApplication.Run in you project's *.lpr file.
}
unit tiLogToGUI;

{$mode objfpc}{$H+}

interface
uses
  Classes,
  SysUtils,
  fpg_widget,
  fpg_form,
  fpg_memo,
  fpg_menu,
  fpg_panel,
  fpg_button,
  fpg_main,
  tiLog;

type
  TtiLogToGUI = class(TtiLogToCacheAbs)
  private
    FForm: TfpgForm;
    FMemoLog: TfpgMemo;
    FToolBar: TfpgBevel;
    FPopupMenu: TfpgPopupMenu;
    FViewLogMenuItem: TfpgMenuItem;
    function    GetFormParent: TfpgWidget;
    procedure   SetFormParent(const AValue: TfpgWidget);
    function    CreateForm: TfpgForm;
    procedure   FormClearMenuItemClick(Sender: TObject);
    procedure   FormWordWrapMenuItemClick(Sender: TObject);
    procedure   FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure   FormLogLevelButtonClick(Sender: TObject);
    procedure   DoViewLogFile(Sender: TObject);
    procedure   DoOnPopup(Sender: TObject);
    procedure   WriteToMemo(const AMessage: string);
  protected
    procedure   WriteToOutput; override;
    procedure   SetSevToLog(const AValue: TtiSevToLog); override;
  public
    constructor Create; override;
    destructor  Destroy; override;
    property    FormParent: TfpgWidget read GetFormParent; // write SetFormParent;
    procedure   Log(const ADateTime, AThreadID, AMessage: string; ASeverity: TtiLogSeverity); override;
  end;


implementation
uses
  fpg_base,
  tiUtils,
  tiCommandLineParams,
  tiDialogs;


{ TtiLogToGUI }

constructor TtiLogToGUI.Create;
begin
  // GUI output must be synchronized with the main thread.
  inherited CreateSynchronized;
  FForm := CreateForm;
  ThrdLog.Resume;
end;

destructor TtiLogToGUI.Destroy;
begin
//  writeln('>> TtiLogToGUI.Destroy');
  if Assigned(FForm) then
    FForm.Free;
  FForm := nil;
  inherited Destroy;
//  writeln('<< TtiLogToGUI.Destroy');
end;

function TtiLogToGUI.CreateForm: TfpgForm;
var
  lMenuItem: TfpgMenuItem;
  lLogSev: TtiLogSeverity;
  lToolButton: TfpgButton;
  x: integer;
begin
  FForm                     := TfpgForm.Create(fpgApplication);
  FForm.WindowPosition      := wpUser;
  FForm.Top                 := 10;
  FForm.Left                := 10;
  FForm.Height              := 150;
  FForm.Width               := fpgApplication.ScreenWidth - 20;
  FForm.WindowTitle         := 'Application event log - ' + ApplicationName;
  FForm.OnCloseQuery        := @FormCloseQuery;

  FPopupMenu                := TfpgPopupMenu.Create(FForm);
  FPopupMenu.Name           := 'PopupMenu';
  FPopupMenu.BeforeShow     := @DoOnPopup;

  FToolBar                  := TfpgBevel.Create(FForm);
  FToolBar.Name             := 'ToolBar';
  FToolBar.SetPosition(0, 0, FForm.Width, 30);
  FToolbar.Shape            := bsSpacer;
  FToolBar.Align            := alTop;
  FToolBar.TabOrder         := 1;

  FMemoLog                  := TfpgMemo.Create(FForm);
  FMemoLog.Name             := 'MemoLog';
  FMemoLog.Top              := 29;
  FMemoLog.Align            := alClient;
  FMemoLog.FontDesc         := '#Edit2';  // monospaced font
  FMemoLog.PopupMenu        := FPopupMenu;
//  FMemoLog.ReadOnly         := True;
//  FMemoLog.ScrollBars       := ssBoth;
  FMemoLog.TabOrder         := 0;
//  FMemoLog.WordWrap         := False;
  FMemoLog.Lines.Clear;

  { Setup popup menu items}
  FViewLogMenuItem          := FPopupMenu.AddMenuItem('View log file', '', @DoViewLogFile);
  FViewLogMenuItem.Name     := 'Viewlogfile1';
  lMenuItem                 := FPopupMenu.AddMenuItem('-', '', nil);
  lMenuItem.Name            := 'N1';
  lMenuItem                 := FPopupMenu.AddMenuItem('Clear', '', @FormClearMenuItemClick);
  lMenuItem.Name            := 'ClearMenuItem';
  lMenuItem                 := FPopupMenu.AddMenuItem('Word wrap', '', @FormWordWrapMenuItemClick);
  lMenuItem.Name            := 'WordWrapMenuItem';
  lMenuItem.Enabled         := False;

  { Setup severity toolbar buttons }
  x := 1;
  for lLogSev := Low(TtiLogSeverity) to High(TtiLogSeverity) do
  begin
    lToolButton           := TfpgButton.Create(FToolBar);
    lToolButton.SetPosition(x, 1, 50, 28);
    lToolButton.Text      := cTILogSeverityStrings[lLogSev];
    lToolButton.Tag       := Ord(lLogSev);
    lToolButton.AllowAllUp := True; // enables toggle button mode
    lToolButton.GroupIndex := Ord(lLogSev) + 1; // enables toggle button mode
    lToolButton.Down      := lLogSev in GLog.SevToLog;
    lToolButton.OnClick   := @FormLogLevelButtonClick;
    lToolButton.Focusable := False;
    Inc(x, 51);
  end;
  
 Result := FForm;
end;

function TtiLogToGUI.GetFormParent: TfpgWidget;
begin
  result := FForm.Parent;
end;

procedure TtiLogToGUI.Log(const ADateTime, AThreadID, AMessage: string; ASeverity: TtiLogSeverity);
begin
  if Terminated then
    Exit; //==>
  if not FForm.HasHandle then
    FForm.Show;
  inherited Log(ADateTime, AThreadID, AMessage, ASeverity);
end;

procedure TtiLogToGUI.SetFormParent(const AValue: TfpgWidget);
begin
  {$Note This is untested!!! }
  FForm.Parent      := AValue;
  FForm.Align       := alClient;
  FForm.WindowAttributes := FForm.WindowAttributes + [waBorderless];
//  FForm.BorderStyle := bsNone;
end;

procedure TtiLogToGUI.SetSevToLog(const AValue: TtiSevToLog);
var
  i: integer;
  lLogSev: TtiLogSeverity;
begin
  // Let parent perform important task(s)
  inherited;
  // All we do here is reflect any changes to LogSeverity in the visual controls
  for i := 0 to FToolBar.ComponentCount - 1 do
  begin
    lLogSev := TtiLogSeverity(FToolBar.Components[i].Tag);
    if FToolBar.Components[i] is TfpgButton then
      TfpgButton(FToolBar.Components[i]).Down := lLogSev in AValue;
  end;
end;

procedure TtiLogToGUI.WriteToMemo(const AMessage: string);
var
  i: integer;
  LLine: string;
  LCount: integer;
begin
  LCount := tiNumToken(AMessage, CrLf);
  if LCount = 1 then
    FMemoLog.Lines.Add(tiTrimTrailingWhiteSpace(AMessage))
  else
    for i := 1 to LCount do
    begin
      LLine := tiTrimTrailingWhiteSpace(tiToken(AMessage, CrLf, i));
      FMemoLog.Lines.Add(LLine);
    end;
end;

procedure TtiLogToGUI.WriteToOutput;
var
  i: integer;
  LLogEvent: TtiLogEvent;
  LPosStart: integer;
  LPosEnd: integer;
const
  ciMaxLineCount = 200;
begin
  if ThrdLog.Terminated then
    Exit; //==>

  inherited WriteToOutput;

  if ListWorking.Count > ciMaxLineCount * 2 then
  begin
    FMemoLog.Lines.Clear;
    LPosStart := ListWorking.Count - 1 - ciMaxLineCount;
    LPosEnd   := ListWorking.Count - 1;
  end else
  begin
    if FMemoLog.Lines.Count > ciMaxLineCount then
    begin
      for i := 0 to ciMaxLineCount div 2 do
        FMemoLog.Lines.Delete(0);
      //{$IFDEF MSWINDOWS}
      //SendMessage(FMemoLog.handle, WM_VSCROLL, SB_Bottom, 0);
      //{$ENDIF MSWINDOWS}
    end;
    LPosStart := 0;
    LPosEnd   := ListWorking.Count - 1;
  end;

  for i := LPosStart to LPosEnd do begin
    if ThrdLog.Terminated then
      Break; //==>
    LLogEvent := TtiLogEvent(ListWorking.Items[i]);
    WriteToMemo(LLogEvent.AsLeftPaddedString);
  end;

  ListWorking.Clear;
end;

procedure TtiLogToGUI.FormClearMenuItemClick(Sender: TObject);
begin
  FMemoLog.Lines.Clear;
end;

procedure TtiLogToGUI.FormWordWrapMenuItemClick(Sender: TObject);
begin
  //FMemoLog.WordWrap        := not FMemoLog.WordWrap;
  //FWordWrapMenuItem.Checked := FMemoLog.WordWrap;
  //if FMemoLog.WordWrap then
    //FMemoLog.ScrollBars := ssVertical
  //else
    //FMemoLog.ScrollBars := ssBoth;
end;

procedure TtiLogToGUI.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
end;

procedure TtiLogToGUI.FormLogLevelButtonClick(Sender: TObject);
var
  lLogSev: TtiLogSeverity;
  lLogChecked: boolean;
begin
  if not (Sender is TfpgButton) then
    Exit; //==>

  lLogSev := TtiLogSeverity(TfpgWidget(Sender).Tag);
  lLogChecked := TfpgButton(Sender).Down;
  if lLogChecked then
    GLog.SevToLog := GLog.SevToLog + [lLogSev]
  else
    GLog.SevToLog := GLog.SevToLog - [lLogSev];
end;

procedure TtiLogToGUI.DoViewLogFile(Sender: TObject);
var
  sl: TStringList;
begin
  if (GLog.LogToFileName <> '') and
     (FileExists(GLog.LogToFileName)) then
  begin
    sl := TStringList.Create;
    try
      sl.LoadFromFile(GLog.LogToFilename);
      tiShowStringList(sl, GLog.LogToFilename);
//      tiEditFile(GLog.LogToFileName);
    finally
      sl.Free;
    end;
  end;
end;

procedure TtiLogToGUI.DoOnPopup(Sender: TObject);
begin
  { If we are logging to file as well, then enable the menu option }
  FViewLogMenuItem.Enabled :=
    (GLog.LogToFileName <> '') and
    (FileExists(GLog.LogToFileName));
end;


end.