Android logcat Application Guide

Source: Internet
Author: User

Android is an open-source mobile phone operating system launched by Google based on the Linux platform. It has been launched to reach out to the favorite of the majority of programmers. Here, we will first have an in-depth understanding of the Android logcat application to facilitate our practical application.

Options and descriptions

  1. -S: sets the filter by default.
  2. -F file output to log file
  3. -C clear logs
  4. -D. Obtain logs.
  5. -G: Get the log size.
  6. For the-v format setting log, see the following format print format)

-V format and example

 
 
  1. brief W/tag ( 876): message   
  2. process W( 876) message (tag)   
  3. tag W/tag : message   
  4. thread W( 876:0x37c) message   
  5. raw message   
  6. time 09-08 05:40:26.729 W/tag ( 876): message   
  7. threadtime 09-08 05:40:26.729 876 892 W tag : message   
  8. long [ 09-08 05:40:26.729 876:0x37c W/tag ] message  

Code example:

AndroidManifest. xml: Add read permission

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

Clear logs

 
 
  1. try {   
  2. Runtime.getRuntime().exec("logcat -c");   
  3. } catch(Exception e) {   
  4. try {  
  5. Runtime.getRuntime().exec("logcat -c");  
  6. } catch(Exception e) {  

Get logs

 
 
  1. try {   
  2. ArrayList< String> commandLine = new ArrayList< String>();   
  3. commandLine.add( "logcat");   
  4. commandLine.add( "-d");   
  5. commandLine.add( "-v");   
  6. commandLine.add( "time");   
  7. commandLine.add( "-s");   
  8. commandLine.add( "tag:W");   
  9. Process process = Runtime.getRuntime().exec
    ( commandLine.toArray( new String[commandLine.size()]));   
  10. BufferedReader bufferedReader = new BufferedReader
    ( new InputStreamReader(process.getInputStream()), 1024);   
  11. String line = bufferedReader.readLine();   
  12. while ( line != null) {   
  13. log.append(line);   
  14. log.append("\n")   
  15. }   
  16. } catch ( IOException e) {   
  17. }   
  18. try {  
  19. ArrayList< String> commandLine = new ArrayList< String>();  
  20. commandLine.add( "logcat");  
  21. commandLine.add( "-d");  
  22. commandLine.add( "-v");  
  23. commandLine.add( "time");  
  24. commandLine.add( "-s");  
  25. commandLine.add( "tag:W");  
  26. Process process = Runtime.getRuntime().exec
    ( commandLine.toArray( new String[commandLine.size()]));  
  27. BufferedReader bufferedReader = new BufferedReader
    ( new InputStreamReader(process.getInputStream()), 1024);  
  28. String line = bufferedReader.readLine();  
  29. while ( line != null) {  
  30. log.append(line);  
  31. log.append("\n")  
  32. }  
  33. } catch ( IOException e) {  

Result:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.