The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_util_Log.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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" |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 27 | #include "JNIHelp.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include "utils/misc.h" |
| 29 | #include "android_runtime/AndroidRuntime.h" |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 30 | #include "android_util_Log.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 46 | static int toLevel(const char* value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 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) { |
Jeff Brown | d56feb9 | 2012-04-02 10:53:17 -0700 | [diff] [blame] | 67 | buf[0] = '\0'; |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int logLevel = toLevel(buf); |
| 71 | return logLevel >= 0 && level >= logLevel; |
| 72 | } |
| 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level) |
| 75 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | if (tag == NULL) { |
| 77 | return false; |
| 78 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | const char* chars = env->GetStringUTFChars(tag, NULL); |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 81 | if (!chars) { |
| 82 | return false; |
| 83 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 85 | jboolean result = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | char buf2[200]; |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 88 | snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %zu characters\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE)); |
| 90 | |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 91 | jniThrowException(env, "java/lang/IllegalArgumentException", buf2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } else { |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 93 | result = isLoggable(chars, level); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | env->ReleaseStringUTFChars(tag, chars); |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 97 | return result; |
| 98 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 100 | bool android_util_Log_isVerboseLogEnabled(const char* tag) { |
| 101 | return isLoggable(tag, levels.verbose); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
| 105 | * In class android.util.Log: |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 106 | * public static native int println_native(int buffer, int priority, String tag, String msg) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | */ |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 108 | static jint android_util_Log_println_native(JNIEnv* env, jobject clazz, |
| 109 | jint bufID, jint priority, jstring tagObj, jstring msgObj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | { |
| 111 | const char* tag = NULL; |
| 112 | const char* msg = NULL; |
| 113 | |
| 114 | if (msgObj == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 115 | jniThrowNullPointerException(env, "println needs a message"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 119 | if (bufID < 0 || bufID >= LOG_ID_MAX) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 120 | jniThrowNullPointerException(env, "bad bufID"); |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 121 | return -1; |
| 122 | } |
| 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | if (tagObj != NULL) |
| 125 | tag = env->GetStringUTFChars(tagObj, NULL); |
| 126 | msg = env->GetStringUTFChars(msgObj, NULL); |
| 127 | |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 128 | int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 }, |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 143 | { "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | int register_android_util_Log(JNIEnv* env) |
| 147 | { |
| 148 | jclass clazz = env->FindClass("android/util/Log"); |
| 149 | |
| 150 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 151 | ALOGE("Can't find android/util/Log"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | return -1; |
| 153 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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")); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | return AndroidRuntime::registerNativeMethods(env, "android/util/Log", gMethods, NELEM(gMethods)); |
| 163 | } |
| 164 | |
| 165 | }; // namespace android |