Recipe Using Log Cat

Logcat is the real-time logging tool Android provides. It collects all system and application log data in circular buffers, which can then be viewed and filtered. It can be accessed as a standalone tool or as part of the DDMS tool.

LogCat can be used in the device after executing the adb shell to log into the device or by using the logcat command through the adb:

> [adb] logcat [<option>] ... [<filter-spec>] ...

All the messages that utilize the android.util.Log class have an associated tag and priority. The tag should be meaningful and related to what the activity does.The tag and priority make the logging data easier to read and filter. Possible tags are

■ S—Silent (highest priority, on which nothing is ever printed)

The LogCat data has a multitude of information, and filters should be used to avoid overload by specifying the tag:priority argument to the logcat command. For example:

> adb logcat ActivityManager:V *:S

This shows verbose (V) data on the ActivityManager while silencing (S) all other log commands.

A circular buffer system is used inside Android logging. By default, all information is logged to the main log buffer.With the Android 2.2 SDK, there are two other buffers: one that contains radio/telephony-related messages and one that contains event-related messages. Different buffers can be enabled using the -b switch. For example:

> adb logcat -b events

This buffer also shows event-related messages: I/menu_opened( 135): 0

I/notification_cancel( 74): [com.android.phone,1,0] I/am_finish_activity( 74):

[1128378040,38,com.android.contacts/.DialtactsActivity,app-request] I/am_pause_activity( 74):

[1128378040,com.android.contacts/.DialtactsActivity]

I/am_on_paused_called( 135): com.android.contacts.RecentCallsListActivity I/am_on_paused_called( 135): com.android.contacts.DialtactsActivity I/am_resume_activity( 74): [1127710848,2,com.android.launcher/.Launcher] I/am_on_resume_called( 135): com.android.launcher.Launcher I/am_destroy_activity( 74):

[1128378040,38,com.android.contacts/.DialtactsActivity] I/power_sleep_requested( 74): 0 I/power_screen_state( 74): [0,1,468,1]

I/power_screen_broadcast_send( 74): 1 I/screen_toggled( 74): 0

I/am_pause_activity( 74): [1127710848,com.android.launcher/.Launcher] Another example follows

> adb logcat -b radio

This shows radio/telephony-related messages:

D/RILJ ( 132): [2981]< GPRS_REGISTRATION_STATE {1, null, null, 2} D/RILJ ( 132): [2982]< REGISTRATION_STATE {1, null, null, 2, null, null, null, null, null, null, null, null, null, null} D/RILJ ( 132): [2983]< QUERY_NETWORK_SELECTION_MODE {0}

D/GSM ( 132): Poll ServiceState done: oldSS=[0 home T - Mobile T - Mobile 31026 Unknown CSS not supported -1 -1RoamInd: -1DefRoamInd: -1] newSS=[0 home T - Mobile T - Mobile 31026 Unknown CSS not supported -1 -1RoamInd: -1DefRoamInd: -1] oldGprs=0 newGprs=0 oldType=EDGE newType=EDGE [UNSL]< UNSOL_NITZ_TIME_RECEIVED 10/06/26,21:49:56-28,1 NITZ: 10/06/26,21:49:56-28,1,237945599 start=237945602

[UNSL]< UNSOL_RESPONSE_NETWORK_STATE_CHANGED

[2984]> OPERATOR

[2985]> GPRS_REGISTRATION_STATE

[2984]< OPERATOR {T - Mobile, T - Mobile, 31026}

[2986]> REGISTRATION_STATE

[2987]> QUERY_NETWORK_SELECTION_MODE

[2985]< GPRS_REGISTRATION_STATE {1, null, null, 2}

[2986]< REGISTRATION_STATE {1, null, null, 2, null, null, null, null, null, null, null, null, null, null}

D/RILJ ( 132): [2987]< QUERY_NETWORK_SELECTION_MODE {0}

Logcat is useful when using Java-based Android applications. However, when applications involve native components, it is harder to trace. In this case, the native components should log to system.out or system.err. By default, the Android system sends stdout and stderr (system.out and system.err) output to /dev/null. These can be routed to a log file with the following ADB commands:

> adb shell stop

> adb shell setprop log.redirect-stdio true

> adb shell start

This stops a running emulator/device instance; use the shell command setprop to enable the redirection of output and restart the instance.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment