Android 直接打开log的一种方法

[Android]Android 直接打开log的一种方法

http://my.oschina.net/u/996206/blog/200789


针对Apk log内部控制方式是通过Log.isLoggable来控制的情况

Example: adb shell setprop log.tag.Finsky VERBOSE

一般code是这样写的: 
import android.util.Log;

public class FinskyLog 

public static final boolean DEBUG = Log.isLoggable(TAG, 2); 
private static String TAG = “Finsky”;

Log.isLoggable的实现: 
Native层处理文件:android_util_Log.cpp (\frameworks\base\core\jni)

Java 定义在Log.java \frameworks\base\core\java\android\util 
public static native boolean isLoggable(String tag, int level); 
非常有助于无source code apk的debug,比如google apk

struct levels_t {

?
1
2
3
4
5
6
jint verbose;
jint debug;
jint info;
jint warn;
jint error;
jint assert ;

}; 
static levels_t levels;

static int toLevel(const char* value) 
{

?
1
2
3
4
5
6
7
8
9
10
switch (value[ 0 ]) {
     case 'V' : return levels.verbose;
     case 'D' : return levels.debug;
     case 'I' : return levels.info;
     case 'W' : return levels.warn;
     case 'E' : return levels.error;
     case 'A' : return levels. assert ;
     case 'S' : return - 1 ; // SUPPRESS
}
return levels.info;

}

static jboolean isLoggable(const char* tag, jint level) {

?
1
2
3
4
5
6
7
8
9
10
11
String8 key;
key.append(LOG_NAMESPACE);
key.append(tag);
 
char buf[PROPERTY_VALUE_MAX];
if (property_get(key.string(), buf, "" ) <= 0 ) {
     buf[ 0 ] = '\0' ;
}
 
int logLevel = toLevel(buf);
return logLevel >= 0 && level >= logLevel;

}

参考文献:地址:http://blog.csdn.net/maybe_windleave/article/details/8742178

你可能感兴趣的:(android,LOG)