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 | |
Andreas Gampe | 8413db8 | 2015-12-14 13:54:51 -0800 | [diff] [blame] | 21 | #include <android-base/macros.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <assert.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 23 | #include <log/log.h> // For LOGGER_ENTRY_MAX_PAYLOAD. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/String8.h> |
| 26 | |
| 27 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 28 | #include <nativehelper/JNIHelp.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include "utils/misc.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 30 | #include "core_jni_helpers.h" |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 31 | #include "android_util_Log.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 45 | static jboolean isLoggable(const char* tag, jint level) { |
Mark Salyzyn | b761d04 | 2015-11-16 16:24:52 +0000 | [diff] [blame] | 46 | return __android_log_is_loggable(level, tag, ANDROID_LOG_INFO); |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 47 | } |
| 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level) |
| 50 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | if (tag == NULL) { |
| 52 | return false; |
| 53 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | const char* chars = env->GetStringUTFChars(tag, NULL); |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 56 | if (!chars) { |
| 57 | return false; |
| 58 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
Mark Salyzyn | 8143fa5 | 2017-04-07 11:26:26 -0700 | [diff] [blame] | 60 | jboolean result = isLoggable(chars, level); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | env->ReleaseStringUTFChars(tag, chars); |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 63 | return result; |
| 64 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 66 | bool android_util_Log_isVerboseLogEnabled(const char* tag) { |
| 67 | return isLoggable(tag, levels.verbose); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * In class android.util.Log: |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 72 | * 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] | 73 | */ |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 74 | static jint android_util_Log_println_native(JNIEnv* env, jobject clazz, |
| 75 | jint bufID, jint priority, jstring tagObj, jstring msgObj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | { |
| 77 | const char* tag = NULL; |
| 78 | const char* msg = NULL; |
| 79 | |
| 80 | if (msgObj == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 81 | jniThrowNullPointerException(env, "println needs a message"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 85 | if (bufID < 0 || bufID >= LOG_ID_MAX) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 86 | jniThrowNullPointerException(env, "bad bufID"); |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | if (tagObj != NULL) |
| 91 | tag = env->GetStringUTFChars(tagObj, NULL); |
| 92 | msg = env->GetStringUTFChars(msgObj, NULL); |
| 93 | |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 94 | 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] | 95 | |
| 96 | if (tag != NULL) |
| 97 | env->ReleaseStringUTFChars(tagObj, tag); |
| 98 | env->ReleaseStringUTFChars(msgObj, msg); |
| 99 | |
| 100 | return res; |
| 101 | } |
| 102 | |
| 103 | /* |
Andreas Gampe | 8413db8 | 2015-12-14 13:54:51 -0800 | [diff] [blame] | 104 | * In class android.util.Log: |
| 105 | * private static native int logger_entry_max_payload_native() |
| 106 | */ |
| 107 | static jint android_util_Log_logger_entry_max_payload_native(JNIEnv* env ATTRIBUTE_UNUSED, |
| 108 | jobject clazz ATTRIBUTE_UNUSED) |
| 109 | { |
| 110 | return static_cast<jint>(LOGGER_ENTRY_MAX_PAYLOAD); |
| 111 | } |
| 112 | |
| 113 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | * JNI registration. |
| 115 | */ |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 116 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | /* name, signature, funcPtr */ |
| 118 | { "isLoggable", "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable }, |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 119 | { "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native }, |
Andreas Gampe | 8413db8 | 2015-12-14 13:54:51 -0800 | [diff] [blame] | 120 | { "logger_entry_max_payload_native", "()I", (void*) android_util_Log_logger_entry_max_payload_native }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | int register_android_util_Log(JNIEnv* env) |
| 124 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 125 | jclass clazz = FindClassOrDie(env, "android/util/Log"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 127 | levels.verbose = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "VERBOSE", "I")); |
| 128 | levels.debug = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "DEBUG", "I")); |
| 129 | levels.info = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "INFO", "I")); |
| 130 | levels.warn = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "WARN", "I")); |
| 131 | levels.error = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ERROR", "I")); |
| 132 | levels.assert = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ASSERT", "I")); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 133 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 134 | return RegisterMethodsOrDie(env, "android/util/Log", gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | }; // namespace android |