Logcat 多次重定向到文件

发布于 2024-11-16 01:15:37 字数 389 浏览 4 评论 0原文

当我的应用程序启动时,我已将 logcat 重定向到我的代码中的文件。 但是当我的应用程序重新启动时,重定向代码再次运行,结果是日志中的每一行都被写入两次。

如何执行命令并确保子进程在父进程终止时也终止?

String.format("logcat -f %s -r %d", filename.getAbsolutePath(), LOG_FILE_SIZE_KB);    
Runtime.getRuntime().exec(cmd);

如何确保我的应用程序的 logcat 仅重定向一次? (如果其他应用程序调用 logcat 并将其重定向到它自己的文件,会发生什么情况,那么检查仍然有效吗?)

谢谢!

I've redirected the logcat to file in my code when my app starts.
But when my application is restarted, the redirecting code runs again and the result is that each line in the log is written twice.

How can I execute the command and make sure the child process dies when the parent dies?

String.format("logcat -f %s -r %d", filename.getAbsolutePath(), LOG_FILE_SIZE_KB);    
Runtime.getRuntime().exec(cmd);

How can i make sure that my app's logcat is redirected only once?
(Whats happends if other app calls the logcat and redirects it to it's own file, will the check still work then?)

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送君千里 2024-11-23 01:15:37

如果您仅将 LogCat 用于调试目的,则可能需要阅读此内容。

激活 LogCat 后,您可以在 Eclipse 中打开 LogCat-View,它将显示所有 LogCat-Output,因此您不需要首先将其写入文件。

If you only use LogCat for debugging purposes, you might want to read this.

After you Activated LogCat, you can open the LogCat-View in Eclipse which will then show you all the LogCat-Output, so you don't need to write it to a file in the first place.

花间憩 2024-11-23 01:15:37

两种选择:
a) 创建一个静态全局变量,其中包含 Logcat 是否已重定向的信息。
b) 在 sdcard 或应用程序目录(xml 或属性文件)中创建一个文件,其中包含天气 LogCat 已重定向的信息。

Two options:
a) Make a static global variable that will contain information weather Logcat was already redirected or not.
b) Make a file on the sdcard or in the app directory (xml or properties file) containing info weather LogCat was already redirected.

世态炎凉 2024-11-23 01:15:37
/** 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;
    }
}
/** 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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文