Search code examples
androidadblogcat

How to dump Android logcat withtout ADB?


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?


Solution

  • 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();
            }
        });
    }