View source with formatted comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2017, 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(swish_logging,
   36	  [ create_log_dir/0,
   37	    create_log_dir/1
   38	  ]).   39:- use_module(library(http/http_log)).   40:- use_module(library(broadcast)).   41:- use_module(library(settings)).   42:- use_module(library(apply)).   43
   44/** <module> Add SWISH query execution to the HTTP log file
   45
   46Add a line of the format below to  the HTTP log file. The src_text(Text)
   47option of Options is replaced by   `Hash-Text`  for the first occurrence
   48and just `Hash` for subsequent occurrences.
   49
   50  ==
   51  pengine(Time, create(Pengine, Application, Options))
   52  ==
   53*/
   54
   55:- setting(swish:logging, boolean, true,
   56	   "Enable/disable logging of SWISH query execution").   57
   58:- listen(pengine(Action), swish_log(Action)).   59
   60swish_log(create(Pengine, Application, Options0)) :-
   61	maplist(hash_option, Options0, Options),
   62	get_time(Now),
   63	format_time(string(HDate), '%+', Now),
   64	http_log('/*~s*/ pengine(~3f, ~q).~n',
   65		 [HDate, Now, create(Pengine, Application, Options)]).
   66swish_log(send(Pengine, Event)) :-
   67	get_time(Now),
   68	format_time(string(HDate), '%+', Now),
   69	http_log('/*~s*/ pengine(~3f, ~q).~n',
   70		 [HDate, Now, send(Pengine, Event)]).
   71
   72:- dynamic
   73	text_hash/3,
   74	gc_text_hash_time/1.   75
   76hash_option(src_text(Text), src_text(Result)) :- !,
   77	(   text_hash(Text, _, Hash)
   78	->  Result = Hash
   79	;   variant_sha1(Text, Hash),
   80	    get_time(Now),
   81	    assert(text_hash(Text, Now, Hash)),
   82	    gc_text_hash,
   83	    Result = Hash-Text
   84	).
   85hash_option(Option, Option).
   86
   87gc_text_hash :-
   88	gc_text_hash_time(Last),
   89	get_time(Now),
   90	Now - Last < 900, !.
   91gc_text_hash :-
   92	get_time(Now),
   93	retractall(gc_text_hash_time(_)),
   94	asserta(gc_text_hash_time(Now)),
   95	Before is Now - 3600,
   96	(   text_hash(Text, Time, Hash),
   97	    Time < Before,
   98	    retractall(text_hash(Text, Time, Hash)),
   99	    fail
  100	;   true
  101	).
  102
  103%!	create_log_dir is det.
  104%!	create_log_dir(FileSpec) is det.
  105%
  106%	Create the directory for holding the log files
  107
  108create_log_dir :-
  109	setting(http:logfile, Term),
  110	create_log_dir(Term).
  111
  112create_log_dir(Term) :-
  113	directory_spec(Term, DirSpec),
  114	(   absolute_file_name(DirSpec, _,
  115			       [ file_type(directory),
  116				 access(write),
  117				 file_errors(fail)
  118			       ])
  119	->  true
  120	;   absolute_file_name(DirSpec, Dir,
  121			       [ solutions(all)
  122			       ]),
  123	    catch(make_directory(Dir), _, fail)
  124	->  true
  125	).
  126
  127directory_spec(Atom, Dir) :-
  128	atomic(Atom), !,
  129	file_directory_name(Atom, Dir).
  130directory_spec(Term, DirTerm) :-
  131	Term =.. [Alias,Atom],
  132	file_directory_name(Atom, Dir),
  133	DirTerm =.. [Alias,Dir]