Android 框架简介
D_MAIN, ASSERT, tag, getStackTraceString(tr));
RuntimeInit.wtf(tag, tr);
return bytes;
}
/**
* Handy function to get a loggable stack trace from a Throwable
* @param tr An exception to log
*/
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return ;
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
return sw.toString();
}
/**
* Low-level logging call.
* @param priority The priority/type of this log message
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @return The number of bytes written.
*/
public static int println(int priority, String tag, String msg) {
return println_native(LOG_ID_MAIN, priority, tag, msg);
}
/** @hide */ public static final int LOG_ID_MAIN = 0;
/** @hide */ public static final int LOG_ID_RADIO = 1;
/** @hide */ public static final int LOG_ID_EVENTS = 2;
/** @hide */ public static final int LOG_ID_SYSTEM = 3;
/** @hide */ public static native int println_native(int bufID,
int priority, String tag, String msg);
}
我们看到所有代码都是调用public static native int println_native(int bufID,
int priority, String tag, String msg);来实现输出的,这个函数的实现就是C++,调用的方式就是JNI
我们看一下对应的jni代码froyo/frameworks/base/core/jni/Android_util_Log.cpp,最终调用的输出函数是
/*
* In class Android.util.Log:
* public static native int println_native(int buffer, int priority, String tag, String msg)
*/
static jint Android_util_Log_println_native(JNIEnv* env, jobject clazz,
jint bufID, jint priority, jstring tagObj, jstring msgObj)
{
const char* tag = NULL;
const char* msg = NULL;
if (msgObj == NULL) {
jclass npeClazz;
npeClazz = env->FindClass(java/lang/NullPointerException);
assert(npeClazz != NULL);
env->ThrowNew(npeClazz, println needs a message);
return -1;
}
if (bufID 0 || bufID >= LOG_ID_MAX) {
jclass npeClazz;
npeClazz = env->FindClass(java/lang/NullPointerException);
assert(npeClazz != NULL);
env->ThrowNew(npeClazz, bad bufID);
return -1;
}
if (tagObj != NULL)
tag = env->GetStringUTFChars(tagObj, NULL);
msg = env->GetStringUTFChars(msgObj, NULL);
int res = __Android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
if (tag != NULL)
env->ReleaseStringUTFChars(tagObj, tag);
env->ReleaseStringUTFChars(msgObj, msg);
return res;
}
当然我们发现最终输出是
? 1int res = __Android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
用力grep了一下代码,结果如下
./system/core/include/cutils/log.h:int __Android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
./system/core/liblog/logd_write.c:int __Android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
./system/core/liblog/logd_write.c: return __Android_log_buf_write(bufID, prio, tag, buf);
这个就是和Android专用驱动进行通信的方式,这个分析下去就有点深了,后面分析。
- Android开发技巧:软硬件的巧妙整合(01-16)
- 基于Android的VoIP系统的设计与实现(03-28)
- 基于Android系统的影音播放器开发(03-25)
- Android的阿喀琉斯之踵(07-08)
- Android开发之“hello World”的实现 (07-23)
- 基于Android平台的即时通信系统客户端设计(09-24)