没有合适的资源?快使用搜索试试~ 我知道了~
首页Android Log系统介绍 (基于Android N)
Android Log系统介绍 (基于Android N). 从调用 Log.d("xxx", "test") 到你从logcat中看到它的输出, 这中间都发生了什么? native代码中的"ALOG/ALOGD/ALOGE"等的"宏函数"log都打到哪里去了? 什么是 kernel log? 请看本文分析.
资源详情
资源评论
资源推荐
先给出一张Android Log系统的总图
1 Android特有Log流程
引言
> Android
的
log
,
从
操作系
统
分
层
上
来
讲
,可以分
为
“
Kernel Log
”
和
“
User Log
”(
>
所
谓
“
Kernel Log
”
就是操作系
统
内
核打印的
log
。
内
核⾥
调
用
printk
等接⼝
请
求
输
出
kernel log
最后
会
被打印到
/
dev/kmsg
文件上。可以通
过
dmesg
查
看到
>
所
谓
“
User Log
”
分
为
2
部分。
⼀
类
是
Linux
的
标
准
输
出
设备
中打印的
log
(
stderr/stdout
)
.
另
⼀
类
是
android
特有的
log
流程。如通
过
android.util.Log
类
打印的
log
,
eventslog, ALOG() nati
他
们
都可以通
过
logcat
看到
.
本文基于
Android N
源
码
,
对
Android
的
log
机制做介
绍
.
// java
import android.util.Log;
==android.util.Log== 是在做Android开发中最常用的log输出手段.这里输出的
log, 我们通过"adb logcat"或"adb shell logcat"命令获取.
那么从"Log.d("cwj", "test log");" 到"logcat"之间到底发生了什么呢?
对于Native Code中的 ALOG 等的log打印 到logcat之间, 又发生了什么呢?
1.1 android.util.Log 和 android.util.writeEvent
1.1.1 android.util.Log
java类Log的源码在 "frameworks/base/core/java/android/util/Log.java"
Lod中的 Log.d() / Log.v() 等打印不级别的函数, 最终都走到 ==println_native
()== native函数.
println_native 的实现在 "/frameworks/base/core/jni/android_util_Log.cpp"
Log.d("cwj", "test log");
// C/C++
#define LOG_TAG "fingerprintd"
ALOG(LOG_VERBOSE, LOG_TAG, "lockout\n");
ALOGE("Invalid callback object");
ALOGD("onAcquired(%d), duplicatedFingerId(%d)", 1, 2);
public static int v(String tag, String msg) {
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
}
public static int d(String tag, String msg) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
}
/** @hide */ public static native int println_native(int bufID,
int priority, String tag, String msg);
println_native() 对应的jni方法是 ==android_util_Log_println_native()==.
/*
* JNI registration.
*/
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
...
{ "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) and
...
};
/*
* In class android.util.Log:
* public static native int println_native(int buffer, int priority, String tag,
*/
static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
jint bufID, jint priority, jstring tagObj, jstring msgObj)
{
const char* tag = NULL;
const char* msg = NULL;
if (msgObj == NULL) {
jniThrowNullPointerException(env, "println needs a message");
return -1;
}
if (bufID < 0 || bufID >= LOG_ID_MAX) {
jniThrowNullPointerException(env, "bad bufID");
return -1;
}
if (tagObj != NULL)
tag = env->GetStringUTFChars(tagObj, NULL);
msg = env->GetStringUTFChars(msgObj, NULL);
int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag,
if (tag != NULL)
env->ReleaseStringUTFChars(tagObj, tag);
env->ReleaseStringUTFChars(msgObj, msg);
return res;
}
android_util_Log_println_native()很简单,就是简单的Log级别检查后, 就调用
==__android_log_buf_write()== 做进一步处理.
__android_log_buf_write() 的声明在 "/system/core/include/log/log.h":
__android_log_buf_write() 的定义(实现)在 "/system/core/liblog".
发现liblog中有多个 __android_log_buf_write() 的实现:
那么我们用到的到底是哪个呢? 具体分析在[1.3 /system/core/liblog]介绍.
1.1.2 android.util.writeEvent
分析 writeEvent 到 liblog 的流程高度相似, 细节就不展开了, 这里直接贴下最
终的结论:
==__android_log_btwrite()== 后面调用到 ==write_to_log()==. 前面提到的
__android_log_buf_write() 也是直接调用到 write_to_log() .
即结论是: android.util.EvnentLog 跟 Android.util.Log 打印日志的流程相同,
都是转到 /system/core/liblog/logger_write.c 去处理.
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *tex
logd_write.c
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg
...
logd_write_kern.c
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg
...
logger_write.c
LIBLOG_ABI_PUBLIC int __android_log_buf_write(int bufID, int prio,
...
/frameworks/base/core/java/android/util/EventLog.java writeEvent()
--> /frameworks/base/core/jni/android_util_EventLog.cp android_btWriteLo
--> /system/core/include/log/logger.h --> log.h android_btWriteLo
--> /system/core/liblog/logger_write.c __android_log_btw
android.util.writeEvent.writeEvent() --> android_btWriteLog_xx() (/
frameworks/base/core/jni/android_util_EventLog.cpp)
--> android_btWriteLog() (/system/core/include/log/log.h) -->
__android_log_btwrite() (/system/core/liblog/logger_write.c) --> write_to_log
()
1.2 Native Code : ALOG / ALOGE / ALOGD ..
我们还会看到一些native code(主要是C/C++)也有打印log:
这些形如 "[ASR]LOG[VDIWE]" 的函数的声明在 "/system/core/include/log/
log.h":
// xxx.c / xxx.cpp
#define LOG_TAG "fingerprintd"
ALOG(LOG_VERBOSE, LOG_TAG, "lockout\n");
ALOGE("Invalid callback object");
ALOGD("onAcquired(%d), duplicatedFingerId(%d)", 1, 2);
// ALOGD/ALOGE ---> ALOG ---> LOG_PRI ---> android_printLog ---> __android_log_
/*
* Basic log message macro.
*
* Example:
* ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
*
* The second argument may be NULL or "" to indicate the "global" tag.
*/
#ifndef ALOG
#define ALOG(priority, tag, ...) \
LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
#endif
/*
* Simplified macro to send an error log message using the current LOG_TAG.
*/
#ifndef ALOGE
#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
剩余34页未读,继续阅读
cwjxiuxing
- 粉丝: 3
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1