Developer

Save coding time by using the Android LogCat facility

Android's LogCat facility and the associated Log class can significantly cut down the time required to "type in" a program.

Over the years I've worked with quite a few interesting characters — software engineering attracts all sorts. When I was just starting out, I attended a meeting run by a project manager who went around the table asking each developer for the status of his or her current assignment. He was almost around the table when he got to Dave.

I was new at the company, but even so, I had already deduced that Dave had sort of a reputation around the office. He was older than the rest of us, NBA tall, and had silver hair that hung down to the middle of his back. When he typed his fingers were a blur, and he was known for being a brilliant engineer. He was also rumored to be a procrastinator and be a bit of a smart ass.

"Dave?" the PM asked again.

"Oh right," Dave answered. "My turn. Well my program is done. I finished it while eating dinner last night."

"That's great!" the PM exclaimed. "How soon before we can get the code into beta?"

"Oh, not long," Dave said. "Like I said I finished the program last night while eating dinner. Now all I have to do is type it in."

Everyone except the PM burst into laughter. Those of you who have been writing code for a while know why. No matter how long you've been an engineer, and no matter how good you are at it, the process of actually banging out the code and working out the bugs is the bulk of the work.

Writing for mobile devices is no exception. However, Android provides an integrated tool as part of the Eclipse environment that can significantly cut down the time required to "type in" your program. I'm talking about the LogCat facility and the associated Log class.

To use the class, you import android.util.Log and then pepper your code with Log statements. The simple tutorial below demonstrates the basics. Feel free to follow along, or download and import the entire project here.

1. Start a new project in Eclipse. Target Android 1.6 or higher. Be sure to change your startup activity to Main.

2. Edit the Main.java file to include a couple of nested dummy functions.

Main.java
package com.authorwjf.debuglogging;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
functionA();
}
private void functionA() {
functionB();
}
private void functionB() {
}
}

3. Let's make static calls to the logger. The class is pretty straightforward, but it never hurts to read the official documentation. There are basically five functions that are of interest: Log.v(), Log.d(), Log.i(), Log.w(), and Log.e(). These represent errors, warnings, informational, debug, and verbose messages, respectively. Knowing the "level" of logging is important for filtering out your messages later. For this demo, I selected Log.i(). Below is the same Main.java source file from above, but now I am logging function entry and exit.

Main.java
package com.authorwjf.debuglogging;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("onCreate:", "Enter");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
functionA();
Log.i("onCreate:", "Exit");
}
private void functionA() {
Log.i("functionA:", "Enter");
functionB();
Log.i("functionA:", "Exit");
}
private void functionB() {
Log.i("functionB:", "Enter");
Log.i("functionB:", "Exit");
}
}
That's really all the code we need for the demonstration. Run the code in Eclipse on the emulator, switch to Debug Perspective, and open the LogCat window. You should see something like Figure A. Figure A

This is a simple example, but if you are not already using the LogCat facility as part of your daily development process, I suspect the wheels are suddenly turning. In my experience, adding basic logging to every program I write as I write it makes life a whole lot easier. Maybe not easy enough that I can finish my code while eating dinner, but still, on the road from conceptual to tangible, LogCat can be your best traveling buddy.

About William J. Francis

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

Editor's Picks

Free Newsletters, In your Inbox