linux重定向logcat,Logcat被重定向到多个文件

/** Redirects the log output to the SDCard.

*

* make sure your app has the WRITE_EXTERNAL_STORAGE and READ_LOGS permissions -

* or it won't allow it to read logs and write them to the sdcard.

* If the application doesn't have the permissions, there will be no exception

* and the program will continue regularly.

*/

public static void redirectOutputToFile()

{

s_enableLogs = true;

if (s_logcatProcess != null)

{

Logger log = new Logger("Logger");

log.info("redirectOutputToFile() called more then once, perhaps from service onCreate and onStart.");

return;

}

try

{

String path = Environment.getExternalStorageDirectory() + LOG_FILE_NAME;

File filename = new File(path);

filename.createNewFile();

//http://www.linuxtopia.org/online_books/android/devguide/guide/developing/tools/android_adb_logcatoptions.html

String cmd = String.format("logcat -v time -f %s -r %d -n %d", filename.getAbsolutePath(), LOG_FILE_SIZE_KB, LOG_FILE_ROTATIONS);

s_logcatProcess = Runtime.getRuntime().exec(cmd);

}

catch (IOException e)

{

Logger log = new Logger("Logger");

log.exception(e);

}

}

/** Kills the logcat process that was created in the redirectOutputToFile() method. */

public static void killLogcatProcess()

{

// first update the log mode state

s_enableLogs = false;

if (s_logcatProcess != null)

{

s_logcatProcess.destroy();

s_logcatProcess = null;

}

}

2011-07-20 11:49:19

Adibe7

+0

-1这是可怕的代码和一个可怕的答案:1)'Environment.getExternalStorageDirectory()+ LOG_FILE_NAME;'使用'File'构造函数接受'File'和'String'。 2)在1.5+中使用一个'ProcessBuilder',这反过来使它更容易一步。3)实现*所有的*当[Runtime.exec()不会](http://www.javaworld的.com /和javaworld/JW-12-2000/JW-1229-traps.html)。 –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值