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" |
| 30 | |
| 31 | #define MIN(a,b) ((a<b)?a:b) |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | struct levels_t { |
| 36 | jint verbose; |
| 37 | jint debug; |
| 38 | jint info; |
| 39 | jint warn; |
| 40 | jint error; |
| 41 | jint assert; |
| 42 | }; |
| 43 | static levels_t levels; |
| 44 | |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 45 | static int toLevel(const char* value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | { |
| 47 | switch (value[0]) { |
| 48 | case 'V': return levels.verbose; |
| 49 | case 'D': return levels.debug; |
| 50 | case 'I': return levels.info; |
| 51 | case 'W': return levels.warn; |
| 52 | case 'E': return levels.error; |
| 53 | case 'A': return levels.assert; |
| 54 | case 'S': return -1; // SUPPRESS |
| 55 | } |
| 56 | return levels.info; |
| 57 | } |
| 58 | |
| 59 | static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level) |
| 60 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | int len; |
| 62 | char key[PROPERTY_KEY_MAX]; |
| 63 | char buf[PROPERTY_VALUE_MAX]; |
| 64 | |
| 65 | if (tag == NULL) { |
| 66 | return false; |
| 67 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | jboolean result = false; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | const char* chars = env->GetStringUTFChars(tag, NULL); |
| 72 | |
| 73 | if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | char buf2[200]; |
| 75 | snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %d characters\n", |
| 76 | chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE)); |
| 77 | |
| 78 | // release the chars! |
| 79 | env->ReleaseStringUTFChars(tag, chars); |
| 80 | |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 81 | jniThrowException(env, "java/lang/IllegalArgumentException", buf2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | return false; |
| 83 | } else { |
| 84 | strncpy(key, LOG_NAMESPACE, sizeof(LOG_NAMESPACE)-1); |
| 85 | strcpy(key + sizeof(LOG_NAMESPACE) - 1, chars); |
| 86 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | env->ReleaseStringUTFChars(tag, chars); |
| 89 | |
| 90 | len = property_get(key, buf, ""); |
| 91 | int logLevel = toLevel(buf); |
| 92 | return (logLevel >= 0 && level >= logLevel) ? true : false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * In class android.util.Log: |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 97 | * 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] | 98 | */ |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 99 | static jint android_util_Log_println_native(JNIEnv* env, jobject clazz, |
| 100 | jint bufID, jint priority, jstring tagObj, jstring msgObj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | { |
| 102 | const char* tag = NULL; |
| 103 | const char* msg = NULL; |
| 104 | |
| 105 | if (msgObj == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 106 | jniThrowNullPointerException(env, "println needs a message"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | return -1; |
| 108 | } |
| 109 | |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 110 | if (bufID < 0 || bufID >= LOG_ID_MAX) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 111 | jniThrowNullPointerException(env, "bad bufID"); |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | if (tagObj != NULL) |
| 116 | tag = env->GetStringUTFChars(tagObj, NULL); |
| 117 | msg = env->GetStringUTFChars(msgObj, NULL); |
| 118 | |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 119 | 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] | 120 | |
| 121 | if (tag != NULL) |
| 122 | env->ReleaseStringUTFChars(tagObj, tag); |
| 123 | env->ReleaseStringUTFChars(msgObj, msg); |
| 124 | |
| 125 | return res; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * JNI registration. |
| 130 | */ |
| 131 | static JNINativeMethod gMethods[] = { |
| 132 | /* name, signature, funcPtr */ |
| 133 | { "isLoggable", "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable }, |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 134 | { "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] | 135 | }; |
| 136 | |
| 137 | int register_android_util_Log(JNIEnv* env) |
| 138 | { |
| 139 | jclass clazz = env->FindClass("android/util/Log"); |
| 140 | |
| 141 | if (clazz == NULL) { |
| 142 | LOGE("Can't find android/util/Log"); |
| 143 | return -1; |
| 144 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | levels.verbose = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "VERBOSE", "I")); |
| 147 | levels.debug = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "DEBUG", "I")); |
| 148 | levels.info = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "INFO", "I")); |
| 149 | levels.warn = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "WARN", "I")); |
| 150 | levels.error = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "ERROR", "I")); |
| 151 | levels.assert = env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "ASSERT", "I")); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | return AndroidRuntime::registerNativeMethods(env, "android/util/Log", gMethods, NELEM(gMethods)); |
| 154 | } |
| 155 | |
| 156 | }; // namespace android |