Thursday, April 22, 2010

Android: DDMS and filtering Logcat output

One of the most useful tools in any Android developers arsenal is logcat. Type "adb shell logcat" from the command line and you will be presented with a stream of messages. This isn't particularly useful if you want to filter out all the noise and concentrate on your application logs. However, it is useful for viewing what other applications are doing. It can often be quite interesting to run applications and watch logcat. When Google first released the ADC tool allowing Android users to vote on ADC II competition entries, there was plenty of interesting output to say the least.

Google have provided an ADT plugin if you are developing for Eclipse (you may have installed this during the SDK installation). Once you have the ADT plugin installed, click in the top right corner on the plus icon near where it says "Java". Select "other" and you will be presented with a list of options.



Select "DDMS" from the list and you will have a new view available to you. You can move the panes around to suit your personal preference. Below is a snapshot of my setup.



You can also launch this tool directly from the command line. Type "ddms" from the command line and you will see the DDMS tool start (providing you have setup Android correctly on your host machine).



If you would like a thorough understanding of how to use the tool I suggest you read the DDMS documentation.


Now we have DDMS running lets go back to our original purpose, filtering out the logcat noise. In the logcat pane you will see a '+' icon. Click on this to bring up the log filter.



Give the filter a name and fill out the attributes you want to select from the logcat output. In our example we are going to grab all the "dalvikvm" messages. You will now have a new tab in the logcat view to select. This tab will only display messages that meet your criteria.



After playing around with the phone for a couple of minutes our dalvikvm tab now shows output. If we cycle back to the main logcat tab we can see all the messages except anything that matches our selection criteria.





While developing, I will have logcat running in full screen on one of my monitors. This allows me to keep an eye on what the system is doing. Make sure before you release to clean up all your logcat output. An easy solution to this is create a final boolean such as DEBUG_MODE that you can set to true/false before compiling. We have all been guilty of releasing products that output something they shouldn't. This simple feature goes a long way to cleaning that up. Not to mention is makes live much easier by only have to change a single value before releasing your application into the wild.

final static boolean DEBUG_MODE = false;

if (DEBUG_MODE) Log.i(LOG_TAG,"Load level: " + mLevel);


Having a logcat filter setup for your application will not only stop your messages getting lost amongst the system output. It will also allow you to quickly tell if you are producing any logcat output before releasing to market.

2 comments:

ross said...

Great point about getting in the habit of prefixing all Log.* statements with "if (DEBUG_MODE)". Too many times I'm flipping // on and off in key spots.

chriv said...

I'm surprised that DDMS doesn't have a way (at least that I can find) to filter out messages by a criteria. For instance, I want to filter out all of the crap sent to logcat with a tag of AlarmManager or SMD.

Creating a filter for tag AlarmManager shows me all the AlarmManager messages only (exactly the opposite of what I want to do). :-(