浅谈Android系统开发中LOG的使用

在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录。在Android系统中,提供了简单、便利的LOG机制,开发人员可以方便地使用。在这一篇文章中,我们简单介绍在Android内核空间和用户空间中LOG的使用和查看方法。

《Android系统源代码情景分析》一书正在进击的程序员网(http://0xcc0xcd.com)中连载,点击进入!

        一. 内核开发时LOG的使用。Android内核是基于Linux Kerne 2.36的,因此,Linux Kernel的LOG机制同样适合于Android内核,它就是有名的printk,与C语言的printf齐名。与printf类似,printk提供格式化输入功能,同时,它也具有所有LOG机制的特点--提供日志级别过虑功能。printk提供了8种日志级别(<linux/kernel.h>):

#define KERN_EMERG "<0>" /* system is unusable */

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

#define KERN_CRIT "<2>" /* critical conditions */

#deinfe KERN_ERR "<3>" /* error conditions */

#deinfe KERN_WARNING "<4>" /* warning conditions */

#deinfe KERN_NOTICE "<5>" /* normal but significant condition */

#deinfe KERN_INFO "<6>" /* informational */

#deinfe KERN_DEBUG "<7>" /* debug-level messages */

      printk的使用方法:

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

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

      在Android系统中,printk输出的日志信息保存在/proc/kmsg中,要查看/proc/kmsg的内容,参照在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)一文,在后台中运行模拟器:

      USER-NAME@MACHINE-NAME:~/Android$ emulator &

      启动adb shell工具:

      USER-NAME@MACHINE-NAME:~/Android$ adb shell

      查看/proc/kmsg文件:

      root@android:/ # cat  /proc/kmsg

      二. 用户空间程序开发时LOG的使用。Android系统在用户空间中提供了轻量级的logger日志系统,它是在内核中实现的一种设备驱动,与用户空间的logcat工具配合使用能够方便地跟踪调试程序。在Android系统中,分别为C/C++ 和Java语言提供两种不同的logger访问接口。C/C++日志接口一般是在编写硬件抽象层模块或者编写JNI方法时使用,而Java接口一般是在应用层编写APP时使用。

      Android系统中的C/C++日志接口是通过宏来使用的。在system/core/include/android/log.h定义了日志的级别:

/*

* Android log priority values, in ascending priority order.

*/

typedef enum android_LogPriority {

ANDROID_LOG_UNKNOWN = 0,

ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */

ANDROID_LOG_VERBOSE,

ANDROID_LOG_DEBUG,

ANDROID_LOG_INFO,

ANDROID_LOG_WARN,

ANDROID_LOG_ERROR,

ANDROID_LOG_FATAL,

ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */

} android_LogPriority;

      在system/core/include/cutils/log.h中,定义了对应的宏,如对应于ANDROID_LOG_VERBOSE的宏LOGV:

/*

* This is the local tag used for the following simplified

* logging macros. You can change this preprocessor definition

* before using the other macros to change the tag.

*/

#ifndef LOG_TAG

#define LOG_TAG NULL

#endif

/*

* Simplified macro to send a verbose log message using the current LOG_TAG.

*/

#ifndef LOGV

#if LOG_NDEBUG

#define LOGV(...)  ((void)0)

#else

#define LOGV(...)  ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))

#endif

#endif

/*

* Basic log message macro.

*

* Example:

*  LOG(LOG_WARN, NULL, "Failed with error %d", errno);

*

* The second argument may be NULL or "" to indicate the "global" tag.

*/

#ifndef LOG

#define LOG(priority, tag, ...) \

    LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)

#endif

/*

* Log macro that allows you to specify a number for priority.

*/

#ifndef LOG_PRI

#define LOG_PRI(priority, tag, ...) \

    android_printLog(priority, tag, __VA_ARGS__)

#endif

/*

* ================================================================

*

* The stuff in the rest of this file should not be used directly.

*/

#define android_printLog(prio, tag, fmt...) \

    __android_log_print(prio, tag, fmt)

        因此,如果要使用C/C++日志接口,只要定义自己的LOG_TAG宏和包含头文件system/core/include/cutils/log.h就可以了:

        #define LOG_TAG "MY LOG TAG"

        #include <cutils/log.h>

        就可以了,例如使用LOGV:

        LOGV("This is the log printed by LOGV in android user space.");

        再来看Android系统中的Java日志接口。Android系统在Frameworks层中定义了Log接口(frameworks/base/core/java/android/util/Log.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 println_native(int bufID,

int priority, String tag, String msg);

}

        因此,如果要使用Java日志接口,只要在类中定义的LOG_TAG常量和引用android.util.Log就可以了:

        private static final String LOG_TAG = "MY_LOG_TAG";

        Log.i(LOG_TAG, "This is the log printed by Log.i in android user space.");

        要查看这些LOG的输出,可以配合logcat工具。如果是在Eclipse环境下运行模拟器,并且安装了Android插件,那么,很简单,直接在Eclipse就可以查看了:


        如果是在自己编译的Android源代码工程中使用,则在后台中运行模拟器:

      USER-NAME@MACHINE-NAME:~/Android$ emulator &

      启动adb shell工具:

      USER-NAME@MACHINE-NAME:~/Android$ adb shell

      使用logcat命令查看日志:

      root@android:/ # logcat

      这样就可以看到输出的日志了。

————————————————

版权声明:本文为CSDN博主「罗升阳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Luoshengyang/article/details/6581828

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容