-2

I know that I can use adb to dump logcat to a file, but I am having issues configuring the drivers for my device (Moverio BT-200). Therefore, adb can't find the device, and then it can't retrieve the logcat.

My application is crashing at start, so I cannot retrieve the logcat programmatically. I tested my app on some other devices (with Android 5.1 and 4.4.2), and it's working fine, so I think it's a problem with the old Android version (4.0.3); yet Android Studio is not giving me any warning about the API version.

Is there some other way to dump the logcat output?

Community
  • 1
  • 1
ocramot
  • 1,361
  • 28
  • 55
  • 2
    try to solve the real issues you have (getting `adb` drivers to work) instead of creating new ones – Alex P. Apr 07 '15 at 17:28
  • I am already trying to fix that, and I have already opened another thread with Epson support for that. I asked this because I am looking for workarounds. Anyway, why the downvote? – ocramot Apr 08 '15 at 08:01

2 Answers2

0

You can dump your logcat programmically:

Runtime.getRuntime().exec("logcat -v time -f /sdcard/mylog.log -d");

I like to use this in conjunction with Thread.setDefaultUncaughtExceptionHandler which catches all uncaught exceptions into a callback. In the callback I dump the log and then throw the exception again to let the app crash.

Randy
  • 4,351
  • 2
  • 25
  • 46
0

Thanks to Randy's suggestion, I used a similar old code of mine to log the exception to a file (I tried to use the runtime exec, but it only produced an empty file).

Here is the code I used:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
        File logFile = new File("/any/directory/logFile.log");
        PrintWriter out = null;

        try{
            if(!logFile.exists()){
                try {
                    if(!logFile.createNewFile())
                        Log.e(TAG, "Some error creating log file " + logFile.getAbsolutePath());
                } catch (IOException ex) {
                    Log.e(TAG, "IoException creating logFile: " + ex.getMessage());
                    ex.printStackTrace();
                }               
            }

            out = new PrintWriter(new BufferedWriter(new FileWriter(logFile, true)));
            out.println(
                new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss.SSS", Locale.getDefault()).format(new Date()) + "\t" +
                TAG + "\t" +
                "Thread " + t + " throws exception: " + e
            );
            if(e != null)
                e.printStackTrace(out);
            out.flush();
        } catch (IOException ex) {
            Log.e(TAG, "IoException writing logFile: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            if(out != null)
                out.close();
        }
    });
}
Community
  • 1
  • 1
ocramot
  • 1,361
  • 28
  • 55