1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2013-2020, VU University Amsterdam 7 CWI Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(syslog, 37 [ openlog/3, % +Ident, +Options, +Facility 38 syslog/2, % +Priority, +Message 39 syslog/3, % +Priority, +Format, +Args 40 closelog/0 41 ]). 42:- autoload(library(lists),[member/2]).
68:- use_foreign_library(foreign(syslog)). 69 70:- dynamic syslog/1.
openlog()
library call. If the library call is successful,
it runs at_halt(closelog)
to ensure closing the system log on
clean exit.
89openlog(Ident, Options, Facility) :-
90 '$openlog'(Ident, Options, Facility),
91 asserta(syslog(Ident)),
92 at_halt(closelog).
110syslog(Priority, Format, Args) :- 111 syslog(_), 112 !, 113 format(string(Msg), Format, Args), 114 syslog(Priority, Msg). 115syslog(Priority, Format, Args) :- 116 syslog_priority(Priority, Kind), 117 print_message(Kind, format(Format, Args)).
123closelog :- 124 retractall(syslog(_)), 125 '$closelog'. 126 127 128 /******************************* 129 * DEBUG INTEGRATION * 130 *******************************/ 131 132:- multifile 133 prolog:debug_print_hook/3.
debug
priority.144prologdebug_print_hook(Topic, Format, Args) :- 145 syslog(_), 146 debug_priority(Topic, Priority), 147 syslog(Priority, Format, Args). 148 149debug_priority(Topic, Priority) :- 150 ( syslog_priority(Topic, _Kind) 151 -> Priority = Topic 152 ; Priority = debug 153 ). 154 155syslog_priority(emerg, error). 156syslog_priority(alert, warning). 157syslog_priority(crit, error). 158syslog_priority(err, error). 159syslog_priority(warning, warning). 160syslog_priority(notice, informational). 161syslog_priority(info, informational). 162syslog_priority(debug, debug). 163 164 165 /******************************* 166 * MESSAGE INTEGRATION * 167 *******************************/ 168 169user:message_hook(Term, Kind, _) :- 170 syslog(_), 171 kind_syslog_priority(Kind, Level), 172 message_to_string(Term, Message), 173 atomic_list_concat(Lines, '\n', Message), 174 forall(member(Line, Lines), 175 syslog(Level, Line)), 176 fail. 177 178kind_syslog_priority(error, err). 179kind_syslog_priority(warning, warning). 180kind_syslog_priority(informational, info)
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: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).