Debug Lines


#1

Is there anything in julia at the moment, that allows statements to be printed that give an idea of what is going on inside a function, only if the script or function is run during a debug mode?

Something like @debug println("Finished looping: i is $(i)")?


#2

No, not at the moment.


#3

I just wrote my own trivial macro for that, which I use in conjunction with my little https://github.com/ScottPJones/StringUtils.jl (unregistered) package to get easily formatted debugging output.

const global debug = [false]
macro deb(str)
    :( debug[] && println($(esc(str))) )
end

and in the code I have things like:

@deb(u"These are floats: \%8.2f(1.2345), \%8.3f(9.8765)")

and get nicely formatted output like:

These are floats:     1.23,    9.877

Then to enable debugging output of that module, I simple do: Foo.debug[] = true.


#4

@maleadt has implemented something similar for CUDAdrv at https://github.com/JuliaGPU/CUDAdrv.jl/blob/03c7b94fb0072b217089ac86e80863e418c1799a/src/util/logging.jl

The idea is that you can start a Julia session with,

TRACE=1 julia --compilecache=no ./program.jl

--compilecache=no is necessary since TRACE=1 is checked at compilation time and if CUDAdrv is precompiled, we can’t switch debugging on.


#5

We do very much need a lightweight, extensible standard way of doing info/debug logging in the standard library.


#6

I’ve been using Logging.jl for this type of thing, with good results.


#7

We ended up coming up with our own macros for logging also (with a bit of weird syntax magic I got from my friends on the https://gitter.im/JuliaLang/julia Gitter chat room.
@error(()->errorcode, message), @fatal(()->errorcode, message), @warn(()->errorcode, message), @info(()->errorcode, message)
The macro doesn’t execute the anonymous function, but instead uses it to get the __FILE__ and __LINE__ information, which unfortunately is otherwise unavailable from within a macro call (actually just one of them isn’t).
We use other flags and level settings to determine whether the macros 1) get compiled into the code (for warning and info messages) or 2) it is logged or not (run-time check based on level/flags)