# Debug Lines

**URL:** <https://discourse.julialang.org/t/debug-lines/192>\
**Category:** General Usage\
**Created:** [November 8, 2016, 1:50pm UTC](https://discourse.julialang.org/t/debug-lines/192 "2016-11-08T13:50:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [November 8, 2016, 1:50pm UTC](https://discourse.julialang.org/t/debug-lines/192/1 "2016-11-08T13:50:28Z")

</div>

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)")`?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 8, 2016, 2:36pm UTC](https://discourse.julialang.org/t/debug-lines/192/2 "2016-11-08T14:36:40Z")

</div>

No, not at the moment.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [November 8, 2016, 4:04pm UTC](https://discourse.julialang.org/t/debug-lines/192/3 "2016-11-08T16:04:23Z")

</div>

I just wrote my own trivial macro for that, which I use in conjunction with my little [https://github.com/ScottPJones/StringUtils.jl](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`.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [November 9, 2016, 1:15am UTC](https://discourse.julialang.org/t/debug-lines/192/4 "2016-11-09T01:15:47Z")

</div>

@maleadt has implemented something similar for `CUDAdrv` at [CUDAdrv.jl/logging.jl at 03c7b94fb0072b217089ac86e80863e418c1799a · JuliaGPU/CUDAdrv.jl · GitHub](https://github.com/JuliaGPU/CUDAdrv.jl/blob/03c7b94fb0072b217089ac86e80863e418c1799a/src/util/logging.jl)

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 9, 2016, 4:19pm UTC](https://discourse.julialang.org/t/debug-lines/192/5 "2016-11-09T16:19:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crbinz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crbinz/32/1919_2.png) [@crbinz](https://discourse.julialang.org/u/crbinz)\
**Post date:** [November 9, 2016, 11:55pm UTC](https://discourse.julialang.org/t/debug-lines/192/6 "2016-11-09T23:55:24Z")

</div>

I’ve been using [Logging.jl](https://github.com/kmsquire/Logging.jl) for this type of thing, with good results.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [November 10, 2016, 12:34pm UTC](https://discourse.julialang.org/t/debug-lines/192/7 "2016-11-10T12:34:49Z")

</div>

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 [JuliaLang/julia - Gitter](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)
