forked from SWI-Prolog/packages-clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog.pl
173 lines (142 loc) · 5.7 KB
/
syslog.pl
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2013, VU University Amsterdam
This program 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 2
of the License, or (at your option) any later version.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(syslog,
[ openlog/3, % +Ident, +Options, +Facility
syslog/2, % +Priority, +Message
syslog/3, % +Priority, +Format, +Args
closelog/0
]).
/** <module> Unix system logger interface
@ingroup SWIclib
This library provides an interface to the Unix syslog() facility. The
interface is an almost direct translation of the POSIX syslog API, with
two additions:
- syslog/3 exploits format/3 to format syslog messages
- The library integrates into library(debug) using
prolog:debug_print_hook/3, where debug _topics_ are mapped to
syslog _priorities_ and remaining debug _topics_ are mapped
to the syslog _priority_ =debug=.
Note that this interface makes no attempt to abstract over logging
facilities of operating systems. We expect that such abstractions will
be implemented at the Prolog level using multiple integrations into
library(debug).
@see detach_IO/1 to detach normal I/O of the process and remove it
from the process group.
@see fork/1 to create a daemon process.
@see library(uid) to manage user identifiers (e.g., drop root
privileges).
*/
:- use_foreign_library(foreign(syslog)).
:- dynamic syslog/1.
%% openlog(+Ident:atom, +Options:list(atom), +Facility:atom) is det.
%
% Open system log. This predicate provides a direct interface into
% the openlog() library call. If the library call is successful,
% it runs at_halt(closelog) to ensure closing the system log on
% clean exit.
%
% @param Ident prepended to every message, and is typically set
% to the program name.
% @param Options is a list of options. Values are corresponding
% C options, after removing =LOG_= and translation to
% lower case: =cons=, =ndelay=, =nowait=, =odelay=,
% =perror=, =pid=.
% @param Facility is one of =auth=, =authpriv=, =cron=, =daemon=,
% =ftp=, =kern=, =local0= ... =local7=, =lpr=, =mail=,
% =news=, =syslog=, =user= or =uucp=.
openlog(Ident, Options, Facility) :-
'$openlog'(Ident, Options, Facility),
asserta(syslog(Ident)),
at_halt(closelog).
%% syslog(+Priority, +Message) is det.
%
% Send a message to the system log. Note that syslog/2 implicitly
% opens a connection to the system log if such a connection has
% not been opened explicitly using openlog/3.
%
% @param Priority is one of =emerg=, =alert=, =crit=, =err=,
% =warning=, =notice=, =info= or =debug=.
%% syslog(+Priority, +Format, +Args) is det.
%
% Send a formatted message to the system log if system logging is
% opened using openlog/3. This predicate combined format/3 with
% syslog/2. If there is no open syslog connection, syslog/3 calls
% print_message/2.
syslog(Priority, Format, Args) :-
syslog(_), !,
format(string(Msg), Format, Args),
syslog(Priority, Msg).
syslog(Priority, Format, Args) :-
syslog_priority(Priority, Kind),
print_message(Kind, format(Format, Args)).
%% closelog is det.
%
% Close the system log.
closelog :-
retractall(syslog(_)),
'$closelog'.
/*******************************
* DEBUG INTEGRATION *
*******************************/
:- multifile
prolog:debug_print_hook/3.
%% prolog:debug_print_hook(+Topic, +Format, +Args) is semidet.
%
% Integration of debug/3 with the syslog facility. If syslog is
% enabled, debug/3 is re-routed to use the syslog facilities. If
% the _topic_ of the debug message matches one of the sylog
% _priority_ values (see syslog/2), the message is sent with the
% corresponding syslog priority. Otherwise it it sent with the
% =debug= priority.
prolog:debug_print_hook(Topic, Format, Args) :-
syslog(_),
debug_priority(Topic, Priority),
syslog(Priority, Format, Args).
debug_priority(Topic, Priority) :-
( syslog_priority(Topic, _Kind)
-> Priority = Topic
; Priority = debug
).
syslog_priority(emerg, error).
syslog_priority(alert, warning).
syslog_priority(crit, error).
syslog_priority(err, error).
syslog_priority(warning, warning).
syslog_priority(notice, informational).
syslog_priority(info, informational).
syslog_priority(debug, debug).
/*******************************
* MESSAGE INTEGRATION *
*******************************/
user:message_hook(Term, Kind, _) :-
syslog(_),
kind_syslog_priority(Kind, Level),
message_to_string(Term, Message),
atomic_list_concat(Lines, '\n', Message),
forall(member(Line, Lines),
syslog(Level, Line)),
fail.
kind_syslog_priority(error, err).
kind_syslog_priority(warning, warning).
kind_syslog_priority(informational, info).