What is in android.util.Log#println_native()?

2019-04-17 12:00发布

Here is the android.util.Log source code.

At the very bottom (line 340), what is in the method:

public static native int println_native(int bufID,
        int priority, String tag, String msg);

i guess println_native() is more or less like its println(), just with an int bufID different.

But even i got the codes of println_native(), i still lack com.android.internal.os.RuntimeInit (line 19, the import) to simulate android.util.Log in old Android version.

2条回答
孤傲高冷的网名
2楼-- · 2019-04-17 12:25

Once you have downloaded the sdk, you can use find to dig files. Java code is linked to cpp code through jni.

In your case, you can use this command to find the related files:

find framework -type f -name "*.cpp" -exec grep "println_native" {} \; -ls

then you'll see that your java function is linked to this file : framework/base/core/jni/android_util_Log.cpp

查看更多
你好瞎i
3楼-- · 2019-04-17 12:30

Just did some digging in the android source code. This function maps to

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) {
    jniThrowNullPointerException(env, "println needs a message");
    return -1;
}

if (bufID < 0 || bufID >= LOG_ID_MAX) {
    jniThrowNullPointerException(env, "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;
}

Basically this then passes the arguments to the driver in the system which goes and writes to the Log buffer. Log buffer is pointed by /dev/log in the Linux kernel.

查看更多
登录 后发表回答