当前位置:   article > 正文

Android logcat系统

android logcat

一 .logcat命令介绍

android log系统:

logcat介绍 :

logcat是android中的一个命令行工具,可以用于得到程序的log信息.

二.C/C++logcat访问接口

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 logcat访问接口

Android系统在Frameworks层中定义了Log接口(frameworks/base/core/java/android/util/Log.java):

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

四.logcat命令参数

参数

描述

-b <buffer>加载一个可使用的日志缓冲区供查看,比如event和radio。默认值是main
-c清除缓冲区中的全部日志并退出(清除完后可以使用-g查看缓冲区)
-d将缓冲区的log转存到屏幕中然后退出
-f <filename>将log输出到指定的文件中<文件名>.默认为标准输出(stdout)
-g打印日志缓冲区的大小并退出
-n <count>设置日志的最大数目<count>,默认值是4,需要和-r选项一起使用
-r <kbytes>没<kbytes>时输出日志,默认值是16,需要和-f选项一起使用
-s设置过滤器
-v <format>设置输出格式的日志消息。默认是短暂的格式。支持的格式列表
  1. //将缓冲区的log打印到屏幕并退出
  2. adb logcat -d
  3. //清除缓冲区log(testCase运行前可以先清除一下)
  4. adb logcat -c
  5. //打印缓冲区大小并退出
  6. adb logcat -g
  7. //输出log
  8. adb logcat -f /data/local/tmp/log.txt -n 10 -r 1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/398372
推荐阅读
相关标签
  

闽ICP备14008679号