蕉下客--)

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 Android系统各层中LOG的使用

来源: http://blog.csdn.net/luoshengyang/article/details/6581828


导语:

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


内核层Log:

Android内核是基于Linux Kerne 2.36的,Linux Kernel的LOG机制同样适合于Android内核,它就是有名的printk,与C语言的printf齐名。同时,它也具有所有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  cop

printk的使用方法:printk(KERN_ALERT"This is the log printed by printk in linux kernel space.");

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


查看/proc/kmsg文件:  cat /proc/kmsg

       

C++接口 Log:

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. * 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.");

         


Java接口 Log:

Log接口:frameworks/base/core/java/android/util/Log.java

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

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






posted on 2017-06-09 01:19  蕉下客--)  阅读(483)  评论(0编辑  收藏  举报