How to read logcat continuously and write into the internal storage file?



  • Reading logcat continuously and write into the internal storage for this i tried the below code.

    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;
        }
    }
    

    once the above code runs,it read the logcat and write into storage but upto some part of the log only it reads.it is not reading the continuos.How to read the logcat continuous?



  • There is a number of lines that demonstrate you have not complete understanding of what you're doing:

    Here Process process = Runtime.getRuntime().exec("logcat -f "+file); you make logcat output logs to the file. However here BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); you're trying to obtain the InputStream of a process that wouldn't have logs since they are forwarded to a file.

    Here while (run.get()) you get boolean value however you do not demonstrate where you change it.

    Here:

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

    You open and close output stream for each line read. This is not the right thing to do from many aspects.

    This line line = null; is not really necessary.

    So, I believe once you consider all my points and fix them, you'll get your feature working. Consider also if you do really need this to do in code since logcat -f does exactly the same, i.e. pushing logs to a chosen file.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2