2011-01-12 104 views
8

對於默認情況在android平臺上登錄的控制檯輸出字符數量有限。大約等於3000多位。因此,如果消息長度超過3000個字符,則不會顯示在屏幕上。如何在android日誌類中增加控制檯輸出

我還沒有找到比這更好的解決辦法:

public class Log { 
    private static int mode = android.util.Log.INFO; 

    public static void e(String tag, String msg, Throwable tr) { 
     if ((mode & android.util.Log.ERROR) <= 0) return; 
     android.util.Log.e(tag, msg, tr); 
    } 

    public static void e(String tag, String msg) { 
     if ((mode & android.util.Log.ERROR) <= 0) return; 
     android.util.Log.e(tag, msg); 
    } 

    public static void w(String tag, String msg) { 
     if ((mode & android.util.Log.WARN) <= 0) return; 
     android.util.Log.w(tag, msg); 
    } 

    public static void i(String tag, String msg) { 
     if ((mode & android.util.Log.INFO) <= 0) return; 
     android.util.Log.i(tag, msg); 
    } 

    public static void d(String tag, String msg) { 
     if ((mode & android.util.Log.DEBUG) <= 0) return; 

      int length = msg.length(); 
      int kind = 3000; 
      if (length >= kind) { 
       int count = length/kind; 
       int u = length % kind; 
       int i = 0; 
       for (i = 0; i < count; ++i) { 
        int start = i * kind; 
        int end = start + kind; 
        android.util.Log.d(tag, msg.substring(start, end)); 
       } 
       if (u != 0) { 
        int start = length - u; 
        int end = start + u; 
        android.util.Log.d(tag, msg.substring(start, end)); 
       } 
      } else { 
       android.util.Log.d(tag, msg); 
      } 
    } 

} 

是否有更好的解決這個問題?

+3

你在做什麼? – Vinay 2011-01-12 12:12:27

回答

9

當做大輸出時(JSON等)1024字符限制的解決方法 是做一個簡單的for循環,以塊記錄它們。

它可能不像您的那樣精心製作,但它很容易在眼睛上看到,而且不會讓代碼混亂太多。

 String json = data.toString(); 
     int length = json.length(); 

     for(int i=0; i<length; i+=1024) 
     { 
      if(i+1024<length) 
       Log.d("JSON OUTPUT", json.substring(i, i+1024)); 
      else 
       Log.d("JSON OUTPUT", json.substring(i, length)); 
     }