如何在没有 ADB 的情况下转储 Android logcat?
How to dump Android logcat withtout ADB?
我知道我可以 use adb to dump logcat to a file,但我在为我的设备 (Moverio BT-200) 配置驱动程序时遇到问题。因此,adb 无法找到该设备,进而无法检索 logcat.
我的应用程序在启动时崩溃,所以我不能 retrieve the logcat programmatically。我在其他一些设备(使用 Android 5.1 和 4.4.2)上测试了我的应用程序,它运行良好,所以我认为这是旧 Android 版本 (4.0.3) 的问题;然而 Android Studio 没有给我任何关于 API 版本的警告。
是否有其他方法可以转储 logcat 输出?
您可以通过编程方式转储您的 logcat:
Runtime.getRuntime().exec("logcat -v time -f /sdcard/mylog.log -d");
我喜欢将它与 Thread.setDefaultUncaughtExceptionHandler
结合使用,后者将所有未捕获的异常捕获到回调中。在回调中我转储日志然后再次抛出异常让应用程序崩溃。
感谢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();
}
});
}
我知道我可以 use adb to dump logcat to a file,但我在为我的设备 (Moverio BT-200) 配置驱动程序时遇到问题。因此,adb 无法找到该设备,进而无法检索 logcat.
我的应用程序在启动时崩溃,所以我不能 retrieve the logcat programmatically。我在其他一些设备(使用 Android 5.1 和 4.4.2)上测试了我的应用程序,它运行良好,所以我认为这是旧 Android 版本 (4.0.3) 的问题;然而 Android Studio 没有给我任何关于 API 版本的警告。
是否有其他方法可以转储 logcat 输出?
您可以通过编程方式转储您的 logcat:
Runtime.getRuntime().exec("logcat -v time -f /sdcard/mylog.log -d");
我喜欢将它与 Thread.setDefaultUncaughtExceptionHandler
结合使用,后者将所有未捕获的异常捕获到回调中。在回调中我转储日志然后再次抛出异常让应用程序崩溃。
感谢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();
}
});
}