android_util_Log.cpp revision ed6b9dff563c5e22f040ff37e12c0d771e0478ae
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 <assert.h>
22#include <cutils/properties.h>
23#include <utils/Log.h>
24#include <utils/String8.h>
25
26#include "jni.h"
27#include "JNIHelp.h"
28#include "utils/misc.h"
29#include "core_jni_helpers.h"
30#include "android_util_Log.h"
31
32namespace android {
33
34struct levels_t {
35    jint verbose;
36    jint debug;
37    jint info;
38    jint warn;
39    jint error;
40    jint assert;
41};
42static levels_t levels;
43
44static int toLevel(const char* value)
45{
46    switch (value[0]) {
47        case 'V': return levels.verbose;
48        case 'D': return levels.debug;
49        case 'I': return levels.info;
50        case 'W': return levels.warn;
51        case 'E': return levels.error;
52        case 'A': return levels.assert;
53        case 'S': return -1; // SUPPRESS
54    }
55    return levels.info;
56}
57
58static jboolean isLoggable(const char* tag, jint level) {
59    String8 key;
60    key.append(LOG_NAMESPACE);
61    key.append(tag);
62
63    char buf[PROPERTY_VALUE_MAX];
64    if (property_get(key.string(), buf, "") <= 0) {
65        buf[0] = '\0';
66    }
67
68    int logLevel = toLevel(buf);
69    return logLevel >= 0 && level >= logLevel;
70}
71
72static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
73{
74    if (tag == NULL) {
75        return false;
76    }
77
78    const char* chars = env->GetStringUTFChars(tag, NULL);
79    if (!chars) {
80        return false;
81    }
82
83    jboolean result = false;
84    if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
85        char buf2[200];
86        snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %zu characters\n",
87                chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));
88
89        jniThrowException(env, "java/lang/IllegalArgumentException", buf2);
90    } else {
91        result = isLoggable(chars, level);
92    }
93
94    env->ReleaseStringUTFChars(tag, chars);
95    return result;
96}
97
98bool android_util_Log_isVerboseLogEnabled(const char* tag) {
99    return isLoggable(tag, levels.verbose);
100}
101
102/*
103 * In class android.util.Log:
104 *  public static native int println_native(int buffer, int priority, String tag, String msg)
105 */
106static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
107        jint bufID, jint priority, jstring tagObj, jstring msgObj)
108{
109    const char* tag = NULL;
110    const char* msg = NULL;
111
112    if (msgObj == NULL) {
113        jniThrowNullPointerException(env, "println needs a message");
114        return -1;
115    }
116
117    if (bufID < 0 || bufID >= LOG_ID_MAX) {
118        jniThrowNullPointerException(env, "bad bufID");
119        return -1;
120    }
121
122    if (tagObj != NULL)
123        tag = env->GetStringUTFChars(tagObj, NULL);
124    msg = env->GetStringUTFChars(msgObj, NULL);
125
126    int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
127
128    if (tag != NULL)
129        env->ReleaseStringUTFChars(tagObj, tag);
130    env->ReleaseStringUTFChars(msgObj, msg);
131
132    return res;
133}
134
135/*
136 * JNI registration.
137 */
138static JNINativeMethod gMethods[] = {
139    /* name, signature, funcPtr */
140    { "isLoggable",      "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
141    { "println_native",  "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
142};
143
144int register_android_util_Log(JNIEnv* env)
145{
146    jclass clazz = FindClassOrDie(env, "android/util/Log");
147
148    levels.verbose = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "VERBOSE", "I"));
149    levels.debug = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "DEBUG", "I"));
150    levels.info = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "INFO", "I"));
151    levels.warn = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "WARN", "I"));
152    levels.error = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ERROR", "I"));
153    levels.assert = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ASSERT", "I"));
154
155    return RegisterMethodsOrDie(env, "android/util/Log", gMethods, NELEM(gMethods));
156}
157
158}; // namespace android
159