【问题标题】:Reading logs with filters programmatically以编程方式使用过滤器读取日志
【发布时间】:2014-04-02 12:24:12
【问题描述】:

我正在尝试使用log.i() 以编程方式读取用户输入的日志,如下所示,

try {
        Log.i("logs", "inside log.java");
        String filter = "logcat ";
        filter += "-d ";
        filter += "logs:I";
        // String[] command = new String[] { "logcat", "-s" , "logs:"+filter
        // };
        Process process = Runtime.getRuntime().exec(filter);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        log = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {

            log.append(line);
            log.append("\n");
        }
        clear = (Button) findViewById(R.id.clear);
        clear.setOnClickListener(onCleared);
        tv = (TextView) findViewById(R.id.logview);
        tv.setText(log.toString());

    } catch (IOException ex) {
        Log.e("Logging", ex.toString());
    }

    // convert log to string
    final String logString = new String(log.toString());

    // 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");

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

        // Write the string to the file
        osw.write(logString);
        osw.flush();
        osw.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

但它只是显示黑屏。但是当我尝试在adb shell 中运行命令时,它可以工作。我的adb 代码是,

 adb logcat -s logs:I

【问题讨论】:

    标签: android logging logcat


    【解决方案1】:

    我在尝试以编程方式将文件记录为文本时遇到了类似的问题。我认为这里发生的情况是日志读取过程阻塞了 UI(主)线程,导致黑屏。

    尝试将整个日志读取过程放入 ASyncTask 中。这使它对我有用!

    【讨论】:

      【解决方案2】:

      您是否将此添加到清单文件中?

      <uses-permission android:name="android.permission.READ_LOGS" />
      

      【讨论】:

        猜你喜欢
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-13
        • 2016-09-06
        • 1970-01-01
        相关资源
        最近更新 更多