Android logcat應用指南

來源:互聯網
上載者:User

Android是由Google推出的一款基於Linux平台的開源手機作業系統。已經推出就伸手廣大編程人員的喜愛。在這裡我們就先從Android logcat的相關應用來對這一系統進行一個深入的瞭解,以此方便我們的實際應用。

選項與說明 

  1. -s 預設設定過濾器    
  2. - f 檔案 輸出到記錄檔    
  3. -c 清除日誌    
  4. -d 擷取日誌    
  5. -g 擷取日誌的大小    
  6. - v 格式 設定日誌見下面的格式列印格式)  

- v 格式與例範例

 
  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  

代碼例子:

AndroidManifest.xml添加讀取許可權

 
  1. < uses-permission android:name=
    "android.permission.READ_LOGS" />   
  2. < uses-permission android:name=
    "android.permission.READ_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) {  

擷取日誌

 
  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) {  

結果:

 
  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  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.