Debug Logging In Swift


In the last iOSDevWeekly Dave Verwer listed Art Sabintsev’s PrintLnMagic - a small function that reproduces the common DLog pattern that many use which not only prints a value, but the filename, the function name and the line of the call. Which is really handy in debugging.

I use a version of Dlog myself, which has the added benefit of only outputting to the console when in Debug configurations, which PrintLnMagic does not do.

So, I wrote this

I’m not overriding printLn() because I’m not sure clobbering such a widely used system function is a good idea.

Of course Swift projects don’t work the same way as Objective-C projects, so it isn’t enough to just have the debug configuration, this needs to have the -D DEBUG flag set in the “Other Swift Flags” section under Debug.

Installation is simple enough, this is just a single bare function, so just download the file and add it to your project. Call loggingPrintln() just as you would println(), only passing a value for the first parameter; the defaults will take care of the rest.

Update Feb 5, 2015

Rather than just passing an object or a value, the function can now take an expression for the first parameter. That way, the expression is only evaluated if the function body runs. Laziness is a virtue.

Update Oct 8, 2015

Changed to support Swift 2.

Thanks to rob_rix and jl_hfl for the suggestion.