Android内核的Log输出

Android内核是基于Linxu kernel的,因此其log机制也是通用的,在Android内核中使用printk函数进行Log输出。与c语言的printf类似,printk提供格式化输入功能,同时,它也具有所有LOG机制的特点--提供日志级别过虑功能。printk提供了8种日志级别(详细的定义在include/linux/kern_levels.h中):

#ifndef __KERN_LEVELS_H__

#define __KERN_LEVELS_H__

#define KERN_SOH "\001" /* ASCII Start Of Header */

#define KERN_SOH_ASCII '\001'

#define KERN_EMERG KERN_SOH "0" /* system is unusable */

#define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */

#define KERN_CRIT KERN_SOH "2" /* critical conditions */

#define KERN_ERR KERN_SOH "3" /* error conditions */

#define KERN_WARNING KERN_SOH "4" /* warning conditions */

#define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */

#define KERN_INFO KERN_SOH "6" /* informational */

#define KERN_DEBUG KERN_SOH "7" /* debug-level messages */

#define KERN_DEFAULT KERN_SOH "d" /* the default kernel loglevel */

/*

* Annotation for a "continued" line of log printout (only done after a

* line that had no enclosing \n). Only to be used by core/arch code

* during early bootup (a continued line is not SMP-safe otherwise).

*/

#define KERN_CONT ""

#endif

printk的最简单的使用方法:

//KERN_ALERT表示日志级别,后面紧跟着要格式化字符串。

printk(KERN_ALERT "This is the log printed by printk in linux kernel space.");

printk的带格式化输出使用方法:

printk(KERN_ERR "%s: Invalid parameter\n", __func__);

adb connect情况下输出kernel打印信息

Android系统中,printk输出的日志信息保存在/proc/kmsg中,要查看/proc/kmsg的内容,只输入在命令行中输入如下命令

shell@rk3288_box:/ # cat /proc/kmsg

输出结果如图

9b657c3c3560

Paste_Image.png

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐