在开发Android应用程序时,少不了使用Log来监控和调试程序的执行。在上一篇文章Android日志系统驱动程序Logger源代码分析中,我们分析了驱动程序Logger的源代码,在前面的文章浅谈Android系统开发中Log的使用一文,我们也简单介绍在应用程序中使Log的方法,在这篇文章中,我们将详细介绍android应用程序框架层和系统运行库存层日志系统的源代码,使得我们可以更好地理解Android的日志系统的实现。
《Android系统源代码情景分析》一书正在进击的程序员网(http://0xcc0xcd.com)中连载,点击进入!
我们在Android应用程序,一般是调用应用程序框架层的Java接口(android.util.Log)来使用日志系统,这个Java接口通过JNI方法和系统运行库最终调用内核驱动程序Logger把Log写到内核空间中。按照这个调用过程,我们一步步介绍Android应用程序框架层日志系统的源代码。学习完这个过程之后,我们可以很好地理解Android系统的架构,即应用程序层(Application)的接口是如何一步一步地调用到内核空间的。
一. 应用程序框架层日志系统Java接口的实现。
在浅谈Android系统开发中Log的使用一文中,我们曾经介绍过Android应用程序框架层日志系统的源代码接口。这里,为了描述方便和文章的完整性,我们重新贴一下这部份的代码,在frameworks/base/core/java/android/util/Log.java文件中,实现日志系统的Java接口:
- ................................................
- public final class Log {
- ................................................
- /**
- * Priority constant for the println method; use Log.v.
- */
- public static final int VERBOSE = 2;
- /**
- * Priority constant for the println method; use Log.d.
- */
- public static final int DEBUG = 3;
- /**
- * Priority constant for the println method; use Log.i.
- */
- public static final int INFO = 4;
- /**
- * Priority constant for the println method; use Log.w.
- */
- public static final int WARN = 5;
- /**
- * Priority constant for the println method; use Log.e.
- */
- public static final int ERROR = 6;
- /**
- * Priority constant for the println method.
- */
- public static final int ASSERT = 7;
- .....................................................
- public static int v(String tag, String msg) {
- return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
- }
- public static int v(String tag, String msg, Throwable tr) {
- return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));
- }
- public static int d(String tag, String msg) {
- return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
- }
- public static int d(String tag, String msg, Throwable tr) {
- return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
- }
- public static int i(String tag, String msg) {
- return println_native(LOG_ID_MAIN, INFO, tag, msg);
- }
- public static int i(String tag, String msg, Throwable tr) {
- return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));
- }
- public static int w(String tag, String msg) {
- return println_native(LOG_ID_MAIN, WARN, tag, msg);
- }
- public static int w(String tag, String msg, Throwable tr) {
- return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));
- }
- public static int w(String tag, Throwable tr) {
- return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
- }
- public static int e(String tag, String msg) {
- return println_native(LOG_ID_MAIN, ERROR, tag, msg);
- }
- public static int e(String tag, String msg, Throwable tr) {
- return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));
- }
- ..................................................................
- /** @hide */ public static native int LOG_ID_MAIN = 0;
- /** @hide */ public static native int LOG_ID_RADIO = 1;
- /** @hide */ public static native int LOG_ID_EVENTS = 2;
- /** @hide */ public static native int LOG_ID_SYSTEM = 3;
- /** @hide */ public static native int println_native(int bufID,
- int priority, String tag, String msg);
- }
在整个Log接口中,最关键的地方声明了println_native本地方法,所有的Log接口都是通过调用这个本地方法来实现Log的定入。下面我们就继续分析这个本地方法println_native。
二. 应用程序框架层日志系统JNI方法的实现。
在frameworks/base/core/jni/android_util_Log.cpp文件中,实现JNI方法println_native:
- /* //device/libs/android_runtime/android_util_Log.cpp
- **
- ** Copyright 2006, The Android Open Source Project
- **
- ** Licensed under the Apache License, Version 2.0 (the "License");
- ** you may not use this file except in compliance with the License.
- ** You may obtain a copy of the License at
- **
- ** http://www.apache.org/licenses/LICENSE-2.0
- **
- ** Unless required by applicable law or agreed to in writing, software
- ** distributed under the License is distributed on an "AS IS" BASIS,
- ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ** See the License for the specific language governing permissions and
- ** limitations under the License.
- */
- #define LOG_NAMESPACE "log.tag."
- #define LOG_TAG "Log_println"
- #include <assert.h>
- #include <cutils/properties.h>
- #include <utils/Log.h>
- #include <utils/String8.h>
- #include "jni.h"
- #include "utils/misc.h"
- #include "android_runtime/AndroidRuntime.h"
- #define MIN(a,b) ((a<b)?a:b)
- namespace android {
- struct levels_t {
- jint verbose;
- jint debug;
- jint info;
- jint warn;
- jint error;
- jint assert;
- };
- static levels_t levels;
- static int toLevel(const char* value)
- {
- switch (value[0]) {
- case 'V': return levels.verbose;
- case 'D': return levels.debug;
- case 'I': return levels.info;
- case 'W': return levels.warn;
- case 'E': return levels.error;
- case 'A': return levels.assert;
- case 'S': return -1; // SUPPRESS
- }
- return levels.info;
- }
- static jboolean android_util_Log_isLoggable(JNIEnv* env, jobject clazz, jstring tag, jint level)
- {
- #ifndef HAVE_ANDROID_OS
- return false;
- #else /* HAVE_ANDROID_OS */
- int len;
- char key[PROPERTY_KEY_MAX];
- char buf[PROPERTY_VALUE_MAX];
- if (tag == NULL) {
- return false;
- }
- jboolean result = false;
- const char* chars = env->GetStringUTFChars(tag, NULL);
- if ((strlen(chars)+sizeof(LOG_NAMESPACE)) > PROPERTY_KEY_MAX) {
- jclass clazz = env->FindClass("java/lang/IllegalArgumentException");
- char buf2[200];
- snprintf(buf2, sizeof(buf2), "Log tag \"%s\" exceeds limit of %d characters\n",
- chars, PROPERTY_KEY_MAX - sizeof(LOG_NAMESPACE));
- // release the chars!
- env->ReleaseStringUTFChars(tag, chars);
- env->ThrowNew(clazz, buf2);
- return false;
- } else {
- strncpy(key, LOG_NAMESPACE, sizeof(LOG_NAMESPACE)-1);
- strcpy(key + sizeof(LOG_NAMESPACE) - 1, chars);
- }
- env->ReleaseStringUTFChars(tag, chars);
- len = property_get(key, buf, "");
- int logLevel = toLevel(buf);
- return (logLevel >= 0 && level >= logLevel) ? true : false;
- #endif /* HAVE_ANDROID_OS */
- }
- /*
- * In class android.util.Log:
- * public static native int println_native(int buffer, int priority, String tag, String msg)
- */
- static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
- jint bufID, jint priority, jstring tagObj, jstring msgObj) <li class="alt" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(108, 226, 108); border-image: initial; list-style-type: decimal-leading-zero; list-style-image