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
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
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]