Sunday, February 20, 2011

Write and view debug output from Eclipse for Android

Sooner or later, you will need the debug output to help you in Android development. The old System.out.println still works, but you need to view the output from Android LogCat instead of Eclipse Console window. To open and view the output of LogCat, open Eclipse menu of Window->Show View->Others->Android->LogCat.
However, although the debug output is available in LogCat, there are quite a lot of other debug output, which really makes it difficult to find the log you are interested. To handle the issue, Log class should be used to write the debug output, since Log class provides a way to specify a log tag for each log entry, and you can filter the entries in LogCat by log tag.

public class myActivity extends Activity {
public static final String MyActivityTag = "MyActivityTag";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(MyActivityTag, "MyActivity onCreate");

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Note you can add multiple log filter by clicking the '+' icon in Log Cat view. For each log filter, a separate log output tab page will be created for showing the log entries belonging to this log tag.

Jonathan

No comments:

Post a Comment