Framework JNI instance --log


1. frameworks\base\core\java\android\util\Log.java

    /**
     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
     *
     *  The default level of any tag is set to INFO. This means that any level above and including
     *  INFO will be logged. Before you make any calls to a logging method you should check to see
     *  if your tag should be logged. You can change the default level by setting a system property:
     *      'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
     *  Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
     *  turn off all logging for your tag. You can also create a local.prop file that with the
     *  following in it:
     *      'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
     *  and place that in /data/local.prop.
     *
     * @param tag The tag to check.
     * @param level The level to check.
     * @return Whether or not that this is allowed to be logged.
     * @throws IllegalArgumentException is thrown if the tag.length() > 23.
     */
    public static <span style="color:#CC0000;">native</span> boolean isLoggable(String tag, int level);

    /** @hide */ public static <span style="color:#CC0000;">native</span> int println_native(int bufID,
            int priority, String tag, String msg);

2.  frameworks\base\core\jni\android_util_Log.cpp

#include "jni.h"<strong> //JNI head file</strong>
#include "JNIHelp.h"<strong> //head file to help jni work better</strong>
#include "utils/misc.h"
#include "android_runtime/AndroidRuntime.h"
#include "android_util_Log.h"
<strong>
/*
Here is the implementing code of function isLoggable() declared in Log.java 
*/</strong>
static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
{
    if (tag == NULL) {
        return false;
    }
<strong>    /*
    call jni function GetStringUTFChars
    */</strong>
    const char* chars = env->GetStringUTFChars(tag, NULL);
    if (!chars) {
        return false;
    }

    jboolean result = false;
    if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
        char buf2[200];
        snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %d characters\n",
                chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));

        jniThrowException(env, "java/lang/IllegalArgumentException", buf2);
    } else {
<strong>        /*
        call native library function
        */</strong>
        result = isLoggable(chars, level);
    }

    env->ReleaseStringUTFChars(tag, chars);
    return result;
}

<pre name="code" class="java"><strong>/*
Here is the implementing code of function println_native() declared in Log.java 
*/</strong>

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 = -1;
#ifdef HAVE_XLOG_FEATURE
    if (xlogf_java_tag_is_on(tag, (android_LogPriority)priority)) {
      res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
    }
#else
    res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
#endif
    if (tag != NULL)
        env->ReleaseStringUTFChars(tagObj, tag);
    env->ReleaseStringUTFChars(msgObj, msg);

    return res;
}

 
3. frameworks\base\core\jni\android_util_Log.cpp 

<strong>/*
 * JNI registration.
 */</strong>
static JNINativeMethod gMethods[] = {
    /* name, signature, funcPtr */
    { "isLoggable",      "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
    { "println_native",  "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
};

/*
jni.h
*/
typedef struct {
    const char* name; <strong>//the function name declared in java layer</strong>
    const char* signature; <strong>//signature</strong>
    void*       fnPtr; <strong>//function pointer to native function</strong>
} JNINativeMethod;



./log.log:6844:I/Gecko   (  167): *** WIFI GEO: Sending request: https://location.services.mozilla.com/v1/geolocate?key=no-mozilla-api-key
./log.log:6846:I/Gecko   (  167): *** WIFI GEO: sending {"cellTowers":[{"radio":"wcdma","mobileCountryCode":"460","mobileNetworkCode":"01","locationAreaCode":42411,"cellId":223651845}]}

./log.log:7343:I/Gecko   (  167): *** WIFI GEO: gls returned status: 200 --> {"location":{"lat":23.03545,"lng":114.34673},"accuracy":387}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值