浅谈Android系统开发中LOG的使用
文章转载至CSDN社区罗升阳的安卓之旅,原文地址: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>):
- #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
这样就可以看到输出的日志了。
老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注!
转载于:https://www.cnblogs.com/Free-Thinker/p/4142018.html
浅谈Android系统开发中LOG的使用相关推荐
- 浅谈Android系统开发中LOG的使用【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以 ...
- Android NDK开发之 Android系统开发中LOG的使用
浅谈Android系统开发中LOG的使用 转自:http://blog.csdn.net/luoshengyang/article/details/6581828
- android系统开发中log的使用方法
1. Linux内核的log输出 使用printk打印级别且将信息保存到/proc/kmsg日志中,使用cat命令查看其信息[ cat /proc/kmsg ] 2. android中log输出 an ...
- 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路
原文地址: http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service Manager成为Android进程间 ...
- 浅谈Android游戏开发基础和经验
Android游戏开发基础和经验是本文要介绍的内容,主要是来了解并学习Android游戏开发的内容实例,具体关于Android游戏开发内容的详解来看本文. 做一个类似俄罗斯方块的android游戏开发 ...
- 浅谈Android SDK开发
目录 浅谈Android SDK开发 SDK开发的原则 SDK设计 功能与职责边界设计 接口设计 兼容性设计 发布输出设计 SDK文档设计 SDK项目架构 组件化.模块化 统一资源管理 第三方依赖管理 ...
- 嵌入式开发-浅谈嵌入式MCU开发中的三个常见误区
浅谈嵌入式MCU开发中的三个常见误区 原创 2017-09-30 胡恩伟 汽车电子expert成长之路 目录 (1)嵌入式MCU与MPU的区分 (2)误区一:MCU的程序都是存储在片上Flash上,然 ...
- 《浅谈-Android系统越用反应越慢的问题》
<浅谈-Android系统越用反应越慢的问题> android应用程序和iphone应用程序不一样,用过iphone的都知道,点击图标进入程序后,如果还想用其他程序,必须先按返回退出然后进 ...
- 问渠哪得清如许,唯有源头活水来-浅谈android 系统
古人学问无遗力,少壮功夫老始成,纸上得来终觉浅,绝知此事要躬行 android系统是基于Linux平台的开源移动操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成. 底层以Linux内核 ...
最新文章
- 零基础入门学习Python(21)-递归1
- 2018年python薪资_2018年国内就业薪资高的7大编程语言排行
- android 测试人员测试时使用release版本还是debug版本_为什么做软件测试
- python检测端口是否被侦听
- 内网穿透的一种方式——基于ngrok的小米球
- 电脑数据恢复软件推荐10款
- Qt::WA_TransparentForMouseEvents 了解一下
- 用计算机最炫民族风乐谱,最炫民族风乐谱及歌词
- VMware虚拟机复制文件卡死的问题
- CSS3 filter滤镜详解
- 基因组测序中N50和N90到底指什么?
- 网络工程师之网络规划
- java程序员工资有多少?java程序员现状如何?
- IMP-00008错误
- openssl源码中的头文件include error
- 研发质量保障体系搭建
- CES Aisa总结篇|盛况依旧,却始终缺了点新意和真实落地的感觉
- 美团-轻食餐饮发展指南
- Android_基于g-sensor的坐下动作的的识别
- 为什么有的测试员路越走越窄?原因在这里
热门文章
- 网络广告推广浅析网站中的关键词密度要如何控制呢?
- 浅析网站排版如何让用户获得良好的访问体验
- 长方形纸做容积最大的长方体_A4纸的尺寸是怎么来的?
- harmonyos上的程序用什么语言写,HarmonyOS应用开发 — HelloWorld应用开发E2E体验
- 零基础python入门书籍推荐书目_铁粉看过来!送书啦,Python推荐书单,送书10本!...
- Linux上新建用户及赋权操作
- RecyclerView父组件和子组件点击冲突问题解决
- https ddos检测——研究现状
- CC通信软件list
- LSM树——LSM 将B+树等结构昂贵的随机IO变的更快,而代价就是读操作要处理大量的索引文件(sstable)而不是一个,另外还是一些IO被合并操作消耗。...