Home | History | Annotate | Download | only in jni
      1 /* //device/libs/android_runtime/android_util_Log.cpp
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #define LOG_NAMESPACE "log.tag."
     19 #define LOG_TAG "Log_println"
     20 
     21 #include <android-base/macros.h>
     22 #include <assert.h>
     23 #include <cutils/properties.h>
     24 #include <log/log.h>               // For LOGGER_ENTRY_MAX_PAYLOAD.
     25 #include <utils/Log.h>
     26 #include <utils/String8.h>
     27 
     28 #include "jni.h"
     29 #include <nativehelper/JNIHelp.h>
     30 #include "utils/misc.h"
     31 #include "core_jni_helpers.h"
     32 #include "android_util_Log.h"
     33 
     34 namespace android {
     35 
     36 struct levels_t {
     37     jint verbose;
     38     jint debug;
     39     jint info;
     40     jint warn;
     41     jint error;
     42     jint assert;
     43 };
     44 static levels_t levels;
     45 
     46 static jboolean isLoggable(const char* tag, jint level) {
     47     return __android_log_is_loggable(level, tag, ANDROID_LOG_INFO);
     48 }
     49 
     50 static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
     51 {
     52     if (tag == NULL) {
     53         return false;
     54     }
     55 
     56     const char* chars = env->GetStringUTFChars(tag, NULL);
     57     if (!chars) {
     58         return false;
     59     }
     60 
     61     jboolean result = isLoggable(chars, level);
     62 
     63     env->ReleaseStringUTFChars(tag, chars);
     64     return result;
     65 }
     66 
     67 bool android_util_Log_isVerboseLogEnabled(const char* tag) {
     68     return isLoggable(tag, levels.verbose);
     69 }
     70 
     71 /*
     72  * In class android.util.Log:
     73  *  public static native int println_native(int buffer, int priority, String tag, String msg)
     74  */
     75 static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
     76         jint bufID, jint priority, jstring tagObj, jstring msgObj)
     77 {
     78     const char* tag = NULL;
     79     const char* msg = NULL;
     80 
     81     if (msgObj == NULL) {
     82         jniThrowNullPointerException(env, "println needs a message");
     83         return -1;
     84     }
     85 
     86     if (bufID < 0 || bufID >= LOG_ID_MAX) {
     87         jniThrowNullPointerException(env, "bad bufID");
     88         return -1;
     89     }
     90 
     91     if (tagObj != NULL)
     92         tag = env->GetStringUTFChars(tagObj, NULL);
     93     msg = env->GetStringUTFChars(msgObj, NULL);
     94 
     95     int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
     96 
     97     if (tag != NULL)
     98         env->ReleaseStringUTFChars(tagObj, tag);
     99     env->ReleaseStringUTFChars(msgObj, msg);
    100 
    101     return res;
    102 }
    103 
    104 /*
    105  * In class android.util.Log:
    106  *  private static native int logger_entry_max_payload_native()
    107  */
    108 static jint android_util_Log_logger_entry_max_payload_native(JNIEnv* env ATTRIBUTE_UNUSED,
    109                                                              jobject clazz ATTRIBUTE_UNUSED)
    110 {
    111     return static_cast<jint>(LOGGER_ENTRY_MAX_PAYLOAD);
    112 }
    113 
    114 /*
    115  * JNI registration.
    116  */
    117 static const JNINativeMethod gMethods[] = {
    118     /* name, signature, funcPtr */
    119     { "isLoggable",      "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
    120     { "println_native",  "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
    121     { "logger_entry_max_payload_native",  "()I", (void*) android_util_Log_logger_entry_max_payload_native },
    122 };
    123 
    124 int register_android_util_Log(JNIEnv* env)
    125 {
    126     jclass clazz = FindClassOrDie(env, "android/util/Log");
    127 
    128     levels.verbose = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "VERBOSE", "I"));
    129     levels.debug = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "DEBUG", "I"));
    130     levels.info = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "INFO", "I"));
    131     levels.warn = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "WARN", "I"));
    132     levels.error = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ERROR", "I"));
    133     levels.assert = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ASSERT", "I"));
    134 
    135     return RegisterMethodsOrDie(env, "android/util/Log", gMethods, NELEM(gMethods));
    136 }
    137 
    138 }; // namespace android
    139