سؤال

In a debug effort to understand what the system is doing I would like to use the java debugger conditional breakpoints mechanism to print log messages on specific lines in Android's source code.

How would I do it? I don't exactly know, that's this question all about.

Things I do know:
The Eclipse java debugger has "conditional Breakpoints", i.e., you set a breakpoint in the code and you write a condition. Each time the code reaches the breakpoint line it will stop execution only if the condition you wrote is true, otherwise it will not stop.

I though that maybe this mechanism can be twisted to print a log message at a specific line in the code instead of stopping the execution. And if it can be done I could be able to track what Android's source code is doing.

هل كانت مفيدة؟

المحلول

You can use the following expression in a conditional breakpoint:

android.util.Log.v("MyApp", "my message") == -1

This will make your breakpoints not break the execution and in the LogCat View, filtering the view using the tag:MyApp expression, you will get the desired logged messages. You can also use other log levels (a, i, e, w). See the Log documentation for more info.

To have the breakpoint always break, use:

android.util.Log.v("MyApp", "my message") >= 0

The return value of Log.v is the number of bytes the log message internally has, so it will always a positive number.

ADDENDUM: There is a more elegant way to do it. Eclipse conditional breakpoints can be any java code, including using local variables at the breakpoint. The last return value must be true if you actually want to stop execution or false. So the same can be achieved as:

android.util.Log.v("MyApp", "my message");
return false;

or you can make more complicated statements like:

if( t.isNumber() ) {
    System.out.println("A number found");
} else {
    System.out.println("Not a number");
    return true; // stops execution
}
android.util.Log.v("MyApp", "my message");
return false; // does not stop execution
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top