ADB Logcat Bootcamp

ADB Logcat Bootcamp
HOW TO USE ADB LOGCAT
The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers, which then can be viewed and filtered by the logcat command. You can use logcat from an ADB shell to view the log messages.

These are the basics of logcat. It will show you how to use it. Once you master this you can show the developer what went wrong on your phone instead of telling him a novel-long story.

How do I install it?
1. Make sure you've enabled on USB debugging on your phone in Settings -> Application -> Development
2. Get the Android SDK here
3. Extract the SDK to any folder, i.e. C:\Android
4. Make sure that you have installed the drivers for your phone and the operating system recognizes your phone.

Specific commands are in different colors:
UNIVERSAL COMMANDS
LINUX COMMANDS
WINDOWS COMMANDS.

When I say Terminal, I mean Command Prompt for Windows users (go to start - choose run - type command cmd - ok)


In Terminal with your phone plugged into the computer:


Logcat

Code:
adb logcat

This doesn't START logcat, this tells terminal to grab the information already on the device logcat and display it in terminal. This isn't so useful to us. It just scrolls the information in terminal and you can read it there. This is kinda difficult to read though, for one it's constantly scrolling as your phone does things and two, it's likely that your terminal is configured to only allow a certain number of lines to be kept readable before they drop off.


Pipe it to Desktop as a .txt file

Code:
adb logcat > ~/Desktop/logcat.txt
adb logcat > %userprofile%\desktop\logcat.txt

This command above will tell the logcat to export the terminal logcat to a .txt file on your desktop called logcat.txt The '>' symbol tells the logcat to pipe to the location listed. This will continue to update even if you open the text file, so long as you have terminal running. It's not done “live” though, you have to either refresh the file, or close it then re-open it. That won't affect anything other than giving you an update. Now we're getting somewhere, but where?


Code:
adb logcat > /sdcard/logcat.txt

If you are using Terminal Emulator on your phone instead of a computer setup, this (above) is the code you'd want to use. It will save the logcat.txt to the root of your SD card. Next!


-v long

Code:
adb logcat -v long > ~/Desktop/logcat.txt
adb logcat -v long > %userprofile%\desktop\logcat.txt

(the -v option sets the output format. -v long after the logcat command formats the log so that it adds a date and time stamp to each line. It also separates each line with a blank line... making the log as a whole much easier to look through)

Now we're telling the logcat to do something more interesting. We are telling it to give us every scrap of information it has. This will space the logcat out nice and pretty and really make things easier to read as well, even giving time stamps of when everything happened! Now you can say “it happened at about 9:30 pm” and developer can find that. Winning!

Sometimes you want to filter down the information though. You want to make the dev's life easier. Here is how:

A brief on Tags and Priorities

Tags are going to be what process is actually giving the information, for example 'ActivityManager', 'Database', and 'WindowsManager' are all Tags you can find. There are TONS of these suckers! Research into what your problem is and try to pick out the tag.

Priorities are different. These will tell you how serious the issue at hand is. The priorities are called by their letter code and are:

V = Verbose
D = Debug
I = Info
W = Warning
E = Error
F = Fatal
S = Silent (highest priority, suppress all output)

These are in ascending order. In other words, Verbose or V is going to be the micro information which doesn't really mean much to anyone 99% of the time where as Fatal or F is going to be a huge catastrophic issue. When filtering for a Priority it will include the Priority you give PLUS all HIGHER Priorities. So, for example, if you call to filter for Warning or W then it will give you Warning, Error, and Fatal. That is common to filter for. Below are some examples of code:

(you would never actually type or input{‘ or ‘}’ in your logcat commands .. they are in some of the examples below to show you that these are generic modifiers … meaning - if you were inputting the command you would replace the {Tag} with an actual Tag, like: ActivityManager or GTalkService .. in the same way you would replace {Priority} with an actual Priority, such as: W or E)


Examples

Code:
adb logcat {Tag}:{Priority} *:S > ~/Desktop/logcat.txt
adb logcat {Tag}:{Priority} *:S > %userprofile%\desktop\logcat.txt

The above line is if you know exactly what Tag (GTalkService or ActivityManager) and Priority (W or E) you are looking for.

Code:
adb logcat *:{Priority} *:S > ~/Desktop/logcat.txt
adb logcat *:{Priority} *:S > %userprofile%\desktop\logcat.txt

The above line is if you don't know the Tag, but know the Priority. The * is a wild card that basically means all/any.

An example of a VERY valuable logcat could be:

Code:
adb logcat *:W *:S > ~/Desktop/logcatALLwarnings.txt
adb logcat *:W *:S > %userprofile%\desktop\logcat.txt

So, the command above would give you all tags that had a priority of Warning, Error, or Fatal. It would silence (not show) everything else and would pipe the output of your log to your desktop as a text file named logcatALLwarnings.txt

Example of a buggy app:

Code:
D/dalvikvm( 1287): GC freed 3 objects / 72 bytes in 92ms
D/dalvikvm( 2216): GC freed 6646 objects / 313032 bytes in 104ms
D/NetworkLocationProvider( 75): onCellLocationChanged [***]
D/LocationManager( 2216): removeUpdates: listener = com.levelup.beautifulwidgets .UpdateWeather$2@447843d8
D/dalvikvm( 1253): GC freed 281 objects / 12688 bytes in 93ms
D/NetworkLocationProvider( 75): onCellLocationChanged [***]
D/LocationManager( 2216): removeUpdates: listener = com.levelup.beautifulwidgets .UpdateWeather$2@447843d8
D/dalvikvm( 75): GC freed 41839 objects / 1963448 bytes in 88ms
D/dalvikvm( 1287): GC freed 3 objects / 72 bytes in 94ms
D/NetworkLocationProvider( 75): onCellLocationChanged [***]
D/LocationManager( 2216): removeUpdates: listener = com.levelup.beautifulwidgets .UpdateWeather$2@447843d8
D/dalvikvm( 2216): GC freed 6657 objects / 314648 bytes in 35ms D/NetworkLocationProvider( 75): onCellLocationChanged [***]
D/LocationManager( 2216): removeUpdates: listener = com.levelup.beautifulwidgets .UpdateWeather$2@447843d8
D/dalvikvm( 1253): GC freed 279 objects / 12640 bytes in 94ms
D/NetworkLocationProvider( 75): onCellLocationChanged [***]
D/LocationManager( 2216): removeUpdates: listener = com.levelup.beautifulwidgets .UpdateWeather$2@447843d8
D/dalvikvm( 1287): GC freed 3 objects / 72 bytes in 60ms
D/dalvikvm( 2216): GC freed 6661 objects / 314192 bytes in 104ms
D/NetworkLocationProvider( 75): onCellLocationChanged [***]
D/LocationManager( 2216): removeUpdates: listener = com.levelup.beautifulwidgets .UpdateWeather$2@447843d8

As you can see the app (BeautifulWidgets) was constantly trying to remove the same updates and thus draining my battery for no reason. This was happening every 10 seconds (!!)

… moving along …

Code:
adb logcat {Tag}:V *:S > ~/Desktop/logcat.txt
adb logcat {Tag}:V *:S > %userprofile%\desktop\logcat.txt

The above line is if you know the Tag but want to see all Priorities. {Tag}:V outputs all priorities for the specific Tag you’ve entered because it calls for the V (Verbose) priority, which is the very lowest priority … and as you recall, it always gives you the priority you’ve asked for AND above.

The *:S tells the logcat to Silence (or ignore) all lines/messages that have not otherwise been specifically called for using these filter expressions. This CAN cause issues though, sometimes it will silence what you're looking for / everything.


A final specific example from my phone to be clear. I got a Database Tag with an Info Priority, if I wanted to see all instances of this happening, I could use the following code:

Code:
adb logcat Database:I *:S > ~/Desktop/logcat.txt
adb logcat Database:I *:S > %userprofile%\desktop\logcat.txt

Or, if I had an ActivityManager warning I could use

Code:
adb logcat ActivityManager:W *:S > ~/Desktop/logcat.txt
adb logcat ActivityManager:W *:S > %userprofile%\desktop\logcat.txt

by arawn
Jul 26, 2012
comnam90 and Robbie Hood like this.