本文转载自:http://blog.csdn.net/luoshengyang/article/details/6581828

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

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

1 #define KERN_EMERG    "<0>"     /* system is unusable             */
2 #define KERN_ALERT    "<1>"     /* action must be taken immediately    */
3 #define KERN_CRIT    "<2>"     /* critical conditions            */
4 #deinfe KERN_ERR    "<3>"     /* error conditions            */
5 #deinfe KERN_WARNING    "<4>"     /* warning conditions            */
6 #deinfe KERN_NOTICE    "<5>"     /* normal but significant condition    */
7 #deinfe KERN_INFO    "<6>"     /* informational             */
8 #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定义了日志的级别:

 1 /*
 2  * Android log priority values, in ascending priority order.
 3  */
 4 typedef enum android_LogPriority {
 5     ANDROID_LOG_UNKNOWN = 0,
 6     ANDROID_LOG_DEFAULT,    /* only for SetMinPriority() */
 7     ANDROID_LOG_VERBOSE,
 8     ANDROID_LOG_DEBUG,
 9     ANDROID_LOG_INFO,
10     ANDROID_LOG_WARN,
11     ANDROID_LOG_ERROR,
12     ANDROID_LOG_FATAL,
13     ANDROID_LOG_SILENT,    /* only for SetMinPriority(); must be last */
14 } android_LogPriority;

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

 1 /*
 2  * This is the local tag used for the following simplified
 3  * logging macros. You can change this preprocessor definition
 4  * before using the other macros to change the tag.
 5  */
 6 #ifndef LOG_TAG
 7 #define LOG_TAG NULL
 8 #endif
 9
10 /*
11  * Simplified macro to send a verbose log message using the current LOG_TAG.
12  */
13 #ifndef LOGV
14 #if LOG_NDEBUG
15 #define LOGV(...)   ((void)0)
16 #else
17 #define LOGV(...)   ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
18 #endif
19 #endif
20
21 /*
22  * Basic log message macro.
23  *
24  * Example:
25  *  LOG(LOG_WARN, NULL, "Failed with error %d", errno);
26  *
27  * The second argument may be NULL or "" to indicate the "global" tag.
28  */
29 #ifndef LOG
30 #define LOG(priority, tag, ...) \
31      LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
32 #endif
33
34 /*
35  * Log macro that allows you to specify a number for priority.
36  */
37 #ifndef LOG_PRI
38 #define LOG_PRI(priority, tag, ...) \
39      android_printLog(priority, tag, __VA_ARGS__)
40 #endif
41
42 /*
43  * ================================================================
44  *
45  * The stuff in the rest of this file should not be used directly.
46  */
47 #define android_printLog(prio, tag, fmt...) \
48      __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):

 1 ................................................
 2
 3 public final class Log {
 4
 5 ................................................
 6
 7     /**
 8      * Priority constant for the println method; use Log.v.
 9          */
10     public static final int VERBOSE = 2;
11
12     /**
13      * Priority constant for the println method; use Log.d.
14          */
15     public static final int DEBUG = 3;
16
17     /**
18      * Priority constant for the println method; use Log.i.
19          */
20     public static final int INFO = 4;
21
22     /**
23      * Priority constant for the println method; use Log.w.
24          */
25     public static final int WARN = 5;
26
27     /**
28      * Priority constant for the println method; use Log.e.
29          */
30     public static final int ERROR = 6;
31
32     /**
33      * Priority constant for the println method.
34          */
35     public static final int ASSERT = 7;
36
37 .....................................................
38
39     public static int v(String tag, String msg) {
40         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
41     }
42
43     public static int v(String tag, String msg, Throwable tr) {
44         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));
45     }
46
47     public static int d(String tag, String msg) {
48         return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
49     }
50
51     public static int d(String tag, String msg, Throwable tr) {
52         return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
53     }
54
55     public static int i(String tag, String msg) {
56         return println_native(LOG_ID_MAIN, INFO, tag, msg);
57     }
58
59     public static int i(String tag, String msg, Throwable tr) {
60         return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));
61     }
62
63     public static int w(String tag, String msg) {
64         return println_native(LOG_ID_MAIN, WARN, tag, msg);
65     }
66
67     public static int w(String tag, String msg, Throwable tr) {
68         return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));
69     }
70
71     public static int w(String tag, Throwable tr) {
72         return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
73     }
74
75     public static int e(String tag, String msg) {
76         return println_native(LOG_ID_MAIN, ERROR, tag, msg);
77     }
78
79     public static int e(String tag, String msg, Throwable tr) {
80         return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));
81     }
82
83 ..................................................................
84
85     /**@hide */ public static native int println_native(int bufID,
86         int priority, String tag, String msg);
87 }

因此,如果要使用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

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

浅谈Android系统开发中LOG的使用【转】相关推荐

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

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用 ...

  2. Android NDK开发之 Android系统开发中LOG的使用

    浅谈Android系统开发中LOG的使用 转自:http://blog.csdn.net/luoshengyang/article/details/6581828

  3. android系统开发中log的使用方法

    1. Linux内核的log输出 使用printk打印级别且将信息保存到/proc/kmsg日志中,使用cat命令查看其信息[ cat /proc/kmsg ] 2. android中log输出 an ...

  4. 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路

    原文地址: http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service Manager成为Android进程间 ...

  5. 浅谈Android游戏开发基础和经验

    Android游戏开发基础和经验是本文要介绍的内容,主要是来了解并学习Android游戏开发的内容实例,具体关于Android游戏开发内容的详解来看本文. 做一个类似俄罗斯方块的android游戏开发 ...

  6. 浅谈Android SDK开发

    目录 浅谈Android SDK开发 SDK开发的原则 SDK设计 功能与职责边界设计 接口设计 兼容性设计 发布输出设计 SDK文档设计 SDK项目架构 组件化.模块化 统一资源管理 第三方依赖管理 ...

  7. 嵌入式开发-浅谈嵌入式MCU开发中的三个常见误区

    浅谈嵌入式MCU开发中的三个常见误区 原创 2017-09-30 胡恩伟 汽车电子expert成长之路 目录 (1)嵌入式MCU与MPU的区分 (2)误区一:MCU的程序都是存储在片上Flash上,然 ...

  8. 《浅谈-Android系统越用反应越慢的问题》

    <浅谈-Android系统越用反应越慢的问题> android应用程序和iphone应用程序不一样,用过iphone的都知道,点击图标进入程序后,如果还想用其他程序,必须先按返回退出然后进 ...

  9. 问渠哪得清如许,唯有源头活水来-浅谈android 系统

    古人学问无遗力,少壮功夫老始成,纸上得来终觉浅,绝知此事要躬行 android系统是基于Linux平台的开源移动操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成. 底层以Linux内核 ...

最新文章

  1. 『模板 高精度计算』
  2. windows下eclipse连接hadoop
  3. JQuery 判断checkbox是否选中,checkbox全选,获取checkbox选中值
  4. hyperopt中文文档:Parallelizing-Evaluations-During-Search-via-MongoDB
  5. NPOI操作Excel
  6. 光端机的几种物理接口类型
  7. sql server 数据库还原
  8. python3 字符串格式化_Python3-字符串格式化
  9. 通过CrawlSpider对招聘网站进行整站爬取(拉勾网实战)
  10. 珍稀干货!阿里 Web 音视频开发趟坑指南
  11. 如果微软开发了 Android,会有何不同?
  12. ARC_xp_20160526
  13. WebLogic—在Eclipse上配置WebLogic Server
  14. 【科普视频】信号在时域和频域上的区别
  15. Ubuntu 14.04 卸载搜狗拼音输入法及后续问题解决
  16. 300万数据导入导出优化方案,从80s优化到8s(实测)
  17. Hitters数据集数据分析
  18. 关于迅雷播放器的模仿
  19. MAC下用F9-F12模拟PageUP/PageDown/HOME/END
  20. stm32开发3D打印机(零)——打印板配置(未完成)

热门文章

  1. 面进了心心念念的国企!以为TM上岸了!干了1年!我却再次选择回到互联网大厂内卷!
  2. 今天终于搞懂了:为什么 Java 的 main 方法必须是 public static void?
  3. MySQL:教你学会如何做性能分析与查询优化
  4. SpringMVC工作原理详解
  5. LeetCode刷题指南!
  6. 集锦分享 | 200篇原创笔记,帮助你快速入门Python与机器学习
  7. 从理论到实践,Top选手带你进入数据竞赛的大门
  8. 刚刚,2021中国最好学科排名发布:北大、清华、人大中国顶尖学科居前三
  9. AI新作品:照片迅速被画成艺术画
  10. 我想说:mysql 的 join 真的很弱