文章转载至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>):

[cpp] view plaincopy
  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定义了日志的级别:

[cpp] view plaincopy
  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:

[cpp] view plaincopy
  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. * Simplified macro to send a verbose log message using the current LOG_TAG.
  11. */
  12. #ifndef LOGV
  13. #if LOG_NDEBUG
  14. #define LOGV(...)   ((void)0)
  15. #else
  16. #define LOGV(...)   ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  17. #endif
  18. #endif
  19. /*
  20. * Basic log message macro.
  21. *
  22. * Example:
  23. *  LOG(LOG_WARN, NULL, "Failed with error %d", errno);
  24. *
  25. * The second argument may be NULL or "" to indicate the "global" tag.
  26. */
  27. #ifndef LOG
  28. #define LOG(priority, tag, ...) \
  29. LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
  30. #endif
  31. /*
  32. * Log macro that allows you to specify a number for priority.
  33. */
  34. #ifndef LOG_PRI
  35. #define LOG_PRI(priority, tag, ...) \
  36. android_printLog(priority, tag, __VA_ARGS__)
  37. #endif
  38. /*
  39. * ================================================================
  40. *
  41. * The stuff in the rest of this file should not be used directly.
  42. */
  43. #define android_printLog(prio, tag, fmt...) \
  44. __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):

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

因此,如果要使用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的使用相关推荐

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

    本文转载自: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. 零基础入门学习Python(21)-递归1
  2. 2018年python薪资_2018年国内就业薪资高的7大编程语言排行
  3. android 测试人员测试时使用release版本还是debug版本_为什么做软件测试
  4. python检测端口是否被侦听
  5. 内网穿透的一种方式——基于ngrok的小米球
  6. 电脑数据恢复软件推荐10款
  7. Qt::WA_TransparentForMouseEvents 了解一下
  8. 用计算机最炫民族风乐谱,最炫民族风乐谱及歌词
  9. VMware虚拟机复制文件卡死的问题
  10. CSS3 filter滤镜详解
  11. 基因组测序中N50和N90到底指什么?
  12. 网络工程师之网络规划
  13. java程序员工资有多少?java程序员现状如何?
  14. IMP-00008错误
  15. openssl源码中的头文件include error
  16. 研发质量保障体系搭建
  17. CES Aisa总结篇|盛况依旧,却始终缺了点新意和真实落地的感觉
  18. 美团-轻食餐饮发展指南
  19. Android_基于g-sensor的坐下动作的的识别
  20. 为什么有的测试员路越走越窄?原因在这里

热门文章

  1. 网络广告推广浅析网站中的关键词密度要如何控制呢?
  2. 浅析网站排版如何让用户获得良好的访问体验
  3. 长方形纸做容积最大的长方体_A4纸的尺寸是怎么来的?
  4. harmonyos上的程序用什么语言写,HarmonyOS应用开发 — HelloWorld应用开发E2E体验
  5. 零基础python入门书籍推荐书目_铁粉看过来!送书啦,Python推荐书单,送书10本!...
  6. Linux上新建用户及赋权操作
  7. RecyclerView父组件和子组件点击冲突问题解决
  8. https ddos检测——研究现状
  9. CC通信软件list
  10. LSM树——LSM 将B+树等结构昂贵的随机IO变的更快,而代价就是读操作要处理大量的索引文件(sstable)而不是一个,另外还是一些IO被合并操作消耗。...