-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.SysLogHandler.pp
154 lines (133 loc) · 4.99 KB
/
Logger.SysLogHandler.pp
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
{*******************************************************}
{ Renegade BBS }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016-2017 Renegade BBS }
{ This file is part of Renegade BBS }
{ Renegade is free software: you can redistribute it }
{ and/or modify it under the terms of the GNU General }
{ Public License as published by the Free Software }
{ Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ Renegade 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. See the GNU General Public }
{ License for more details. }
{ You should have received a copy of the GNU General }
{ Public License along with Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$mode objfpc}
{$linklib c}
{$codepage utf8}
{$h+}
{
Unix System Log Handler.
The constructor takes one of the defined Logging Facilities.
}
unit Logger.SysLogHandler;
interface
uses
Classes,
SysUtils,
Logger.HandlerInterface;
const
// log the pid with each message
LOG_PID = $01;
// Write directly to system console if there is an error
// while sending to system logger.
LOG_CONS = $02;
// The converse of LOG_NDELAY; opening of the connection is delayed until
// syslog() is called. (This is the default, and need not be specified.)
LOG_ODELAY = $04;
// Open the connection immediately (normally, the connection is opened when
// the first message is logged).
LOG_NDELAY = $08;
// Don't wait for child processes that may have been created while logging
// the message. (The GNU C library does not create a child process, so this
// option has no effect on Linux.)
LOG_NOWAIT = $10;
// log to stderr as well
LOG_PERROR = $20;
{ Logging Facilities }
LOG_KERN = 0 shl 3; // kernel messages
LOG_USER = 1 shl 3; // random user-level messages
LOG_MAIL = 2 shl 3; // mail system
LOG_DAEMON = 3 shl 3; // system daemons
LOG_AUTH = 4 shl 3; // security/authorization messages
LOG_SYSLOG = 5 shl 3; // messages generated internally by syslogd
LOG_LPR = 6 shl 3; // line printer subsystem
LOG_NEWS = 7 shl 3; // network news subsystem
LOG_UUCP = 8 shl 3; // UUCP subsystem
LOG_CRON = 9 shl 3; // clock daemon
LOG_AUTHPRIV = 10 shl 3; // security/authorization messages (private)
var
UnixFacility: longint;
type
SysLogHandler = class(TObject, LoggingHandlerInterface)
private
FUnixFacility: longint;
procedure SetFacility(const UnixFacility: longint);
public
constructor Create(const LoggingFacility: longint);
function Open(Identifier: UTF8String): boolean;
function Close(): boolean;
function Write(const LogData: UTF8String): boolean;
published
property UnixFacility: longint read FUnixFacility write SetFacility;
end;
EPlatformNotSupported = class(Exception) end;
procedure closelog; cdecl; external;
procedure openlog(__ident: PChar; __option: longint; __facilit: longint);
cdecl; external;
function setlogmask(__mask: longint): longint; cdecl; external;
procedure syslog(__pri: longint; __fmt: PChar; args: array of const); cdecl; external;
implementation
constructor SysLogHandler.Create(const LoggingFacility: longint);
begin
{$IFDEF WIN}
Raise EPlatformNotSupported.Create ('SysLogHandler is not supported under Windows.') at
get_caller_addr(get_frame),
get_caller_frame(get_frame);
{$ENDIF WIN}
FUnixFacility := LoggingFacility;
end;
procedure SysLogHandler.SetFacility(const UnixFacility: longint);
begin
{$IFDEF WIN}
Raise EPlatformNotSupported.Create ('SysLogHandler is not supported under Windows.') at
get_caller_addr(get_frame),
get_caller_frame(get_frame);
{$ENDIF WIN}
FUnixFacility := UnixFacility;
end;
function SysLogHandler.Open(Identifier: UTF8String): boolean;
var
SysLogIdentifier: PAnsiChar;
begin
SysLogIdentifier := PAnsiChar(Identifier);
openlog(SysLogIdentifier, LOG_PID xor LOG_CONS xor LOG_NDELAY, FUnixFacility);
Result := True;
end;
function SysLogHandler.Close(): boolean;
begin
closelog;
Result := True;
end;
function SysLogHandler.Write(const LogData: UTF8String): boolean;
var
SysLogData: PAnsiChar;
begin
SysLogData := PAnsiChar(LogData);
syslog(FUnixFacility, SysLogData, []);
Result := True;
end;
end.