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)  2019, 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(standard,
   36          [ error_write/1,              % @Message
   37            error_writeln/1,            % @Message
   38            console_write/1,            % @Message
   39            console_writeln/1,          % @Message
   40            warning/1,                  % @Message
   41            message/1,                  % @Message
   42            messageln/1                 % @Message
   43          ]).   44:- if(exists_source(library(dialect/xsb/timed_call))).   45:- use_module(library(dialect/xsb/timed_call)).   46:- export(timed_call/2).   47:- endif.   48
   49/** <module> XSB Term Writing to Designated I/O Streams
   50
   51This module emulates the XSB dedicated  term writing predicates from the
   52`standard` module.
   53
   54@compat XSB has a number of additional streams which we do not have. For
   55now we send all messages to   `user_error`. Onlt warning/1 is redirected
   56through SWI-Prolog's print_message/2 interface.
   57*/
   58
   59:- create_prolog_flag(warning_action, print_warning, [keep(true)]).   60
   61%!  error_write(@Term) is det.
   62%!  error_writeln(@Term) is det.
   63%
   64%   As write/1 and writeln/1 to `user_error`.
   65
   66error_write(Term) :-
   67    write(user_error, Term).
   68error_writeln(Term) :-
   69    writeln(user_error, Term).
   70
   71%!  console_write(@Term) is det.
   72%!  console_writeln(@Term) is det.
   73%
   74%   As write/1 and writeln/1 to `user_error`.  The XSB version writes to
   75%   ``STDFDBK``.  What does that mean?
   76
   77console_write(Term) :-
   78    write(user_error, Term).
   79console_writeln(Term) :-
   80    writeln(user_error, Term).
   81
   82%!  warning(@Message) is det.
   83%
   84%   Print a warning. The behaviour depends on the flag `warning_action`,
   85%   which can be one of:
   86%
   87%     - print_warning
   88%       Re-direct the message to SWI-Prolog's print_message
   89%     - error_warning
   90%       Throw error(xsb_warning(Message), _)
   91%     - silent_warning
   92%       Ignore the warning.
   93%
   94%   If Message is a list or   comma-list,  the elements are concatenated
   95%   without a space.
   96
   97warning(Message) :-
   98    current_prolog_flag(warning_action, Action),
   99    warning(Action, Message).
  100
  101warning(silent_warning, _) :-
  102    !.
  103warning(error_warning, Term) :-
  104    !,
  105    throw(error(xsb_warning(Term), _)).
  106warning(_, Term) :-
  107    print_message(warning, xsb_warning(Term)).
  108
  109:- multifile
  110    prolog:message//1.  111
  112prolog:message(xsb_warning(Term)) -->
  113    [ 'XSB:'-[] ],
  114    xsb_warning(Term).
  115prolog:error_message(xsb_warning(Term)) -->
  116    [ 'XSB:'-[] ],
  117    xsb_warning(Term).
  118
  119xsb_warning(List) -->
  120    { is_list(List) },
  121    !,
  122    xsb_warning_list(List).
  123xsb_warning(Var) -->
  124    { var(Var) },
  125    !,
  126    [ '~w'-[Var] ].
  127xsb_warning((A,B)) -->
  128    !,
  129    xsb_warning(A),
  130    xsb_warning(B).
  131xsb_warning(Term) -->
  132    [ '~w'-[Term] ].
  133
  134xsb_warning_list([]) -->
  135    [].
  136xsb_warning_list([H|T]) -->
  137    [ '~w'-[H] ],
  138    xsb_warning_list(T).
  139
  140%!  message(@Message) is det.
  141%!  messageln(@Message) is det.
  142%
  143%   Write message to `user_error`. As warning/1,   Message can be a list
  144%   or comma list.
  145%
  146%   @compat XSB. XSB writes  to  ``STDMSG``.   Possibly  we  should also
  147%   redirect this through SWI-Prolog's print_message/2 interface.
  148
  149message(Message) :-
  150    is_list(Message),
  151    !,
  152    maplist(message_elem, Message).
  153message(Var) :-
  154    var(Var),
  155    !,
  156    message_elem(Var).
  157message((A,B)) :-
  158    !,
  159    message(A),
  160    message(B).
  161message(Term) :-
  162    message_elem(Term).
  163
  164messageln(Term) :-
  165    message(Term),
  166    nl(user_error).
  167
  168message_elem(Term) :-
  169    write(user_error, Term)