View source with raw 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)  2015, 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	  [
   37	  ]).   38:- use_module(library(http/http_log)).   39:- use_module(library(broadcast)).   40:- use_module(library(settings)).   41:- use_module(library(apply)).

Add SWISH query execution to the HTTP log file

Add a line of the format below to the HTTP log file. The src_text(Text) option of Options is replaced by Hash-Text for the first occurrence and just Hash for subsequent occurrences.

pengine(Time, create(Pengine, Application, Options))

*/

   54:- setting(swish:logging, boolean, true,
   55	   "Enable/disable logging of SWISH query execution").   56
   57:- listen(pengine(Action), swish_log(Action)).   58
   59swish_log(create(Pengine, Application, Options0)) :-
   60	maplist(hash_option, Options0, Options),
   61	get_time(Now),
   62	format_time(string(HDate), '%+', Now),
   63	http_log('/*~s*/ pengine(~3f, ~q).~n',
   64		 [HDate, Now, create(Pengine, Application, Options)]).
   65swish_log(send(Pengine, Event)) :-
   66	get_time(Now),
   67	format_time(string(HDate), '%+', Now),
   68	http_log('/*~s*/ pengine(~3f, ~q).~n',
   69		 [HDate, Now, send(Pengine, Event)]).
   70
   71:- dynamic
   72	text_hash/3,
   73	gc_text_hash_time/1.   74
   75hash_option(src_text(Text), src_text(Result)) :- !,
   76	(   text_hash(Text, _, Hash)
   77	->  Result = Hash
   78	;   variant_sha1(Text, Hash),
   79	    get_time(Now),
   80	    assert(text_hash(Text, Now, Hash)),
   81	    gc_text_hash,
   82	    Result = Hash-Text
   83	).
   84hash_option(Option, Option).
   85
   86gc_text_hash :-
   87	gc_text_hash_time(Last),
   88	get_time(Now),
   89	Now - Last < 900, !.
   90gc_text_hash :-
   91	get_time(Now),
   92	retractall(gc_text_hash_time(_)),
   93	asserta(gc_text_hash_time(Now)),
   94	Before is Now - 3600,
   95	(   text_hash(Text, Time, Hash),
   96	    Time < Before,
   97	    retractall(text_hash(Text, Time, Hash)),
   98	    fail
   99	;   true
  100	)