日志记录

摘要

枚举

android_LogPriority{
  ANDROID_LOG_UNKNOWN = 0,
  ANDROID_LOG_DEFAULT,
  ANDROID_LOG_VERBOSE,
  ANDROID_LOG_DEBUG,
  ANDROID_LOG_INFO,
  ANDROID_LOG_WARN,
  ANDROID_LOG_ERROR,
  ANDROID_LOG_FATAL,
  ANDROID_LOG_SILENT
}
枚举
Android 日志优先级值,按优先级升序排列。
log_id{
  LOG_ID_MIN = 0,
  LOG_ID_MAIN = 0,
  LOG_ID_RADIO = 1,
  LOG_ID_EVENTS = 2,
  LOG_ID_SYSTEM = 3,
  LOG_ID_CRASH = 4,
  LOG_ID_STATS = 5,
  LOG_ID_SECURITY = 6,
  LOG_ID_KERNEL = 7,
  LOG_ID_MAX,
  LOG_ID_DEFAULT = 0x7FFFFFFF
}
枚举
标识用于 __android_log_buf_write()__android_log_buf_print() 的特定日志缓冲区。

类型定义

__android_aborter_function)(const char *abort_message) typedef
void(*
当 liblog 因 __android_log_assert() 失败而中止时调用的 'abort' 函数的原型。
__android_logger_function)(const struct __android_log_message *log_message) typedef
void(*
为每个日志消息调用的 'logger' 函数的原型。
android_LogPriority typedef
Android 日志优先级值,按优先级升序排列。
log_id_t typedef
枚举 log_id
标识用于 __android_log_buf_write()__android_log_buf_print() 的特定日志缓冲区。

函数

__android_log_assert(const char *cond, const char *tag, const char *fmt, ...)
void
将断言失败写入日志(作为 ANDROID_LOG_FATAL)和 stderr,然后调用 abort(3)
__android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
int
将格式化字符串写入日志缓冲区 id,优先级为 prio,标签为 tag
__android_log_buf_write(int bufID, int prio, const char *tag, const char *text)
int
将常量字符串 text 写入日志缓冲区 id,优先级为 prio,标签为 tag
__android_log_call_aborter(const char *abort_message)
void
调用存储的中止函数。
__android_log_default_aborter(const char *abort_message)
void
在设备上设置 android_set_abort_message(),然后调用 aborts()。
__android_log_get_minimum_priority(void)
int32_t
获取此进程将记录的最低优先级。
__android_log_id_is_valid(log_id_t id)
bool
__android_log_is_loggable(int prio, const char *tag, int default_prio)
int
使用每标签属性“log.tag。",以及 __android_log_set_minimum_priority() 设置的最低优先级,以确定是否将打印具有给定 prio 和 tag 的日志消息。
__android_log_is_loggable_len(int prio, const char *tag, size_t len, int default_prio)
int
使用每标签属性“log.tag。",以及 __android_log_set_minimum_priority() 设置的最低优先级,以确定是否将打印具有给定 prio 和 tag 的日志消息。
__android_log_logd_logger(const struct __android_log_message *log_message)
void
将日志消息写入 logd。
__android_log_print(int prio, const char *tag, const char *fmt, ...)
int
将格式化字符串写入日志,优先级为 prio,标签为 tag
__android_log_set_aborter(__android_aborter_function aborter)
void
设置用户定义的中止函数,该函数在 __android_log_assert() 失败时调用。
__android_log_set_default_tag(const char *tag)
void
如果在写入日志消息时未提供标签,则设置默认标签。
__android_log_set_logger(__android_logger_function logger)
void
设置用户定义的日志记录器函数。
__android_log_set_minimum_priority(int32_t priority)
int32_t
设置此进程将记录的最低优先级。
__android_log_stderr_logger(const struct __android_log_message *log_message)
void
将日志消息写入 stderr。
__android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
int
等同于 __android_log_print(),但接受 va_list
__android_log_write(int prio, const char *tag, const char *text)
int
将常量字符串 text 写入日志,优先级为 prio,标签为 tag
__android_log_write_log_message(struct __android_log_message *log_message)
void
写入 log_message 指定的日志消息。

结构体

__android_log_message

用于通过 __android_log_write_logger_data() 将日志消息写入 liblog,并将日志消息发送到 __android_log_set_logger() 中指定的用户定义日志记录器的数据结构。

枚举

android_LogPriority

android/log.h 中声明
 android_LogPriority

Android 日志优先级值,按优先级升序排列。

属性
ANDROID_LOG_DEBUG

调试日志记录。

通常应在发布 APK 中停用。

ANDROID_LOG_DEFAULT

默认优先级,仅供内部使用。

ANDROID_LOG_ERROR

错误日志记录。

用于不可恢复的故障。

ANDROID_LOG_FATAL

致命日志记录。

中止时使用。

ANDROID_LOG_INFO

信息日志记录。

通常应在发布 APK 中停用。

ANDROID_LOG_SILENT

仅供内部使用。

ANDROID_LOG_UNKNOWN

仅供内部使用。

ANDROID_LOG_VERBOSE

详细日志记录。

通常应在发布 APK 中停用。

ANDROID_LOG_WARN

警告日志记录。

用于可恢复的故障。

log_id

android/log.h 中声明
 log_id

标识用于 __android_log_buf_write()__android_log_buf_print() 的特定日志缓冲区。

属性
LOG_ID_CRASH

崩溃日志缓冲区。

LOG_ID_DEFAULT

让日志记录函数选择最佳日志目标。

LOG_ID_EVENTS

事件日志缓冲区。

LOG_ID_KERNEL

内核日志缓冲区。

LOG_ID_MAIN

主日志缓冲区。

这是应用唯一可用的日志缓冲区。

LOG_ID_MAX
LOG_ID_MIN
LOG_ID_RADIO

无线日志缓冲区。

LOG_ID_SECURITY

安全日志缓冲区。

LOG_ID_STATS

统计信息日志缓冲区。

LOG_ID_SYSTEM

系统日志缓冲区。

类型定义

__android_aborter_function

android/log.h 中声明
void(* __android_aborter_function)(const char *abort_message)

当 liblog 因 __android_log_assert() 失败而中止时调用的 'abort' 函数的原型。

__android_logger_function

android/log.h 中声明
void(* __android_logger_function)(const struct __android_log_message *log_message)

为每个日志消息调用的 'logger' 函数的原型。

android_LogPriority

android/log.h 中声明
enum android_LogPriority android_LogPriority

Android 日志优先级值,按优先级升序排列。

log_id_t

android/log.h 中声明
enum log_id log_id_t

标识用于 __android_log_buf_write()__android_log_buf_print() 的特定日志缓冲区。

函数

__android_log_assert

android/log.h 中声明
void __android_log_assert(
  const char *cond,
  const char *tag,
  const char *fmt,
  ...
)

将断言失败写入日志(作为 ANDROID_LOG_FATAL)和 stderr,然后调用 abort(3)

如果 fmt 非空,则 cond 未使用。如果 fmt 为 null,则使用字符串 Assertion failed: s 并将 cond 作为字符串参数。如果 fmtcond 都为 null,则提供默认字符串。

大多数调用方应改用 中的 assert(3),如果需要更多控制,则使用 bionic 提供的 __assert__assert2 函数。它们比此函数更方便地支持自动包含源文件名和行号。

__android_log_buf_print

android/log.h 中声明
int __android_log_buf_print(
  int bufID,
  int prio,
  const char *tag,
  const char *fmt,
  ...
)

将格式化字符串写入日志缓冲区 id,优先级为 prio,标签为 tag

格式化的详细信息与 printf(3) 相同。

应用应改用 __android_log_print()

详细信息
返回值
如果消息已写入日志,则返回 1;如果未写入,则返回 -EPERM;请参阅 __android_log_is_loggable()

__android_log_buf_write

android/log.h 中声明
int __android_log_buf_write(
  int bufID,
  int prio,
  const char *tag,
  const char *text
)

将常量字符串 text 写入日志缓冲区 id,优先级为 prio,标签为 tag

应用应改用 __android_log_write()

详细信息
返回值
如果消息已写入日志,则返回 1;如果未写入,则返回 -EPERM;请参阅 __android_log_is_loggable()

__android_log_call_aborter

android/log.h 中声明
void __android_log_call_aborter(
  const char *abort_message
)

调用存储的中止函数。

这允许其他日志记录库通过在 liblog 中调用此函数来使用相同的中止函数。

自 API 级别 30 起可用。

详细信息
参数
abort_message
中止时提供的附加消息,例如,这用于在 __android_log_default_aborter() 中调用 android_set_abort_message()

__android_log_default_aborter

android/log.h 中声明
void __android_log_default_aborter(
  const char *abort_message
)

在设备上设置 android_set_abort_message(),然后调用 aborts()。

这是默认的中止程序。

自 API 级别 30 起可用。

详细信息
参数
abort_message
中止时提供的附加消息。此函数使用其内容调用 android_set_abort_message()

__android_log_get_minimum_priority

android/log.h 中声明
int32_t __android_log_get_minimum_priority(
  void
)

获取此进程将记录的最低优先级。

自 API 级别 30 起可用。

详细信息
返回值
当前最低优先级,如果未设置,则为 ANDROID_LOG_DEFAULT

__android_log_id_is_valid

android/log.h 中声明
bool __android_log_id_is_valid(
  log_id_t id
)

__android_log_is_loggable

android/log.h 中声明
int __android_log_is_loggable(
  int prio,
  const char *tag,
  int default_prio
)

使用每标签属性“log.tag。",以及 __android_log_set_minimum_priority() 设置的最低优先级,以确定是否将打印具有给定 prio 和 tag 的日志消息。

非零结果表示是,零表示否。

如果同时设置了标签优先级和 __android_log_set_minimum_priority() 设置的最低优先级,则取两个值中的较低者作为记录所需的最低优先级。如果只设置了一个,则使用该值确定所需的最低优先级。如果都没有设置,则使用 default_priority。

自 API 级别 30 起可用。

详细信息
参数
prio
要测试的优先级,取 android_LogPriority 值。
tag
要测试的标签。
default_prio
如果未设置任何属性或最低优先级,则使用的默认优先级。
返回值
一个整数,1 表示消息可记录,0 表示不可记录。

__android_log_is_loggable_len

android/log.h 中声明
int __android_log_is_loggable_len(
  int prio,
  const char *tag,
  size_t len,
  int default_prio
)

使用每标签属性“log.tag。",以及 __android_log_set_minimum_priority() 设置的最低优先级,以确定是否将打印具有给定 prio 和 tag 的日志消息。

非零结果表示是,零表示否。

如果同时设置了标签优先级和 __android_log_set_minimum_priority() 设置的最低优先级,则取两个值中的较低者作为记录所需的最低优先级。如果只设置了一个,则使用该值确定所需的最低优先级。如果都没有设置,则使用 default_priority。

自 API 级别 30 起可用。

详细信息
参数
prio
要测试的优先级,取 android_LogPriority 值。
tag
要测试的标签。
len
标签的长度。
default_prio
如果未设置任何属性或最低优先级,则使用的默认优先级。
返回值
一个整数,1 表示消息可记录,0 表示不可记录。

__android_log_logd_logger

android/log.h 中声明
void __android_log_logd_logger(
  const struct __android_log_message *log_message
)

将日志消息写入 logd。

这是一个 __android_logger_function,可以提供给 __android_log_set_logger()。它是在设备上运行 liblog 时的默认日志记录器。

自 API 级别 30 起可用。

详细信息
参数
log_message
要写入的日志消息,请参阅 __android_log_message

__android_log_print

android/log.h 中声明
int __android_log_print(
  int prio,
  const char *tag,
  const char *fmt,
  ...
)

将格式化字符串写入日志,优先级为 prio,标签为 tag

格式化的详细信息与 printf(3) 相同。

详细信息
返回值
如果消息已写入日志,则返回 1;如果未写入,则返回 -EPERM;请参阅 __android_log_is_loggable()

__android_log_set_aborter

android/log.h 中声明
void __android_log_set_aborter(
  __android_aborter_function aborter
)

设置用户定义的中止函数,该函数在 __android_log_assert() 失败时调用。

强烈建议此用户定义的中止函数中止且不返回,但并非严格要求。

自 API 级别 30 起可用。

详细信息
参数
aborter
新的中止函数,请参阅 __android_aborter_function

__android_log_set_default_tag

android/log.h 中声明
void __android_log_set_default_tag(
  const char *tag
)

如果在写入日志消息时未提供标签,则设置默认标签。

默认为 getprogname()。这将把标签截断为最大日志消息大小,尽管合适的标签应该小得多。

自 API 级别 30 起可用。

详细信息
参数
tag
新的日志标签。

__android_log_set_logger

android/log.h 中声明
void __android_log_set_logger(
  __android_logger_function logger
)

设置用户定义的日志记录器函数。

发送到 liblog 的所有日志消息都将设置为 logger 指定的函数指针进行处理。不要求日志消息已由换行符终止。如果需要换行符进行行分隔,此函数应添加换行符。

自 API 级别 30 起可用。

详细信息
参数
logger
将处理日志消息的新函数。

__android_log_set_minimum_priority

android/log.h 中声明
int32_t __android_log_set_minimum_priority(
  int32_t priority
)

设置此进程将记录的最低优先级。

自 API 级别 30 起可用。

详细信息
参数
priority
要设置的新最低优先级,取 android_LogPriority 值。
返回值
先前设置的最低优先级,如果未设置,则为 ANDROID_LOG_DEFAULT

__android_log_stderr_logger

android/log.h 中声明
void __android_log_stderr_logger(
  const struct __android_log_message *log_message
)

将日志消息写入 stderr。

这是一个 __android_logger_function,可以提供给 __android_log_set_logger()。它是在主机上运行 liblog 时的默认日志记录器。

自 API 级别 30 起可用。

详细信息
参数
log_message
要写入的日志消息,请参阅 __android_log_message

__android_log_vprint

android/log.h 中声明
int __android_log_vprint(
  int prio,
  const char *tag,
  const char *fmt,
  va_list ap
)

等同于 __android_log_print(),但接受 va_list

(如果 __android_log_print() 像 printf(),这就像 vprintf()。)

详细信息
返回值
如果消息已写入日志,则返回 1;如果未写入,则返回 -EPERM;请参阅 __android_log_is_loggable()

__android_log_write

android/log.h 中声明
int __android_log_write(
  int prio,
  const char *tag,
  const char *text
)

将常量字符串 text 写入日志,优先级为 prio,标签为 tag

详细信息
返回值
如果消息已写入日志,则返回 1;如果未写入,则返回 -EPERM;请参阅 __android_log_is_loggable()

__android_log_write_log_message

android/log.h 中声明
void __android_log_write_log_message(
  struct __android_log_message *log_message
)

写入 log_message 指定的日志消息。

log_message 包含日志记录器可能使用的附加文件名和行号信息。log_message 已版本化以实现向后兼容性。这假定已通过 __android_log_is_loggable() 检查了可记录性。更高级别的日志记录库(例如 libbase)首先检查可记录性,然后格式化其缓冲区,再通过此函数将消息传递给 liblog,因此我们不希望在此处重复进行可记录性检查。

自 API 级别 30 起可用。

详细信息
参数
log_message
日志消息本身,请参阅 __android_log_message