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)  2009-2020, University of 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(stream_info,
   37          [ stream_info/1               % +Stream
   38          ]).   39:- autoload(library(error),[existence_error/2]).   40:- autoload(library(lists),[member/2]).   41
   42:- use_foreign_library(foreign(streaminfo)).   43
   44%!  stream_info(+Stream) is det.
   45%
   46%   Print detailed information about a stream   or  a file-number to
   47%   the error output. The  output  of   this  command  is  meant for
   48%   experts and requires  knowledge  about   the  implementation  of
   49%   streams. It has been  added  to   diagnose  leaking  streams  in
   50%   web-servers. For example,  on  linux   systems  we  can  examine
   51%   process file-descriptors using
   52%
   53%   ==
   54%   % ls -l /proc/<pid>/fd
   55%   ==
   56%
   57%   If now (say) descriptor 15 is open   where  it should not be, we
   58%   can this command to find the associated Prolog streams and print
   59%   as mush as possible information about the stream.
   60%
   61%   ==
   62%   ?- stream_info(15).
   63%   ==
   64%
   65%   @param  Stream  A stream-handle, alias name, (integer) system
   66%           file handle or `'<stream>(address)'` atom.
   67
   68stream_info(Stream) :-
   69    is_stream(Stream),
   70    !,
   71    forall(stream_property(Stream, P),
   72           print_property(P)),
   73    nl,
   74    catch('$stream_info'(current_output, Stream), E, true),
   75    (   nonvar(E)
   76    ->  format('~w:~t~25|~q~n', ['pending exception', E])
   77    ;   true
   78    ).
   79stream_info(FileNo) :-
   80    integer(FileNo),
   81    !,
   82    findall(S, stream_property(S, file_no(FileNo)), Streams),
   83    length(Streams, Len),
   84    format('File no ~w is connected to ~d streams~n', [FileNo, Len]),
   85    forall(member(Stream, Streams),
   86           (   format('****************~nStream ~p:~n', [Stream]),
   87               stream_info(Stream))).
   88stream_info(Atom) :-
   89    atom(Atom),
   90    (   stream_property(Stream, type(_)),
   91        format(atom(Atom), '~p', [Stream])
   92    ->  stream_info(Stream)
   93    ;   existence_error(stream, Atom)
   94    ).
   95
   96print_property(P) :-
   97    P =.. [Name,Value],
   98    !,
   99    format('~w:~t~25|~q~n', [Name, Value]).
  100print_property(input) :- !.
  101print_property(output) :- !.
  102print_property(P) :-
  103    format('~p~n', [P])