blob: 536a582aff45e4e5f5293d9f548300ea855af366 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_util_Log.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughes8451b252011-04-07 19:17:57 -07005** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughes8451b252011-04-07 19:17:57 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughes8451b252011-04-07 19:17:57 -070011** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** 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"
Elliott Hughes8451b252011-04-07 19:17:57 -070027#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include "utils/misc.h"
29#include "android_runtime/AndroidRuntime.h"
Jeff Brown4b575532012-01-20 12:34:34 -080030#include "android_util_Log.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32#define MIN(a,b) ((a<b)?a:b)
33
34namespace android {
35
36struct levels_t {
37 jint verbose;
38 jint debug;
39 jint info;
40 jint warn;
41 jint error;
42 jint assert;
43};
44static levels_t levels;
45
Elliott Hughes8451b252011-04-07 19:17:57 -070046static int toLevel(const char* value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047{
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
Jeff Brown4b575532012-01-20 12:34:34 -080060static 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) {
Jeff Brownd56feb92012-04-02 10:53:17 -070067 buf[0] = '\0';
Jeff Brown4b575532012-01-20 12:34:34 -080068 }
69
70 int logLevel = toLevel(buf);
71 return logLevel >= 0 && level >= logLevel;
72}
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
75{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 if (tag == NULL) {
77 return false;
78 }
Elliott Hughes8451b252011-04-07 19:17:57 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 const char* chars = env->GetStringUTFChars(tag, NULL);
Jeff Brown4b575532012-01-20 12:34:34 -080081 if (!chars) {
82 return false;
83 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Jeff Brown4b575532012-01-20 12:34:34 -080085 jboolean result = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 char buf2[200];
88 snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %d characters\n",
89 chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));
90
Elliott Hughes8451b252011-04-07 19:17:57 -070091 jniThrowException(env, "java/lang/IllegalArgumentException", buf2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 } else {
Jeff Brown4b575532012-01-20 12:34:34 -080093 result = isLoggable(chars, level);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
Elliott Hughes8451b252011-04-07 19:17:57 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 env->ReleaseStringUTFChars(tag, chars);
Jeff Brown4b575532012-01-20 12:34:34 -080097 return result;
98}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Jeff Brown4b575532012-01-20 12:34:34 -0800100bool android_util_Log_isVerboseLogEnabled(const char* tag) {
101 return isLoggable(tag, levels.verbose);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102}
103
104/*
105 * In class android.util.Log:
Joe Onorato00bb9382010-02-26 18:07:01 -0800106 * public static native int println_native(int buffer, int priority, String tag, String msg)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
Joe Onorato00bb9382010-02-26 18:07:01 -0800108static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
109 jint bufID, jint priority, jstring tagObj, jstring msgObj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110{
111 const char* tag = NULL;
112 const char* msg = NULL;
113
114 if (msgObj == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700115 jniThrowNullPointerException(env, "println needs a message");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 return -1;
117 }
118
Joe Onorato00bb9382010-02-26 18:07:01 -0800119 if (bufID < 0 || bufID >= LOG_ID_MAX) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700120 jniThrowNullPointerException(env, "bad bufID");
Joe Onorato00bb9382010-02-26 18:07:01 -0800121 return -1;
122 }
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 if (tagObj != NULL)
125 tag = env->GetStringUTFChars(tagObj, NULL);
126 msg = env->GetStringUTFChars(msgObj, NULL);
127
Joe Onorato00bb9382010-02-26 18:07:01 -0800128 int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
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 */
140static JNINativeMethod gMethods[] = {
141 /* name, signature, funcPtr */
142 { "isLoggable", "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
Joe Onorato00bb9382010-02-26 18:07:01 -0800143 { "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144};
145
146int register_android_util_Log(JNIEnv* env)
147{
148 jclass clazz = env->FindClass("android/util/Log");
149
150 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000151 ALOGE("Can't find android/util/Log");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 return -1;
153 }
Elliott Hughes8451b252011-04-07 19:17:57 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 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"));
Elliott Hughes8451b252011-04-07 19:17:57 -0700161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 return AndroidRuntime::registerNativeMethods(env, "android/util/Log", gMethods, NELEM(gMethods));
163}
164
165}; // namespace android