开发者

Reading Logcat within the app returns null

开发者 https://www.devze.com 2023-02-22 01:06 出处:网络
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 p

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();
        sh开发者_高级运维owErrorDialog(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


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消