Saturday, May 21, 2011

Handy Android Debugging Tip!

Well, completely ignoring the 2+ year hiatus since I last wrote on this blog (hi all you folks who are still subscribed!), wanted to note down a quick tip for Android developers.  I've been working on a new project lately which has me working in Rails and Android, and I'm having a blast with both.

When building your awesome Android app, you'll often use the logcat output to debug stuff.  Example:

log.d(Constants.REMOVE, "Last updated date is " + lastUpdated.toString());

So, here's the cheesy-simple tip.  Sometimes you want to get the context of exactly how you got to the method in question when logging something like this.  In the case above, this is in the midst of deserializing some JSON and we could get to this code down a variety of paths.  So to get a stacktrace in your log output, just use the other form of the Log call (such as you might normally do within a catch block) with a freshly-minted Exception that you don't throw:

log.d(Constants.REMOVE, "Last updated date is " + lastUpdated.toString(), new Exception());

And voilĂ !  (by the way, I use the Constants.REMOVE tag for transient debug logging, so that I can do an easy sweep of my code to remove them before checkins)

No comments: