55uses
66 { $IFDEF UNIX}
77 cthreads,
8+ BaseUnix,
89 { $ENDIF}
910 Classes, SysUtils, CustApp
1011 { you can add units after this } ;
1112
1213type
1314
14- { TPasLogBot }
15-
15+ { TPasLogBot }
1616 TPasLogBot = class (TCustomApplication)
1717 protected
1818 procedure DoRun ; override;
@@ -22,6 +22,65 @@ TPasLogBot = class(TCustomApplication)
2222 procedure WriteHelp ; virtual ;
2323 end ;
2424
25+ var
26+ Application: TPasLogBot;
27+
28+ { Signal Handling }
29+ { $IFDEF UNIX}
30+ procedure SignalHandler (signal: longint; info: psiginfo; context: psigcontext); cdecl;
31+ begin
32+ case signal of
33+ SIGTERM, SIGINT:
34+ begin
35+ WriteLn(' Received termination signal' );
36+ if Assigned(Application) then
37+ Application.Terminate;
38+ end ;
39+ SIGHUP:
40+ begin
41+ WriteLn(' Received SIGHUP - could implement config reload here' );
42+ // Could implement configuration reload here
43+ end ;
44+ end ;
45+ end ;
46+
47+ procedure SetupSignalHandlers ;
48+ var
49+ act: SigActionRec;
50+ begin
51+ FillChar(act, SizeOf(act), 0 );
52+ act.sa_handler := @SignalHandler;
53+ act.sa_flags := 0 ;
54+
55+ // Set up signal handlers
56+ fpSigAction(SIGTERM, @act, nil );
57+ fpSigAction(SIGINT, @act, nil );
58+ fpSigAction(SIGHUP, @act, nil );
59+ end ;
60+ { $ENDIF}
61+
62+ { $IFDEF WINDOWS}
63+ function ConsoleCtrlHandler (CtrlType: DWORD): BOOL; stdcall;
64+ begin
65+ case CtrlType of
66+ CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT:
67+ begin
68+ WriteLn(' Received termination signal' );
69+ if Assigned(Application) then
70+ Application.Terminate;
71+ Result := True;
72+ Exit;
73+ end ;
74+ end ;
75+ Result := False;
76+ end ;
77+
78+ procedure SetupSignalHandlers ;
79+ begin
80+ SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
81+ end ;
82+ { $ENDIF}
83+
2584{ TPasLogBot }
2685
2786procedure TPasLogBot.DoRun ;
@@ -43,10 +102,12 @@ procedure TPasLogBot.DoRun;
43102 Exit;
44103 end ;
45104
46-
47-
105+ while not Terminated do
106+ begin
107+ Sleep(50 );
108+ end ;
48109 // stop program loop
49- Terminate;
110+ // Terminate;
50111end ;
51112
52113constructor TPasLogBot.Create(TheOwner: TComponent);
@@ -66,8 +127,6 @@ procedure TPasLogBot.WriteHelp;
66127 writeln(' Usage: ' , ExeName, ' -h' );
67128end ;
68129
69- var
70- Application: TPasLogBot;
71130begin
72131 Application:=TPasLogBot.Create(nil );
73132 Application.Title:=' Pascal Log Bot' ;
0 commit comments