2015-04-07 30 views
-2

我知道我可以use adb to dump logcat to a file,但我在为我的设备(Moverio BT-200)配置驱动程序时遇到问题。因此,adb找不到设备,然后它无法检索logcat。如何转储Android logcat withtout ADB?

我的应用程序在启动时崩溃,所以我不能retrieve the logcat programmatically。我在其他一些设备(Android 5.1和4.4.2)上测试了我的应用程序,并且它工作正常,所以我认为这是旧版Android(4.0.3)的问题。但Android Studio并没有给我任何关于API版本的警告。

是否有其他方式来转储logcat输出?

+1

尝试解决你(让'adb'司机的工作)的实际问题,而不是建立新的 –

+0

我已经在努力解决这个问题,我已经打开了另一个爱普生支持的线程。我问这是因为我正在寻找解决方法。无论如何,为什么downvote? – ocramot

回答

0

感谢Randy的建议,我用一个类似的旧代码将异常记录到一个文件中(我尝试使用运行时的exec,但它只产生一个空文件)。

这里是我使用的代码:

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

您可以programmically转储你的logcat:

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

我想结合Thread.setDefaultUncaughtExceptionHandler其捕获所有捕获的异常为回调使用。在回调中,我转储日志,然后再次抛出异常,让应用程序崩溃。