Android Log System

文章目录

    • 日志等级
    • 日志分类
    • [liblog 的使用](#liblog 的使用)
    • [配置发送给 logd 的日志等级](#配置发送给 logd 的日志等级)
    • logcat

日志等级

日志等级可快速配置该日志在配置的环境下是否会输出。

cpp 复制代码
typedef enum android_LogPriority {
  /** For internal use only.  */
  ANDROID_LOG_UNKNOWN = 0,
  /** The default priority, for internal use only.  */
  ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
  /** Verbose logging. Should typically be disabled for a release apk. */
  ANDROID_LOG_VERBOSE,
  /** Debug logging. Should typically be disabled for a release apk. */
  ANDROID_LOG_DEBUG,
  /** Informational logging. Should typically be disabled for a release apk. */
  ANDROID_LOG_INFO,
  /** Warning logging. For use with recoverable failures. */
  ANDROID_LOG_WARN,
  /** Error logging. For use with unrecoverable failures. */
  ANDROID_LOG_ERROR,
  /** Fatal logging. For use when aborting. */
  ANDROID_LOG_FATAL,
  /** For internal use only.  */
  ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority;

日志分类

每个日志类型都有自己的 RingBuffer。

常规的日志 __android_log_write 不带 log_id 分类参数,默认是往 main 中写入。

c 复制代码
typedef enum log_id {
  /** For internal use only.  */
  LOG_ID_MIN = 0,

  /** The main log buffer. This is the only log buffer available to apps. */
  LOG_ID_MAIN = 0,
  /** The radio log buffer. */
  LOG_ID_RADIO = 1,
  /** The event log buffer. */
  LOG_ID_EVENTS = 2,
  /** The system log buffer. */
  LOG_ID_SYSTEM = 3,
  /** The crash log buffer. */
  LOG_ID_CRASH = 4,
  /** The statistics log buffer. */
  LOG_ID_STATS = 5,
  /** The security log buffer. */
  LOG_ID_SECURITY = 6,
  /** The kernel log buffer. */
  LOG_ID_KERNEL = 7,

  /** For internal use only.  */
  LOG_ID_MAX,

  /**
   * Let the logging library choose the best log target in cases where it's
   * unclear. This is useful if you're generic library code that can't know
   * which log your caller should use.
   */
  LOG_ID_DEFAULT = 0x7FFFFFFF
} log_id_t;

liblog 的使用

c 复制代码
#include <android/log.h>

// 输出字符串
__android_log_write(ANDROID_LOG_DEBUG, "v_gal", "Button clicked");
__android_log_print(ANDROID_LOG_DEBUG, "v_gal", "Player score: %d", score);

配置发送给 logd 的日志等级

liblog 库会读取 Property 来获取日志显示等级,确定当前日志级别是否需要发送给 logd

源码路径:system/logging/liblog/properties.cpp

相关函数:

c 复制代码
static int __android_log_level(const char* tag, size_t tag_len)

该函数会读取四个 Property 来判断:

  1. log.tag.<TAG>
  2. persist.log.tag.<TAG>
  3. log.tag
  4. persist.log.tag

如果没有给 tag 设置任何属性,则系统的默认等级为 INFO:

shell 复制代码
$ getprop | grep log.tag
[log.tag.stats_log]: [I]

可以通过修改 Property 来修改发送日志的行为:

shell 复制代码
$ setprop log.tag.<TAG> DEBUG

<TAG> 标签 DEBUG 及以上等级日志均会发送给 logd

logcat

logcat 从 logd 中读取日志,由于 logd 有 8 个分类的 RingBuffer,可以通过 --buffer 选项来选择:

  1. main
  2. radio
  3. events
  4. system
  5. crash
  6. stats
  7. security
  8. kernel
  9. default
  10. all
shell 复制代码
$ logcat --buffer=crash

如果不设置 buffer,则默认为 default,包含 main, system, crash, kernel

查看和设置 RingBuffer 大小:

shell 复制代码
### 查看 RingBuffer 的大小
$ logcat --buffer-size
### 设置 RingBuffer 的大小
$ logcat --buffer=main --buffer-size=256K
shell 复制代码
### 清除 RingBuffer 中的日志
$ logcat --clear
shell 复制代码
### -d:执行命令后立刻退出,而不是持续监听
### -s: 静默所有日志
### <tag>[:priority]:只打印标签为 <tag>,并且优先级为 priority 以上的日志
$ logcat -d -s <tag>[:priority]
相关推荐
千里马学框架1 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone1 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc1 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo1 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077001 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼1 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone1 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen1 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone1 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui