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