【Android源码 栏目提醒】:网学会员,鉴于大家对Android源码 十分关注,论文会员在此为大家搜集整理了“Android系统开发中LOG的使用 - 电子设计”一文,供大家参考学习!
Android系统开发中LOG的使用 在程序开发过程中LOG是广泛使用的用来记录程序执行过程的机制它既可以用于程序调试也可以用于产品运营中的事件记录。
在
Android系统中提供了简单、便利的LOG机制开发人员可以方便地使用。
在这一篇文章中我们简单介绍在
Android内核空间和用户空间中LOG的使用和查看方法。
一. 内核开发时LOG的使用。
Android内核是基于Linux Kerne 2.36的因此Linux Kernel的LOG机制同样适合于
Android内核它就是有名的printk与C语言的printf齐名。
与printf类似printk提供格式化输入功能同时它也具有所有LOG机制的特点--提供日志级别过虑功能。
printk提供了8种日志级别 cpp view plaincopy 1. define KERN_EMERG / system is unusable / 2. define KERN_ALERT / action must be taken immediately / 3. define KERN_CRIT / critical conditions / 4. deinfe KERN_ERR / error conditions / 5. deinfe KERN_WARNING / warning conditions / 6. deinfe KERN_NOTICE / normal but significant condition / 7. deinfe KERN_INFO / informational / 8. deinfe KERN_DEBUG / debug-level messages / printk的使用方法 printkKERN_ALERTThis is the log printed by printk in linux kernel space. KERN_ALERT表示日志级别后面紧跟着要格式化字符串。
在
Android系统中printk输出的日志信息保存在/proc/kmsg中要查看/proc/kmsg的内容参照在Ubuntu上下载、编译和安装
Android最新内核源代码Linux Kernel一文在后台中运行模拟器 USER-NAMEMACHINE-NAME:/
Android emulator 启动adb shell工具 USER-NAMEMACHINE-NAME:/
Android adb shell 查看/proc/kmsg文件 rootandroid:/ 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. / 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... void0 16. else 17. define LOGV... voidLOGLOG_VERBOSE LOG_TAG __VA_ARGS__ 18. endif 19. endif 20. 21. / 22. Basic log message macro. 23. 24. Example: 25. LOGLOG_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 LOGpriority tag ... 31. LOG_PRIANDROID_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_PRIpriority tag ... 39.
android_printLogpriority 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_printLogprio tag fmt... 48. __
android_log_printprio tag fmt 因此如果要使用C/C日志接口只要定义自己的LOG_TAG宏和包含头文件system/core/include/cutils/log.h就可以了 define LOG_TAG MY LOG TAG include 就可以了例如使用LOGV LOGVThis 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. 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 vString tag String msg 40. return println_nativeLOG_ID_MAIN VERBOSE tag msg 41. 42. 43. public static int vString tag String msg Throwable tr 44. return println_nativeLOG_ID_MAIN VERBOSE tag msg n getStackTraceStringtr 45. 46. 47. public static int dString tag String msg 48. return println_nativeLOG_ID_MAIN DEBUG tag msg 49. 50. 51. public static int dString tag String msg Throwable tr 52. return println_nativeLOG_ID_MAIN DEBUG tag msg n getStackTraceStringtr 53. 54. 55. public static int iString tag String msg 56. return println_nativeLOG_ID_MAIN INFO tag msg 57. 58. 59. public static int iString tag String msg Throwable tr 60. return println_nativeLOG_ID_MAIN INFO tag msg n getStackTraceStringtr 61. 62. 63. public static int wString tag String msg 64. return println_nativeLOG_ID_MAIN WARN tag msg 65. 66. 67. public static int wString tag String msg Throwable tr 68. return println_nativeLOG_ID_MAIN WARN tag msg n getStackTraceStringtr 69. 70. 71. public static int wString tag Throwable tr 72. return println_nativeLOG_ID_MAIN WARN tag getStackTraceStringtr 73. 74. 75. public static int eString tag String msg 76. return println_nativeLOG_ID_MAIN ERROR tag msg 77. 78. 79. public static int eString tag String msg Throwable tr 80. return println_nativeLOG_ID_MAIN ERROR tag msg n getStackTraceStringtr 81. 82. 83. .................................................................. 84. 85. /hide / public static native int println_nativeint 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.iLOG_TAG This is the log printed by Log.i in
android user space. 要查看这些LOG的输出可以配合logcat工具。
如果是在Eclipse环境下运行模拟器并且安装了
Android插件那么很简单直接在Eclipse就可以查看了 如果是在自己编译的
Android源代码工程中使用则在后台中运行模拟器 USER-NAMEMACHINE-NAME:/
Android emulator 启动adb shell工具 USER-NAMEMACHINE-NAME:/
Android adb shell 使用logcat命令查看日志 rootandroid:/ logcat 这样就可以看到输出的日志了。
上一篇:
基于Android平台的USB数据采集系统
下一篇:
阴道松弛练紧三法