【问题标题】:How to read logcat continuously and write into the internal storage file?如何连续读取logcat并写入内部存储文件?
【发布时间】:2019-03-15 03:30:24
【问题描述】:

连续读取logcat并写入内部存储我尝试了下面的代码。

public class LogCatTask extends AsyncTask<Void, String, Void> {
    public AtomicBoolean run = new AtomicBoolean(true);

    @Override
    protected Void doInBackground(Void... params) {
        try {

            //create text file in SDCard
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
            dir.mkdirs();
            File file = new File(dir, "logcat.txt");

            Runtime.getRuntime().exec("logcat -c");
            Process process = Runtime.getRuntime().exec("logcat -f "+file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder log = new StringBuilder();
            String line = "";
            Log.e("log cat task...","while...run.get()."+run.get());
            while (run.get()) {
                line = bufferedReader.readLine();
                //Log.e("log cat task...","while...out.");
                if (line != null) {
                    Log.e("log cat task...","while....");
                    log.append(line);
                    //publishProgress(log.toString());

                    //to write logcat in text file
                    FileOutputStream fOut = new FileOutputStream(file);
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);

                    // Write the string to the file
                    osw.write(log.toString());
                    osw.flush();
                    osw.close();
                }
                line = null;
                Thread.sleep(10);
                //Log.e("log cat task...","while...out.");
            }
            //Log.e("log cat task...","ouet....while....");
        }
        catch(Exception ex){

        }
        return null;
    }
}

一旦上面的代码运行,它会读取 logcat 并写入存储,但最多只能读取日志的某些部分。它不读取 continuos。如何连续读取 logcat?

【问题讨论】:

  • 你能找到解决办法吗?

标签: android logging logcat


【解决方案1】:

这是一个工作代码 sn-p

    try {
        Process process = Runtime.getRuntime().exec("logcat");
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        StringBuilder log=new StringBuilder();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            // do what you want with the line you just read
            // you're in an infinite loop continuously reading the log,
            // you might want to add some mechanism to break out of it
        }
    }
    catch (IOException  e) {
        fail("failed to get log");
    }

您的代码过于复杂并且容易出现竞争条件。例如,logcat -c 是否保证在 logcat -f 命令之前完成。 -f 命令将输出重定向到文件,并且您的逻辑也在尝试写入同一个文件;这是非常有问题的。我注意到某些 logcat 选项(例如“-T”)不能以编程方式工作。也许“-f”选项也有问题。如果您只想读取当前时间之前的日志文件,请使用“logcat -d”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2018-08-04
    • 2016-03-24
    相关资源
    最近更新 更多