【问题标题】:Logcat is redirected to file more then onceLogcat 被重定向到文件不止一次
【发布时间】:2011-06-19 11:41:12
【问题描述】:

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

如何执行命令并确保父进程死亡时子进程也死亡?

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

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

谢谢!

【问题讨论】:

  • 为什么需要将其重定向到文件?请显示一些代码。
  • 我需要将文件发送到服务器。我添加了执行重定向的代码。你怎么看?
  • LogCat 旨在调试您的开发环境。如果应用程序在用户设备上崩溃并且在市场上,用户可以向您发送 Stack-Trace。无论如何,你能展示你使用这段代码的整个方法吗? (我猜是onCreate)。
  • 这是整个方法,我仅将其用于调试目的。问题是第二行 (Runtime.getRuntime().exec(cmd);) 被调用了两次,我想知道如何检查它是否已在此设备上调用
  • 看看这个,这可能会有所帮助:stackoverflow.com/a/43364736/2046324

标签: java android logcat runtime.exec android-logcat


【解决方案1】:

如果您仅将 LogCat 用于调试目的,您可能需要阅读 this

激活 LogCat 后,您可以在 Eclipse 中打开 LogCat-View,然后会显示所有 LogCat-Output,因此您无需将其写入文件。

【讨论】:

  • 我需要这些文件,因为我正在将它们发送到服务器。我想用真机测试,有没有正常的方法可以检查logcat是否已经重定向到文件?
  • 如果您想使用硬件设备,您可能需要阅读this
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    /** 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;
        }
    }
    

    【讨论】:

    • -1 这是可怕的代码和糟糕的答案:1) Environment.getExternalStorageDirectory() + LOG_FILE_NAME; 使用接受FileStringFile 构造函数。 2) 在 1.5+ 中使用ProcessBuilder,这反过来又使这一步更容易.. 3) 实施所有 When Runtime.exec() won't 的建议。
    猜你喜欢
    • 2012-05-03
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多