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