View source with formatted 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]).   43
   44/** <module> Unix syslog interface
   45
   46This library provides an interface to   the  Unix syslog() facility. The
   47interface is an almost direct translation of  the POSIX syslog API, with
   48two additions:
   49
   50  - syslog/3 exploits format/3 to format syslog messages
   51  - The library integrates into library(debug) using
   52    prolog:debug_print_hook/3, where debug _topics_ are mapped to
   53    syslog _priorities_ and remaining debug _topics_ are mapped
   54    to the syslog _priority_ =debug=.
   55
   56Note that this interface  makes  no   attempt  to  abstract over logging
   57facilities of operating systems. We expect   that such abstractions will
   58be implemented at the Prolog  level   using  multiple  integrations into
   59library(debug).
   60
   61@see    detach_IO/1 to detach normal I/O of the process and remove it
   62        from the process group.
   63@see    fork/1 to create a daemon process.
   64@see    library(uid) to manage user identifiers (e.g., drop root
   65        privileges).
   66*/
   67
   68:- use_foreign_library(foreign(syslog)).   69
   70:- dynamic syslog/1.   71
   72%!  openlog(+Ident:atom, +Options:list(atom), +Facility:atom) is det.
   73%
   74%   Open system log. This predicate provides a direct interface into
   75%   the openlog() library call. If the   library call is successful,
   76%   it runs at_halt(closelog) to ensure closing   the  system log on
   77%   clean exit.
   78%
   79%   @param  Ident prepended to every message, and is typically set
   80%           to the program name.
   81%   @param  Options is a list of options.  Values are corresponding
   82%           C options, after removing =LOG_= and translation to
   83%           lower case: =cons=, =ndelay=, =nowait=, =odelay=,
   84%           =perror=, =pid=.
   85%   @param  Facility is one of =auth=, =authpriv=, =cron=, =daemon=,
   86%           =ftp=, =kern=, =local0= ... =local7=, =lpr=, =mail=,
   87%           =news=, =syslog=, =user= or =uucp=.
   88
   89openlog(Ident, Options, Facility) :-
   90    '$openlog'(Ident, Options, Facility),
   91    asserta(syslog(Ident)),
   92    at_halt(closelog).
   93
   94%!  syslog(+Priority, +Message) is det.
   95%
   96%   Send a message to the system  log. Note that syslog/2 implicitly
   97%   opens a connection to the system log   if  such a connection has
   98%   not been opened explicitly using openlog/3.
   99%
  100%   @param  Priority is one of =emerg=, =alert=, =crit=, =err=,
  101%           =warning=, =notice=, =info= or =debug=.
  102
  103%!  syslog(+Priority, +Format, +Args) is det.
  104%
  105%   Send a formatted message to the system  log if system logging is
  106%   opened using openlog/3. This predicate   combined  format/3 with
  107%   syslog/2. If there is no open  syslog connection, syslog/3 calls
  108%   print_message/2.
  109
  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)).
  118
  119%!  closelog is det.
  120%
  121%   Close the system log.
  122
  123closelog :-
  124    retractall(syslog(_)),
  125    '$closelog'.
  126
  127
  128                 /*******************************
  129                 *     DEBUG INTEGRATION        *
  130                 *******************************/
  131
  132:- multifile
  133    prolog:debug_print_hook/3.  134
  135%!  prolog:debug_print_hook(+Topic, +Format, +Args) is semidet.
  136%
  137%   Integration of debug/3 with the syslog   facility.  If syslog is
  138%   enabled, debug/3 is re-routed to use   the syslog facilities. If
  139%   the _topic_ of the  debug  message   matches  one  of  the sylog
  140%   _priority_ values (see syslog/2), the message   is sent with the
  141%   corresponding syslog priority. Otherwise  it   it  sent with the
  142%   =debug= priority.
  143
  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)