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-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]).

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). */
   68:- use_foreign_library(foreign(syslog)).   69
   70:- 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.
   89openlog(Ident, Options, Facility) :-
   90    '$openlog'(Ident, Options, Facility),
   91    asserta(syslog(Ident)),
   92    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.
  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)).
 closelog is det
Close the system log.
  123closelog :-
  124    retractall(syslog(_)),
  125    '$closelog'.
  126
  127
  128                 /*******************************
  129                 *     DEBUG INTEGRATION        *
  130                 *******************************/
  131
  132:- multifile
  133    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.
  144prolog:debug_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)