Reading Logcat within the app returns null

末鹿安然 提交于 2019-11-28 06:30:03

问题


I read the other posts and can't figure out the "trick".

I looked at Log Collector but can't use a separate APK. I'm basically using the same approach and I consistently get nothing back on the processes inputstream.

I have READ_LOGS in the manifest.

From within my default activity, I'm able to get the log, but if I move the logic to another activity or utilize an asynctask, no output is returned.

this code is from my default activity... inline, i dump it to the log

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

    StringBuilder log=new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        log.append(line);
    }
    Log.d(LOGTAG, "Logcat: " +log.toString());
 } catch (IOException e) {}

if i wrap it in an asynctask or just inline it in another activity, it returns nothing

    ArrayList<String> commandLine = new ArrayList<String>();
    //terminate on completion and suppress everything except the filter
    commandLine.add("logcat -d -s");
       ...
    //replace asynctask with inline (could not get log in asynctask)
    showProgressDialog(getString(R.string.acquiring_log_progress_dialog_message));
    final StringBuilder log = new StringBuilder();
    BufferedReader bufferedReader = null;
    try{

        Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
        bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = bufferedReader.readLine()) != null){ 
            log.append(line);
            log.append(MangoApp.LINE_SEPARATOR); 
        }


        sendIntent.putExtra(Intent.EXTRA_TEXT, log.toString());
        startActivity(Intent.createChooser(sendIntent, getString(R.string.chooser_title)));
        dismissProgressDialog();
        dismissMainDialog();
        finish();           
    } 
    catch (IOException e){
        dismissProgressDialog();
        showErrorDialog(getString(R.string.failed_to_get_log_message));
        Log.e(LOGTAG, "Log collection failed: ", e);//$NON-NLS-1$
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ignore) {}
        }
    }

Can anyone spot the diff or explain the magic? I'm pretty sure the commandline is right in the second version so scratching my head. I'm using 2.1 SDK 7 on the emulator.

Thanks


回答1:


Can you try it without the ArrayList. Just pass the command String I have implemented it in the following way (without the ArrayList). It works for me.

        String baseCommand = "logcat -v time";
        baseCommand += " MyApp:I "; // Info for my app
        baseCommand += " *:S "; // Silence others

        ServicesController.logReaderProcess = Runtime.getRuntime().exec(baseCommand);



回答2:


Hope this will be helpful, you don't have to create file by your self just execute the below command, to get the error info.

Runtime.getRuntime().exec("logcat -v time -r 100 -f /sdcard/log.txt *:E");

Logcat parameters options:

-r <size in kilobytes> -> for specifying the size of file  
-f <filename> -> file to which you want to write the logs.


来源:https://stackoverflow.com/questions/5556028/reading-logcat-within-the-app-returns-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!