blob: 7719e31f4240e679e2bdb737c94dddd4425b5dbb [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
Andreas Gampe8413db82015-12-14 13:54:51 -080021#include <android-base/macros.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <assert.h>
23#include <cutils/properties.h>
Andreas Gampe8413db82015-12-14 13:54:51 -080024#include <log/logger.h> // For LOGGER_ENTRY_MAX_PAYLOAD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <utils/Log.h>
26#include <utils/String8.h>
27
28#include "jni.h"
Elliott Hughes8451b252011-04-07 19:17:57 -070029#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include "utils/misc.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080031#include "core_jni_helpers.h"
Jeff Brown4b575532012-01-20 12:34:34 -080032#include "android_util_Log.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034namespace 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
Jeff Brown4b575532012-01-20 12:34:34 -080046static jboolean isLoggable(const char* tag, jint level) {
Mark Salyzynb761d042015-11-16 16:24:52 +000047 return __android_log_is_loggable(level, tag, ANDROID_LOG_INFO);
Jeff Brown4b575532012-01-20 12:34:34 -080048}
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
51{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 if (tag == NULL) {
53 return false;
54 }
Elliott Hughes8451b252011-04-07 19:17:57 -070055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 const char* chars = env->GetStringUTFChars(tag, NULL);
Jeff Brown4b575532012-01-20 12:34:34 -080057 if (!chars) {
58 return false;
59 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Jeff Brown4b575532012-01-20 12:34:34 -080061 jboolean result = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 char buf2[200];
Ashok Bhatf5df7002014-03-25 20:51:35 +000064 snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %zu characters\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));
66
Elliott Hughes8451b252011-04-07 19:17:57 -070067 jniThrowException(env, "java/lang/IllegalArgumentException", buf2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 } else {
Jeff Brown4b575532012-01-20 12:34:34 -080069 result = isLoggable(chars, level);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 }
Elliott Hughes8451b252011-04-07 19:17:57 -070071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 env->ReleaseStringUTFChars(tag, chars);
Jeff Brown4b575532012-01-20 12:34:34 -080073 return result;
74}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Jeff Brown4b575532012-01-20 12:34:34 -080076bool android_util_Log_isVerboseLogEnabled(const char* tag) {
77 return isLoggable(tag, levels.verbose);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078}
79
80/*
81 * In class android.util.Log:
Joe Onorato00bb9382010-02-26 18:07:01 -080082 * public static native int println_native(int buffer, int priority, String tag, String msg)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 */
Joe Onorato00bb9382010-02-26 18:07:01 -080084static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
85 jint bufID, jint priority, jstring tagObj, jstring msgObj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086{
87 const char* tag = NULL;
88 const char* msg = NULL;
89
90 if (msgObj == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -070091 jniThrowNullPointerException(env, "println needs a message");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 return -1;
93 }
94
Joe Onorato00bb9382010-02-26 18:07:01 -080095 if (bufID < 0 || bufID >= LOG_ID_MAX) {
Elliott Hughes8451b252011-04-07 19:17:57 -070096 jniThrowNullPointerException(env, "bad bufID");
Joe Onorato00bb9382010-02-26 18:07:01 -080097 return -1;
98 }
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 if (tagObj != NULL)
101 tag = env->GetStringUTFChars(tagObj, NULL);
102 msg = env->GetStringUTFChars(msgObj, NULL);
103
Joe Onorato00bb9382010-02-26 18:07:01 -0800104 int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106 if (tag != NULL)
107 env->ReleaseStringUTFChars(tagObj, tag);
108 env->ReleaseStringUTFChars(msgObj, msg);
109
110 return res;
111}
112
113/*
Andreas Gampe8413db82015-12-14 13:54:51 -0800114 * In class android.util.Log:
115 * private static native int logger_entry_max_payload_native()
116 */
117static jint android_util_Log_logger_entry_max_payload_native(JNIEnv* env ATTRIBUTE_UNUSED,
118 jobject clazz ATTRIBUTE_UNUSED)
119{
120 return static_cast<jint>(LOGGER_ENTRY_MAX_PAYLOAD);
121}
122
123/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * JNI registration.
125 */
Daniel Micay76f6a862015-09-19 17:31:01 -0400126static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /* name, signature, funcPtr */
128 { "isLoggable", "(Ljava/lang/String;I)Z", (void*) android_util_Log_isLoggable },
Joe Onorato00bb9382010-02-26 18:07:01 -0800129 { "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
Andreas Gampe8413db82015-12-14 13:54:51 -0800130 { "logger_entry_max_payload_native", "()I", (void*) android_util_Log_logger_entry_max_payload_native },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131};
132
133int register_android_util_Log(JNIEnv* env)
134{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800135 jclass clazz = FindClassOrDie(env, "android/util/Log");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800137 levels.verbose = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "VERBOSE", "I"));
138 levels.debug = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "DEBUG", "I"));
139 levels.info = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "INFO", "I"));
140 levels.warn = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "WARN", "I"));
141 levels.error = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ERROR", "I"));
142 levels.assert = env->GetStaticIntField(clazz, GetStaticFieldIDOrDie(env, clazz, "ASSERT", "I"));
Elliott Hughes8451b252011-04-07 19:17:57 -0700143
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800144 return RegisterMethodsOrDie(env, "android/util/Log", gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145}
146
147}; // namespace android