-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdebug.pro
More file actions
55 lines (36 loc) · 1.49 KB
/
Copy pathdebug.pro
File metadata and controls
55 lines (36 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
:- use_module(library(ansi_term)). % Colorize outputs written from directives.
% ----------------- monkey-patching
/*
Monkey-patching SWI-Prolog 7.6.4 library(error)
because with_output_to/2 clobbers the backtrace.
Who is to blame: with_output_to/2 or setup_call_cleanup/3?
Another problem: If a directive throws an exception, no backtrace is printed.
Only an unhelpful "Goal (directive) failed".
*/
% Turn this to true if you suspect that the backtrace is incomplete.
is_debugging(false).
% -------------------- checking
/** fatal(?Error) is failure.
Prints back-trace where it is raised, if not is_debugging(false).
*/
fatal(E) :- is_debugging(false), !, throw(E).
fatal(E) :- print_message(error,E),
get_prolog_backtrace(32,Backtrace),
% get_prolog_backtrace, get_prolog_backtrace, fatal
Backtrace = [_,_,_|Backtrace0],
print_prolog_backtrace(user_error,Backtrace0),
throw(E).
:- use_module(library(error)).
:- abolish(error:domain_error/2).
error:domain_error(T,V) :- fatal(error(domain_error(T,V),_)).
% -------------------- workaround for lost exceptions thrown from directives
% This is for getting the backtrace of exceptions thrown from directives.
:- if(getenv('DEBUG',_)).
:- debug.
user:prolog_exception_hook(error(E,_), error(E,Backtrace), _, _) :-
get_prolog_backtrace(32, Backtrace).
prolog:message(error(E,C)) -->
{print_prolog_backtrace(string(S), C)},
(prolog:error_message(E) ; ["~w"-[E]]), !,
["~n~w~n"-[S]].
:- endif.