0

I have searched and I have found the following codes will let my program read the output of the logcat in android.However,after I call this function peroidically, nothing happens.Nothing is output through system.out except the "logcat called".I really do not know what happened because many posts here tell this will work:<

public void  Collector_logcat(){


    String stringbuffer="";
    String command="logcat -d";
    String command_c="logcat -c";
     System.out.println("logcat called\n"); 
    try{
        m_logcatprocess=Runtime.getRuntime().exec(command);
        m_logcat_inputreader=new InputStreamReader(m_logcatprocess.getInputStream());
        m_logcat_reader=new BufferedReader(m_logcat_inputreader);
      while((stringbuffer=m_logcat_reader.readLine())!=null){
          System.out.println(stringbuffer+"\n");  
      }

      Runtime.getRuntime().exec(command_c);

    }

        catch(Exception ex){
            System.out.println(ex.getMessage());
            System.out.println("error in Collector_logcat\n");
        }

     return ;

    }   
Hao Shen
  • 2,605
  • 3
  • 37
  • 68
  • And if I try to directly read the /dev/log/main file, it shows that the permission is denied. I do not want my program to obtain the root previlige – Hao Shen Apr 20 '12 at 15:38
  • 1
    Do you get any errors in logcat when it stops working? – dymmeh Apr 20 '12 at 15:41
  • @dymmeh no error at all.The only output is "logcat called".... – Hao Shen Apr 20 '12 at 15:44
  • Make sure you close your InputStream and BufferedReader once you finish using them (m_logcat_inputreader and m_logcat_reader). I'm guessing not closing them has something to do with it – dymmeh Apr 20 '12 at 15:46
  • Does the device you are logging from have a lot of apps or services running? If it's a pretty bare device or the emulator, there literally may not be much of anything going on in Logcat (especially after system has settled and is in sleep), and the logcat -c cleared all the old stuff out so you don't see anything. Maybe try running an app or two after it stops working, and see if it shows something then – Drake Clarris Apr 20 '12 at 15:47
  • @dymmeh no..still doesn't work. – Hao Shen Apr 20 '12 at 15:59
  • @Drake Clarris Yes.The eclipse will print other log information by itself.But nothing is output from system.out: – Hao Shen Apr 20 '12 at 16:00

1 Answers1

5

try this:

AndroidManifest.xml

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

Get catlog:

try {  
    ArrayList<String> commandLine = new ArrayList<String>();  
commandLine.add("logcat");  
    commandLine.add( "-d");  
    commandLine.add( "-v");  
    commandLine.add( "time");  
    commandLine.add( "-s");  
    commandLine.add( "tag:W");  
    Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()]));  
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);  
    String line;  
    while ((line = bufferedReader.readLine()) != null) {  
        log.append(line);  
        log.append("\n")  
    }  
} catch (IOException e) {  
}

You will get output as:

09-08 09:44:42.267 W/tag ( 754): message1
09-08 09:44:42.709 W/tag ( 754): message2
09-08 09:44:43.187 W/tag ( 754): message3
09-08 09:44:45.295 E/tag ( 754): message8

Sebastiano
  • 12,289
  • 6
  • 47
  • 80
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    ...Oh great. The point is that I forgot the permission in xml file. I hate android, several times I have spent much time trying to debug the program and finally I found it was the permission. Thx... – Hao Shen Apr 20 '12 at 16:13