-
Notifications
You must be signed in to change notification settings - Fork 19
/
syslog.pl
180 lines (149 loc) · 6.38 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
174
175
176
177
178
179
180
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2013-2020, VU University Amsterdam
CWI Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(syslog,
[ openlog/3, % +Ident, +Options, +Facility
syslog/2, % +Priority, +Message
syslog/3, % +Priority, +Format, +Args
closelog/0
]).
:- autoload(library(lists),[member/2]).
/** <module> Unix syslog interface
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).