1 | :- module(plspec_logger, [log/3, log/2, set_loglevel/1, possible_loglevel/1]). | |
2 | ||
3 | possible_loglevel(info). | |
4 | possible_loglevel(warning). | |
5 | possible_loglevel(error). | |
6 | possible_loglevel(debug). | |
7 | possible_loglevel(nothing). | |
8 | ||
9 | :- dynamic loglevel/1. | |
10 | loglevel(error). | |
11 | loglevel(warning). | |
12 | ||
13 | set_loglevel(X) :- | |
14 | retractall(loglevel(_)), | |
15 | print(set_loglevel(X)),nl, | |
16 | set_loglevel_aux(X). | |
17 | ||
18 | set_loglevel_aux(debug) :- | |
19 | assertz(loglevel(debug)), | |
20 | assertz(loglevel(warning)), | |
21 | assertz(loglevel(error)), | |
22 | assertz(loglevel(info)). | |
23 | set_loglevel_aux(info) :- | |
24 | assertz(loglevel(info)), | |
25 | assertz(loglevel(error)), | |
26 | assertz(loglevel(warning)). | |
27 | set_loglevel_aux(warning) :- | |
28 | assertz(loglevel(error)), | |
29 | assertz(loglevel(warning)). | |
30 | set_loglevel_aux(error) :- | |
31 | assertz(loglevel(error)). | |
32 | set_loglevel_aux(nothing). | |
33 | ||
34 | log_stream(error,R,Col) :- !,R=user_error,Col=[bold,red]. | |
35 | log_stream(_,user_output,[blue]). | |
36 | ||
37 | :- load_files(library(system), [when(compile_time), imports([environ/2])]). | |
38 | :- if(environ(prob_safe_mode,true)). | |
39 | % avoid loading tools_printing; pathes may not yet be setup | |
40 | %:- use_module(probsrc(tools_printing),[format_with_colour/4]). | |
41 | format_with_colour(Stream,Col,Str,Args) :- | |
42 | catch( | |
43 | tools_printing:format_with_colour(Stream,Col,Str,Args), | |
44 | error(existence_error(_,_),_), % in case tools_printing not loaded yet | |
45 | format(Stream,Str,Args)). | |
46 | :- else. | |
47 | format_with_colour(Stream,_,Str,Args) :- format(Stream,Str,Args). | |
48 | :- endif. | |
49 | ||
50 | log(X, Format, Args) :- | |
51 | (loglevel(X) | |
52 | -> log_stream(X,Stream,Colour), | |
53 | format(Stream,'~w ~w~`.t~20|',[plspec, X]), | |
54 | format_with_colour(Stream,Colour,Format,Args), nl(Stream) | |
55 | ; possible_loglevel(X) -> true | |
56 | ; format(user_error,'plspec internal error: ~w is not a valid loglevel!', [X]) | |
57 | ). | |
58 | ||
59 | log(X, Format) :- | |
60 | log(X, Format, []). |