Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

android - Reading Logcat within the app returns null

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...