LogCat

TIP: Using LogCat And Breakpoints

In this tip you will learn how to log messages and exceptions and set breakpoints.


Logging Messages

Sometimes you just want to write out debugging strings to a log. You can do this with the Log methods. What is not so obvious is how to view the log messages in Eclipse. To display the LogCat window choose Window --> Show View --> Other. Then select Android --> LogCat OK. On the right hand pane of Eclipse the LogCat view appears as a tab! There are various methods of logcat. The safest to use may be Log.d or DEBUG. This does NOT mean that you must be in debug mode. This means that calls to Log.d are "compiled, but stripped at runtime." This is useful to those of use with a tendency to forget about calls to Log leaving them scattered about like coding litter! To use Log.d, create a TAG, and call Log.d(tag,msg) as in:

import android.util.*;

    private static final String TAG= "Confuse Text";

        Log.d(TAG,"Test");

With the LogCat window in focus, click on the "+" sign. Enter a name for the filter and the criteria to filter the log output as in:


Click OK. Now you can switch from the "Log" view to the Confuse Text view to view the debug string. Here is our log message when we RUN the app.



Cool!


Using ProGuard to Remove Logging from the Release APK

Here is an unverified TIP:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** i(...);
}
The end result is that these log lines are not in your release apk, and therefore any user with logcat won't see d/v/i logs.

Thanks to David C.

Logging Exceptions

You can log exceptions using Log.d(String tag, String msg, Throwable tr). So the following test code:

        String test= null;
        try {
         test.length();
        }
        catch (Exception e) {
            Log.d(TAG,"test",e);
        }

Results in the following log:



LogCat Failure Eclipse

If LogCat stops working in Eclipse, you may need to quit and restart the IDE or clear the Log (click on the top right icon). As your app runs, if you are in filter view, the Log button in LogCat should dynamically show the number of log statements being recorded, confirming that the LogCat is working. Once you have LogCat working, do not kill the emulator. Just rebuild your app which will load the latest build onto the emulator.


Debug Breakpoint

If you are having trouble getting LogCat to display in a reliable manner, consider setting a breakpoint (right click on the blue line) at the line of code of interest and choose "debug" instead of "run". The code will stop at this point and you will be able to view the value of the variable. Here is a snapshot of a breakpoint:


When this line of code is encountered in the debug mode, the code stops and the value of e is displayed in the variable window in the debug view. If the code does not stop at your breakpoint, it suggest a problem in your code flow that is not calling the line of code as expected.


You can continue after a breakpoint as in:

 Resume  Continue normally or until the next break point
 Step Into  Run the next line of code in the method call, or go to the next line of code if not a method call
 Step Over  Execute the entire method call and then break on the next line of code, or go to the next line of code if not a method call
 Step Return Execute the rest of this method and break at the return point in the code that called this method


TIP: Running Emulator

You DO NOT need to quit the emulator between builds!


Toast

You can also put up a brief debug box using Toast as in:

Toast.makeText(this,"onCreate", Toast.LENGTH_SHORT).show();


Cool!
JAL
Comments