android_util_Log.cpp revision 00bb93823d082c31d757bd7b75a8615afbd2c1a5
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 "utils/misc.h"
28#include "android_runtime/AndroidRuntime.h"
29
30#define MIN(a,b) ((a<b)?a:b)
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 android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
59{
60#ifndef HAVE_ANDROID_OS
61    return false;
62#else /* HAVE_ANDROID_OS */
63    int len;
64    char key[PROPERTY_KEY_MAX];
65    char buf[PROPERTY_VALUE_MAX];
66
67    if (tag == NULL) {
68        return false;
69    }
70
71    jboolean result = false;
72
73    const char* chars = env->GetStringUTFChars(tag, NULL);
74
75    if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
76        jclass clazz = env->FindClass("java/lang/IllegalArgumentException");
77        char buf2[200];
78        snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %d characters\n",
79                chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));
80
81        // release the chars!
82        env->ReleaseStringUTFChars(tag, chars);
83
84        env->ThrowNew(clazz, buf2);
85        return false;
86    } else {
87        strncpy(key, LOG_NAMESPACE, sizeof(LOG_NAMESPACE)-1);
88        strcpy(key + sizeof(LOG_NAMESPACE) - 1, chars);
89    }
90
91    env->ReleaseStringUTFChars(tag, chars);
92
93    len = property_get(key, buf, "");
94    int logLevel = toLevel(buf);
95    return (logLevel >= 0 && level >= logLevel) ? true : false;
96#endif /* HAVE_ANDROID_OS */
97}
98
99/*
100 * In class android.util.Log:
101 *  public static native int println_native(int buffer, int priority, String tag, String msg)
102 */
103static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
104        jint bufID, jint priority, jstring tagObj, jstring msgObj)
105{
106    const char* tag = NULL;
107    const char* msg = NULL;
108
109    if (msgObj == NULL) {
110        jclass npeClazz;
111
112        npeClazz = env->FindClass("java/lang/NullPointerException");
113        assert(npeClazz != NULL);
114
115        env->ThrowNew(npeClazz, "println needs a message");
116        return -1;
117    }
118
119    if (bufID < 0 || bufID >= LOG_ID_MAX) {
120        jclass npeClazz;
121
122        npeClazz = env->FindClass("java/lang/NullPointerException");
123        assert(npeClazz != NULL);
124
125        env->ThrowNew(npeClazz, "bad bufID");
126        return -1;
127    }
128
129    if (tagObj != NULL)
130        tag = env->GetStringUTFChars(tagObj, NULL);
131    msg = env->GetStringUTFChars(msgObj, NULL);
132
133    int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
134
135    if (tag != NULL)
136        env->ReleaseStringUTFChars(tagObj, tag);
137    env->ReleaseStringUTFChars(msgObj, msg);
138
139    return res;
140}
141
142/*
143 * JNI registration.
144 */
145static JNINativeMethod gMethods[] = {
146    /* name, signature, funcPtr */
147    { "isLoggable",      "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
148    { "println_native",  "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
149};
150
151int register_android_util_Log(JNIEnv* env)
152{
153    jclass clazz = env->FindClass("android/util/Log");
154
155    if (clazz == NULL) {
156        LOGE("Can't find android/util/Log");
157        return -1;
158    }
159
160    levels.verbose = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "VERBOSE", "I"));
161    levels.debug = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "DEBUG", "I"));
162    levels.info = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "INFO", "I"));
163    levels.warn = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "WARN", "I"));
164    levels.error = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "ERROR", "I"));
165    levels.assert = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "ASSERT", "I"));
166
167    return AndroidRuntime::registerNativeMethods(env, "android/util/Log", gMethods, NELEM(gMethods));
168}
169
170}; // namespace android
171
172