登录设备到文件

Dmy*_*tro 6 java logging android

如何将设备上的日志记录重定向到文件?

我的应用程序挂在设备上,但在模拟器上工作得很好 - 我希望看到我的设备上没有sdk及其工具的日志记录.

Dmy*_*tro 4

我发现Logcat 的优秀功能。它可以通过使用简单的命令参数“-f”将输出重定向到自己的文件。要使用它,您可以在应用程序 aLogcat 中编写 Logcat 包装器。显然这是我做的:)

为了在 android 上使用 logcat 我写了这段代码:

Process proc = null;
try {
  proc = Runtime.getRuntime().exec(new String[] { "logcat", <!parametes_here!> });
  mReader = new BufferedReader(new InputStreamReader(proc.getInputStream()), 1024);
  String line;
  while ((line = mReader.readLine()) != null) {
   if (line.length() == 0) {
    continue;
   }
  mHandler.sendMessage(mHandler.obtainMessage(Logcat.MSG_READ_LINE, line));
 }
} catch (IOException e) {
 Log.e(TAG, "Cannot start process", e);
} finally {
  if (mReader != null)
  try {
    mReader.close();
  } catch (IOException e) {
    Log.e(TAG, "Cannot close stream", e);
 }
}
Run Code Online (Sandbox Code Playgroud)