AndroidT(13) -- C 语言格式的LOG输出(一)
创始人
2024-02-24 20:09:25
0

1.概览

  c语言中的printf我想大家都非常的熟悉了,他的基本格式如下

int printf(const char *format, ...);

  前半部分 format 指向的字符串用于描述最终输出内容的格式,而后半部分则是被前面 format 所解析的各种变量/常量,这两部分则是构成输出的内容。而Android中也有类似的内容输出函数,用法和printf也是类似的及前半部为格式,后半部为各种变量/常量。

ALOGI(const char *format, ...);

  printf打印内容到标准输出,而ALOG系列接口则是打印内容到Android的log系统中去。

2.打印LOG内容所属的类型

  在 Android 中,有着非常多的子系统,所以 Android 的 ALOG 系统是支持多个buffer的,即不同类型的log写入不同的buffer中。它所支持的log类型可以简单的使用对应版本的 logcat 工具查看

board:/ # logcat -h
Usage: logcat [options] [filterspecs]General options:-b, --buffer=       Request alternate ring buffer(s):main system radio events crash default allAdditionally, 'kernel' for userdebug and eng builds, and'security' for Device Owner installations.Multiple -b parameters or comma separated list of buffers areallowed. Buffers are interleaved.Default -b main,system,crash,kernel.

  在c语言风格的log接口中大致支持3种buffer,它们分别是 main/system/radio ,在使用的时候只要包含 liblog::log/log.h 即可

2.1 main 类型

  main类型的的buffer用来打印一般的log信息,注意了,虽然是一般,但是也不要打印垃圾内容,毕竟log系统的buffer是很紧俏的。

//system\logging\liblog\include\log\log_main.h
#define ALOGI(const char *format, ...)

2.2 system 类型

  该类型的 buffer 用来打印系统组件相关的log。同样的不要打印垃圾信息到这一类型的buffer中去。

//system\logging\liblog\include\log\log_system.h
#define SLOGI(const char *format, ...)

2.3 radio 类型

  该类型的 buffer 用来打印电话相关的组件的log。同样的不要打印垃圾信息到这一类型的buffer中去。

//system\logging\liblog\include\log\log_radio.h
#define RLOGI(const char *format, ...)

3.打印内容所属的级别

  手头需要做的事有轻重缓急,log中的信息所代表的事项也同样如此。下面是Android log 系统的几个级别

//file:system\logging\liblog\include\android\log.h
/*** Android log priority values, in increasing order of priority.*/
typedef enum android_LogPriority {.../** 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,...
} android_LogPriority;

  每一个级别的使用场景在代码注释中说的也算详细,此处就不再赘述。其中 ANDROID_LOG_FATAL 级别的LOG最终会调用 abort,abort则会造成应用程序的意外退出。如果在非系统应用中则会导致应用本身退出。如果是系统级别的那么就有可能导致Android系统奔溃了

NAMEabort - cause abnormal process terminationSYNOPSIS#include void abort(void);DESCRIPTIONThe  abort() first unblocks the SIGABRT signal, and then raises that signal for the calling process (as though raise(3) was called).  This results inthe abnormal termination of the process unless the SIGABRT signal is caught and the signal handler does not return (see longjmp(3)).If the abort() function causes process termination, all open streams are closed and flushed.If the SIGABRT signal is ignored, or caught by a handler that returns, the abort() function will still  terminate  the  process.   It  does  this  byrestoring the default disposition for SIGABRT and then raising the signal for a second time.RETURN VALUEThe abort() function never returns.

3.1 VERBOSE 级别

//system\logging\liblog\include\log\log_main.h
#define ALOGV(const char *format, ...)
//system\logging\liblog\include\log\log_radio.h
#define RLOGV(const char *format, ...)
//system\logging\liblog\include\log\log_system.h
#define SLOGV(const char *format, ...)

3.2 DEBUG 级别

//system\logging\liblog\include\log\log_main.h
#define ALOGD(const char *format, ...)
//system\logging\liblog\include\log\log_radio.h
#define RLOGD(const char *format, ...)
//system\logging\liblog\include\log\log_system.h
#define SLOGD(const char *format, ...)

3.3 INFO 级别

//system\logging\liblog\include\log\log_main.h
#define ALOGI(const char *format, ...)
//system\logging\liblog\include\log\log_radio.h
#define RLOGI(const char *format, ...)
//system\logging\liblog\include\log\log_system.h
#define SLOGI(const char *format, ...)

3.4 WARN 级别

//system\logging\liblog\include\log\log_main.h
#define ALOGW(const char *format, ...)
//system\logging\liblog\include\log\log_radio.h
#define RLOGW(const char *format, ...)
//system\logging\liblog\include\log\log_system.h
#define SLOGW(const char *format, ...)

3.5 ERROR 级别

//system\logging\liblog\include\log\log_main.h
#define ALOGE(const char *format, ...)
//system\logging\liblog\include\log\log_radio.h
#define RLOGE(const char *format, ...)
//system\logging\liblog\include\log\log_system.h
#define SLOGE(const char *format, ...)

3.6 FATAL 级别

//system\logging\liblog\include\log\log_main.h
ALOG(LOG_FATAL, LOG_TAG, const char *format, ...);

4.使用示例 – ALOGtest

4.1 编译配置 – Android.bp

cc_binary {name: "ALOGtest",srcs: ["*.cpp",],shared_libs: ["liblog",],cppflags: ["-Wno-unused-parameter",],
}

  C语言风格的log打印接口,只使用了库 liblog,所以只要包含它即可。

4.2 测试源码 – ALOGtest.cpp

//ALOGtest.cpp#define LOG_TAG     "ALOGtest"#include 
#include //from liblog/*** board:/ # logcat -b all -s ALOGtest&* board:/ # ALOGtest* board:/ # --------- beginning of main* 11-27 14:40:09.361  5863  5863 I ALOGtest: ALOGI:write INFO level to main buffer* 11-27 14:40:09.362  5863  5863 I ALOGtest: ALOGI_IF:write INFO level to main buffer* --------- beginning of radio* 11-27 14:40:09.362  5863  5863 I ALOGtest: RLOGI:write INFO level to radio buffer* 11-27 14:40:09.362  5863  5863 I ALOGtest: RLOGI_IF:write INFO level to radio buffer* --------- beginning of system* 11-27 14:40:09.362  5863  5863 I ALOGtest: SLOGI:write INFO level to sytem buffer* 11-27 14:40:09.362  5863  5863 I ALOGtest: SLOGI_IF:write INFO level to sytem buffer
*/int main(int argc, char* argv[])
{/*logcat -b main -s ALOGtest&*/ALOGI("ALOGI:write INFO level to main buffer\n");ALOGI_IF((3 > 2), "ALOGI_IF:write INFO level to main buffer\n");/*logcat -b radio -s ALOGtest&*/RLOGI("RLOGI:write INFO level to radio buffer\n");RLOGI_IF((3 > 2), "RLOGI_IF:write INFO level to radio buffer\n");/*logcat -b system -s ALOGtest&*/SLOGI("SLOGI:write INFO level to sytem buffer\n");SLOGI_IF((3 > 2), "SLOGI_IF:write INFO level to sytem buffer\n");/*** board:/ # ALOGtest* 11-27 16:04:54.597  5883  5883 I ALOGtest: ALOGI:write INFO level to main buffer* 11-27 16:04:54.597  5883  5883 I ALOGtest: ALOGI_IF:write INFO level to main buffer* 11-27 16:04:54.597  5883  5883 I ALOGtest: RLOGI:write INFO level to radio buffer* 11-27 16:04:54.597  5883  5883 I ALOGtest: RLOGI_IF:write INFO level to radio buffer* 11-27 16:04:54.597  5883  5883 I ALOGtest: SLOGI:write INFO level to sytem buffer* 11-27 16:04:54.598  5883  5883 I ALOGtest: SLOGI_IF:write INFO level to sytem buffer* 11-27 16:04:54.598  5883  5883 F ALOGtest: ALOG:LOG_FATAL:write LOG_FATAL level to main buffer*/ALOG(LOG_FATAL, LOG_TAG, "ALOG:LOG_FATAL:write LOG_FATAL level to main buffer\n");return EXIT_SUCCESS;
}

  LOG_TAG需要在log/log.h前定义。
  所有支持的接口都位于如下接口

file:system\logging\liblog\include\log\log.h

5.运行结果

board:/ # ALOGtest
11-27 16:15:05.936  5885  5885 I ALOGtest: ALOGI:write INFO level to main buffer
11-27 16:15:05.937  5885  5885 I ALOGtest: ALOGI_IF:write INFO level to main buffer
11-27 16:15:05.937  5885  5885 I ALOGtest: RLOGI:write INFO level to radio buffer
11-27 16:15:05.937  5885  5885 I ALOGtest: RLOGI_IF:write INFO level to radio buffer
11-27 16:15:05.937  5885  5885 I ALOGtest: SLOGI:write INFO level to sytem buffer
11-27 16:15:05.938  5885  5885 I ALOGtest: SLOGI_IF:write INFO level to sytem buffer
11-27 16:15:05.938  5885  5885 F ALOGtest: ALOG:LOG_FATAL:write LOG_FATAL level to main buffer

6.使用属性控制log级别输出

  Android log子系统也提供了通过属性来控制log级别的输出,格式如下

adb shell setprop log.tag. LEVEL:VDIWEF

  其中的TAG就是我们代码中设置的LOG_TAG

board:/ # setprop log.tag.ALOGtest F
board:/ # ALOGtest
11-27 16:18:10.801  5888  5888 F ALOGtest: ALOG:LOG_FATAL:write LOG_FATAL level to main buffer
board:/ # setprop log.tag.ALOGtest V
board:/ # ALOGtest
11-27 16:22:18.811  5891  5891 I ALOGtest: ALOGI:write INFO level to main buffer
11-27 16:22:18.812  5891  5891 I ALOGtest: ALOGI_IF:write INFO level to main buffer
11-27 16:22:18.812  5891  5891 I ALOGtest: RLOGI:write INFO level to radio buffer
11-27 16:22:18.812  5891  5891 I ALOGtest: RLOGI_IF:write INFO level to radio buffer
11-27 16:22:18.812  5891  5891 I ALOGtest: SLOGI:write INFO level to sytem buffer
11-27 16:22:18.812  5891  5891 I ALOGtest: SLOGI_IF:write INFO level to sytem buffer
11-27 16:22:18.812  5891  5891 F ALOGtest: ALOG:LOG_FATAL:write LOG_FATAL level to main buffer

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...