On 13/02/2013 07:54, Basile Starynkevitch wrote: > Hello All, > > In C or C++ I would routinely have > > #define dbgprintf(Fmt,...) do{if(debugging) printf("%s:%d:" Fmt "\n", __FILE__,__LINE__, ##__VA_ARGS__);}while(0) > > and then I would use at many places things like (in file foo.c line 123) > dbgprintf("here x=%d", x); > with an output like > foo.c:123: Here x=10 > > > Is it possible to get the line number of the invoking super-goal in Eclipse Prolog? Hi Basile, First, I would agree with Kish in recommending the use of tkeclipse and its debugger, at least that's what I do for debugging. I think it is fairly easy to use and reasonably quick to learn. You can set breakpoints with a mouse click, inspect data, etc. Having said that, I've taken up your little challenge, just to prove that there is little that can't be done with ECLiPSe ;) % The predicate that prints the message dbg_printf(Name,Fmt,Args,File,Line) :- ( debug_wanted(Name) -> printf(output, "*| %s:%d:%n",[File,Line]), printf(output, "*| %a ",[Name]), printf(output, Fmt, Args), nl ; true ). % Compile-time substitution of dbg_printf/3 with dbg_printf/5 :- inline(dbg_printf/3, expand_dbg/4). % uncomment next line to remove all dbg_printfs at compile time %expand_dbg(dbg_printf(_,_,_), true, _, _). expand_dbg(dbg_printf(Name,Fmt,Args), dbg_printf(Name,Fmt,Args,File,Line), OldAnnGoal, _NewAnnGoal) :- ( var(OldAnnGoal) -> File = 'unknown', Line=0 ; % get source position from the annotated source term OldAnnGoal = annotated_term{file:File,line:Line} ). % example debug_wanted(here). test :- X=foo(Y), dbg_printf(here, "X=%w", [X]), Y=bar(Z), dbg_printf(here, "X=%w", [X]), Z=baz, dbg_printf(here, "X=%w", [X]). And this is what it prints: [eclipse 2]: test. *| //C/cygwin/home/jschimpf/SF/Main/Kernel/x86_64_nt/dbg_printf.ecl:27: *| here X=foo(_159) *| //C/cygwin/home/jschimpf/SF/Main/Kernel/x86_64_nt/dbg_printf.ecl:29: *| here X=foo(bar(_179)) *| //C/cygwin/home/jschimpf/SF/Main/Kernel/x86_64_nt/dbg_printf.ecl:31: *| here X=foo(bar(baz)) Yes (0.00s cpu) Cheers, JoachimReceived on Wed Feb 13 2013 - 20:13:33 CET
This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST