View source with raw comments or as raw
    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          ]).

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:

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 also
- detach_IO/1 to detach normal I/O of the process and remove it from the process group.
- fork/1 to create a daemon process.
-
library(uid) to manage user identifiers (e.g., drop root privileges). */
   66:- use_foreign_library(foreign(syslog)).   67
   68:- 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.
Arguments:
Ident- prepended to every message, and is typically set to the program name.
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.
Facility- is one of auth, authpriv, cron, daemon, ftp, kern, local0 ... local7, lpr, mail, news, syslog, user or uucp.
   87openlog(Ident, Options, Facility) :-
   88    '$openlog'(Ident, Options, Facility),
   89    asserta(syslog(Ident)),
   90    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.
Arguments:
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.
  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)).
 closelog is det
Close the system log.
  121closelog :-
  122    retractall(syslog(_)),
  123    '$closelog'.
  124
  125
  126                 /*******************************
  127                 *     DEBUG INTEGRATION        *
  128                 *******************************/
  129
  130:- multifile
  131    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.
  142prolog:debug_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)