简体   繁体   中英

Print Stack Trace to Logcat

How can you print the stack trace to the logcat?

When the app is running through Eclipse and the app crashes I get a series of messages in the logcat window like "FATAL EXCEPTION: main" and stack trace is printed. However, when I use the adb logcat command from the command line I do not see any of these messages? All I see is a message saying example.app has died.

How can I get the same stack trace that shows up in the Eclipse logcat window with the adb logcat command?

Edit to clarify:

The stack trace is printed to the log when I reproduce the crash while the device is connected to the computer, but not when I reproduce the crash when the device is not connected to the computer.

After reproducing the crash while the device is not connected to the computer, I plug in the usb cable and use: 'adb logcat -d' and instead of a nice stack trace the only thing I see related to the crash is:

I/WindowManager(  656): WIN DEATH: Window{42aba898 XXXXXXXXXX.XXXXXXX.XXXXX.XXXXXX/XXXXXXXXXX.XXXXXXX.XXXXX.XXXXXX.XXXXXXXXXXXX paused=false}
I/ActivityManager(  656): Process example.app (pid 28681) has died.
I/WindowManager(  656): WIN DEATH: Window{42aa9548 XXXXXXXXXX.XXXXXXX.XXXXX.XXXXXX/XXXXXXXXXX.XXXXXXX.XXXXX.XXXXXX.XXXXXXXXXXXXXX paused=true}

Is there a way to have it still print the stack trace to logcat when the device is not connected to the computer?

Maybe the method you are looking for to show the whole exception in Logcat being able to track it down and Tag it is this one:

android.util.Log.e("Your TAG," , "Your Message", exceptionReference);

If you want to know about any uncaught exception whether you know where it happened or not, this is what you have to do at some point when your application just starts (Application Context class would be a good place...):

UncaughtExceptionHandler uncaughtExceptionHandler = new UncaughtExceptionHandler(){

                    @Override
                    public void uncaughtException(Thread thread,
                            Throwable ex) {
                        //Here print the log...
                    }

                };
                Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);

This will let you know if an exception have been unhandled by the end of the stack...

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM