summaryrefslogtreecommitdiff
path: root/examples/apps/debugserver/frm_main.pas
blob: e0da08a74652211e75d90f63a4de2c82bc8f2837 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      This is the server part to a standard FPC unit - dbugintf. This unit
      offers a simple API to send messages to a debug server (using
      SimpleIPC), modeled after the GExperts GDebug tool for Delphi, with
      some minor enhancements. This is a great way to debug CGI apps on a
      server as well.

      NOTE: you would normally wrap the SendXXX methods with {$ifdef debug} so
      the code can be excluded from a final released product (without debug
      information). But this is obviously for you do decide.

      Typical usage is as follows:

      uses
        dbugintf, sysutils;

      procedure BackupFile(FN : String);
      var
         BFN: String;
      begin
         SendMethodEnter('BackupFile');
         BFN := FN + '.bak';
         SendDebug(Format('backup file "%s" exists, deleting',[BFN]));
         SendDebug(Format('Backing up "%s" to "%s"',[FN,BFN]));
         SendMethodExit('BackupFile');
      end;

      procedure SaveToFile(FN : String);
      begin
         SendMethodEnter('SaveToFile');
         BackupFile(FN);
         SendDebug('Saving to file '+FN);
         SendMethodExit('SaveToFile');
      end;


      Available SendXXX methods from the dbugintf unit are:

        procedure SendBoolean(const Identifier: string; const Value: Boolean);
        procedure SendDateTime(const Identifier: string; const Value: TDateTime);
        procedure SendInteger(const Identifier: string; const Value: Integer; HexNotation: Boolean = False);
        procedure SendPointer(const Identifier: string; const Value: Pointer);
        procedure SendDebugEx(const Msg: string; MType: TDebugLevel);
        procedure SendDebug(const Msg: string);
        procedure SendMethodEnter(const MethodName: string);
        procedure SendMethodExit(const MethodName: string);
        procedure SendSeparator;
        procedure SendDebugFmt(const Msg: string; const Args: array of const);
        procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TDebugLevel);

}
unit frm_main;

{$mode objfpc}{$H+}

interface

uses
  SysUtils
  ,Classes
  ,fpg_base
  ,fpg_main
  ,fpg_form
  ,fpg_button
  ,fpg_panel
  ,fpg_menu
  ,fpg_basegrid
  ,fpg_grid
  ,simpleipc
  ,dbugmsg
  ;

type
  TMainForm = class(TfpgForm)
  private
    {@VFD_HEAD_BEGIN: MainForm}
    MainMenu: TfpgMenuBar;
    Bevel1: TfpgBevel;
    grdMessages: TfpgStringGrid;
    mnuFile: TfpgPopupMenu;
    mnuEdit: TfpgPopupMenu;
    mnuHelp: TfpgPopupMenu;
    btnQuit: TfpgButton;
    Bevel2: TfpgBevel;
    btnPause: TfpgButton;
    btnStart: TfpgButton;
    btnClear: TfpgButton;
    {@VFD_HEAD_END: MainForm}
    FIPCSrv: TSimpleIPCServer;
    FPaused: Boolean;
    FAddAtBottom: Boolean;
    FDiscarded: Integer;
    FShowOnMessage: Boolean;
    procedure   StartServer;
    procedure   StopServer;
    procedure   CheckMessages(Sender: TObject);
    procedure   CheckDebugMessages;
    procedure   ReadDebugMessage;
    procedure   ShowDebugMessage(const AMsg: TDebugmessage);
    procedure   ShowMessageWindow;
    procedure   miFileQuit(Sender: TObject);
    procedure   miHelpAboutFPGui(Sender: TObject);
    procedure   miHelpProductInformation(Sender: TObject);
    procedure   btnClearClicked(Sender: TObject);
    procedure   btnPauseClicked(Sender: TObject);
    procedure   btnStartClicked(Sender: TObject);
    procedure   GridDrawCell(Sender: TObject; const ARow, ACol: Integer; const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   AfterCreate; override;
  end;

{@VFD_NEWFORM_DECL}

implementation

uses
  dateutils
  ,fpg_dialogs
  ;


{$I images.inc}
{$I state_images.inc}

{@VFD_NEWFORM_IMPL}

procedure TMainForm.btnClearClicked(Sender: TObject);
begin
  grdMessages.RowCount := 0;
end;

procedure TMainForm.btnPauseClicked(Sender: TObject);
begin
  FPaused := not FPaused;
end;

procedure TMainForm.btnStartClicked(Sender: TObject);
begin
  if Assigned(FIPCSrv) then
    Exit;
  StartServer;
end;

procedure TMainForm.GridDrawCell(Sender: TObject; const ARow, ACol: Integer;
  const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
var
  img: TfpgImage;
  i: integer;
  dx: integer;
begin
  if ACol = 0 then
  begin
    ADefaultDrawing := False;
    try
      i := StrToInt(grdMessages.Cells[ACol, ARow]);
      case i of
        -1:  img := fpgImages.GetImage('dbs.state.stop');
         0:  img := fpgImages.GetImage('dbs.state.info');
         1:  img := fpgImages.GetImage('dbs.state.warning');
         2:  img := fpgImages.GetImage('dbs.state.error');
         3:  img := fpgImages.GetImage('dbs.state.identify');
      end;
      dx := (grdMessages.ColumnWidth[ACol] - 16) div 2;
      grdMessages.Canvas.DrawImage(ARect.Left + dx, ARect.Top {+ y}, img);
    except
      on E: Exception do
        begin
//          writeln('Cell text = ' + grdMessages.Cells[ACol, ARow]);
        end;
    end;
  end;
end;

procedure TMainForm.StartServer;
begin
  FIPCSrv := TSimpleIPCServer.Create(nil);
  FIPCSrv.ServerID := DebugServerID;
  FIPCSrv.Global := True;
  FIPCSrv.Active := True;
  FIPCSrv.StartServer;
  fpgApplication.OnIdle := @CheckMessages;
//  ITMessages.Enabled:=True;
end;

procedure TMainForm.StopServer;
begin
  fpgApplication.OnIdle := nil;
//  ITMessages.Enabled := False;
  FreeAndNil(FIPCSrv);
end;

procedure TMainForm.CheckMessages(Sender: TObject);
begin
  CheckDebugMessages;
end;

procedure TMainForm.CheckDebugMessages;
begin
  while FIPCSrv.PeekMessage(1, True) do
    ReadDebugMessage;
end;

procedure TMainForm.ReadDebugMessage;
var
  Msg: TDebugMessage;
begin
  FIPCSrv.MsgData.Seek(0, soFromBeginning);
  ReadDebugMessageFromStream(FIPCSrv.MsgData, MSg);
  if not FPaused then
    ShowDebugMessage(Msg)
  else
    Inc(FDiscarded);
end;

procedure TMainForm.ShowDebugMessage(const AMsg: TDebugmessage);
var
  r: integer;
begin
  grdMessages.BeginUpdate;
  try
    grdMessages.RowCount := grdMessages.RowCount + 1;
    r := grdMessages.RowCount-1;
    //if FAddAtBottom then
    //  grdMessages.Items.Add(LI)
    //else
    //  grdMessages.Items.InsertItem(LI, 0);
    grdMessages.Cells[0, r] := IntToStr(AMsg.MsgType);
    grdMessages.Cells[1, r] := FormatDateTime('HH:mm:ss', AMsg.MsgTimeStamp);
    grdMessages.Cells[2, r] := AMsg.Msg;
    grdMessages.FocusCol := 0;
    grdMessages.FocusRow := grdMessages.RowCount-1;
  finally
    grdMessages.EndUpdate;
  end;
  if FShowOnMessage then
    ShowMessageWindow;
end;

procedure TMainForm.ShowMessageWindow;
begin
  if not Visible then
    Show;
end;

procedure TMainForm.miFileQuit(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.miHelpAboutFPGui(Sender: TObject);
begin
  TfpgMessageDialog.AboutFPGui;
end;

procedure TMainForm.miHelpProductInformation(Sender: TObject);
begin
  TfpgMessageDialog.Information('Product Information', WindowTitle + LineEnding + 'Written by Graeme Geldenhuys - 2010');
end;

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FPaused := False;
  FAddAtBottom := False;
  FShowOnMessage := False;
  StartServer;

  fpgImages.AddMaskedBMP('dbs.clean', @DBS_clean, sizeof(DBS_clean), 15, 0);
  fpgImages.AddMaskedBMP('dbs.stop', @DBS_stop, sizeof(DBS_stop), 0, 0);
  fpgImages.AddMaskedBMP('dbs.pause', @DBS_pause, sizeof(DBS_pause), 0, 0);
  fpgImages.AddMaskedBMP('dbs.run', @DBS_run, sizeof(DBS_run), 0, 0);

  fpgImages.AddMaskedBMP('dbs.state.info', @DBS_state_info, sizeof(DBS_state_info), 0, 0);
  fpgImages.AddMaskedBMP('dbs.state.warning', @DBS_state_warning, sizeof(DBS_state_warning), 0, 0);
  fpgImages.AddMaskedBMP('dbs.state.error', @DBS_state_error, sizeof(DBS_state_error), 0, 0);
  fpgImages.AddMaskedBMP('dbs.state.identify', @DBS_state_lightbulb, sizeof(DBS_state_lightbulb), 0, 0);
  fpgImages.AddMaskedBMP('dbs.state.stop', @DBS_state_lightbulb_off, sizeof(DBS_state_lightbulb_off), 0, 0);
end;

destructor TMainForm.Destroy;
begin
  StopServer;
  inherited Destroy;
end;

procedure TMainForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: MainForm}
  Name := 'MainForm';
  SetPosition(353, 245, 486, 313);
  WindowTitle := 'fpGUI''s Debug Server';
  Hint := '';
  ShowHint := True;

  MainMenu := TfpgMenuBar.Create(self);
  with MainMenu do
  begin
    Name := 'MainMenu';
    SetPosition(0, 0, 486, 26);
    Anchors := [anLeft,anRight,anTop];
  end;

  Bevel1 := TfpgBevel.Create(self);
  with Bevel1 do
  begin
    Name := 'Bevel1';
    SetPosition(0, 26, 486, 31);
    Anchors := [anLeft,anRight,anTop];
    Hint := '';
    Style := bsLowered;
    Shape := bsBottomLine;
  end;

  grdMessages := TfpgStringGrid.Create(self);
  with grdMessages do
  begin
    Name := 'grdMessages';
    SetPosition(4, 63, 478, 246);
    Anchors := [anLeft,anRight,anTop,anBottom];
    AddColumn('Type', 50, taLeftJustify);
    AddColumn('Time', 75, taCenter);
    AddColumn('Message', 330, taLeftJustify);
    FontDesc := '#Grid';
    HeaderFontDesc := '#GridHeader';
    Hint := '';
    RowCount := 0;
    RowSelect := True;
    ShowGrid := False;
    TabOrder := 2;
    Options := [go_SmoothScroll];
    OnDrawCell  := @GridDrawCell;
  end;

  mnuFile := TfpgPopupMenu.Create(self);
  with mnuFile do
  begin
    Name := 'mnuFile';
    SetPosition(260, 100, 120, 24);
    AddMenuItem('Quit', '', @miFileQuit);
  end;

  mnuEdit := TfpgPopupMenu.Create(self);
  with mnuEdit do
  begin
    Name := 'mnuEdit';
    SetPosition(260, 126, 120, 24);
    AddMenuItem('Cut', '', nil).Enabled := False;
    AddMenuItem('Copy', '', nil).Enabled := False;
    AddMenuItem('Paste', '', nil).Enabled := False;
    AddMenuItem('-', '', nil);
    AddMenuItem('Preferences...', '', nil).Enabled := False;
  end;

  mnuHelp := TfpgPopupMenu.Create(self);
  with mnuHelp do
  begin
    Name := 'mnuHelp';
    SetPosition(260, 152, 120, 24);
    AddMenuItem('About fpGUI...', '', @miHelpAboutFPGui);
    AddMenuItem('Product Information...', '', @miHelpProductInformation);
  end;

  btnQuit := TfpgButton.Create(Bevel1);
  with btnQuit do
  begin
    Name := 'btnQuit';
    SetPosition(4, 2, 25, 25);
    Text := '';
    Flat := True;
    FontDesc := '#Label1';
    Hint := '';
    ImageMargin := -1;
    ImageName := 'stdimg.quit';
    ImageSpacing := 0;
    TabOrder := 0;
    Focusable := False;
    OnClick := @miFileQuit;
  end;

  Bevel2 := TfpgBevel.Create(Bevel1);
  with Bevel2 do
  begin
    Name := 'Bevel2';
    SetPosition(34, 2, 8, 25);
    Hint := '';
    Style := bsLowered;
    Shape := bsLeftLine;
  end;

  btnPause := TfpgButton.Create(Bevel1);
  with btnPause do
  begin
    Name := 'btnPause';
    SetPosition(43, 2, 25, 25);
    Text := '';
    AllowAllUp := True;
    Flat := True;
    FontDesc := '#Label1';
    GroupIndex := 1;
    Hint := 'pause server';
    ImageMargin := -1;
    ImageName := 'dbs.pause';
    TabOrder := 2;
    Focusable := False;
    OnClick :=@btnPauseClicked;
  end;

  btnStart := TfpgButton.Create(Bevel1);
  with btnStart do
  begin
    Name := 'btnStart';
    SetPosition(70, 2, 25, 25);
    Text := '';
    Flat := True;
    FontDesc := '#Label1';
    Hint := 'start server';
    ImageMargin := -1;
    ImageName := 'dbs.run';
    TabOrder := 2;
    Focusable := False;
    OnClick := @btnStartClicked;
    Enabled := False;
  end;

  btnClear := TfpgButton.Create(Bevel1);
  with btnClear do
  begin
    Name := 'btnClear';
    SetPosition(97, 2, 25, 25);
    Text := '';
    Flat := True;
    FontDesc := '#Label1';
    Hint := 'clear listview';
    ImageMargin := -1;
    ImageName := 'dbs.clean';
    TabOrder := 2;
    Focusable := False;
    OnClick :=@btnClearClicked;
  end;

  {@VFD_BODY_END: MainForm}
  {%endregion}

  // Hook up the menus to the MenuBar
  MainMenu.AddMenuItem('File', nil).SubMenu := mnuFile;
  MainMenu.AddMenuItem('Edit', nil).SubMenu := mnuEdit;
  MainMenu.AddMenuItem('Help', nil).SubMenu := mnuHelp;
end;


end.