Monday, 21 March 2011

Android logcat command - Basic notions

As I wrote here, logcat is a special system implemented on Android to print output. It is similar to System.out.print of Java, but a little bit sophisticate.

There are several way to see the output file. Here comes two ways to see it in the terminal:
1. Directly from the terminal
adb -d logcat
2. From the device terminal
2.1 Access to the Android's shell
adb -d shell
2.2 Invoke logcat
logcat

The command logcat has several options and filters. The structure of it is:
logcat [options] [filterspecs]

You can see all them invoking help:
adb -d logcat --help
or
logcat --help

In the Android official devguide you have the complete options also:

Options
For me, the most interesting options are:
  • -d: This option make the shell dump the log and then exit. Normally the listener of log is always running and it will stop until the device is disconnected or turned off. It is very useful to see just commands from this moment and exit.
  • -s: Set default filter to silent. It works exactly as the filter "*:S" (Explained below). This is used to ensure that only messages with "View" tag and the specified in the command tag will be displayed.
  • -f <filePath+fileName>: Set the output as a file in a specific path. Notices that the root path of android is read only. The best way is storing the file in /sdcard. For example, to store it to "output.txt" file in /sdcard, the option is
adb -f /sdcard/output.txt
  • -b <buffer>: Switch between different buffers. There are three buffers:  main, event and radio. The default one is main. The event buffer is specially useful to see all the lifecycle of the activities happening in the phone (Creating, destroying, pausing, resuming, etc)
Filters
The filters has the follow structure:
<tag>[:priority]
The priority corresponds to the priority that a log message can have. That are:
  • Verbose: V
  • Debug: D
  • Info: I
  • Warning: W
  • Error: E
  • Fatal: F
  • Silent: S
They are organized by the level of restriction. The verbose are the less restrictive one and silent is the most restrictive one.

If a priority is set, then all the messages with that priority or more restrictive priority will be shown.

For example:
logcat -d myApp:D *:S
will show all the messages that has tag "myApp" and has priority D, I, W, E, F and S.
The tag :*S (or the option -s) is very important. It will ensure that only the messages which has specific tag and which has the specific level or more restrictives one will be displayed.

The filters can be put together by writing a priority behind another. For example:
logcat -d myApp:D myApp2:W *:S
will display all the messages that has tag "myApp" which has the priority D or more restrictives one AND all the messages that has tag "myApp2" which has the priority W or more restrictives one.

Things to take account
  • The tag can be work without any kind of priority. The default on is V (verbose)
  • By default, logcat doesn't display the time. If you need it, then the option "-v time" can be used.
  • The tags are case sensitive. That means they are sensitive to mayus-minus. For example, the tag "youtube" is different from the tag "YouTube"
  • Unlike in the eclipse, here is not option to filter the process by pid. One way to do it manually is using grep. Be careful because the pid in the Android has 5 ciphers. If the pid you want to filter has just two ciphers (like 77), you should put some white space before. For example, to filter all the process which has the pid 77, the command could be used is (with three white space):
adb logcat -d | grep "(   77)"

For more information:
Android official devguide: http://developer.android.com/guide/developing/tools/adb.html#logcat

No comments:

Post a Comment