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=<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 <stdlib.h>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 <stdlib.h>
#include <log/log.h>//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.<TAG> <LEVEL>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

AndroidT(13) Log 系统 -- C 语言格式的LOG输出(一)相关推荐

  1. AndroidT(13) Log 系统 -- C plus plus 语言格式的LOG输出(二)

    1.概览   上一章提到的是在Android系统中,以C语言格式方式进行log输出.本章就来讲讲c++语言格式的. std::cout<<"This is a c++ log&q ...

  2. C语言-格式输入与输出

    格式输入与输出 前言 1.格式输出函数printf 2.格式输入函数scanf 3.字符输出函数putchar 4.字符输入函数getchar     前言:格式输入输出函数有 输入函数printf( ...

  3. 利用 CocoaLumberjack 搭建自己的 Log 系统

    2019独角兽企业重金招聘Python工程师标准>>> 先说下需求,我理想中的 Log 系统需要: 可以设定 Log 等级 可以积攒到一定量的 log 后,一次性发送给服务器,绝对不 ...

  4. AndroidT(13) Log 系统 -- logd 服务的初始化(七)

    1. 概览   经过上一章的分析,现在也是时候讨论下logd的初始化了,虽然 logd 在代码量上来说并不大,但是还是分模块进行分析比较合适.所以这里就不贴整体代码了,这部分代码也被包含在AOSP t ...

  5. AVI音视频封装格式学习(四)——linux系统C语言AVI格式音视频封装应用

    拖了很久的AVI音视频封装实例,花了一天时间终于调完了,兼容性不是太好,但作为参考学习使用应该没有问题.RIFF和AVI以及WAV格式,可以参考前面的一些文章.这里详细介绍将一个H264视频流和一个2 ...

  6. c语言保龄球计分系统课程设计,保龄球计分系统C语言课程设计毕业设计(论文)word格式.doc...

    保龄球计分系统C语言课程设计毕业设计(论文)word格式 目 录 1 1 1.1问题描述1 1.2输入数据要求1 1.3输出数据要求2 1.4开发环境和工具2 1.5成员分工2 2总体设计3 2.1总 ...

  7. linux 系统 Shell语言 基础

    linux 系统 Shell语言 基础 第一章 Shell 编程 1 . 概述 ​ Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Sh ...

  8. IOS 13 + 13.1 系统更新,你想知道的都在这里

    ​ 随着Iphone秋季发布会的召开,Iphone11手机和和IOS 13也随之发布,我们不提Iphone11发热严重,IOS13系统相机不能使用.无信号.相册闪退.多光标.三指游戏无法操作等等一系列 ...

  9. android log系统

    转载自http://blog.csdn.net/Luoshengyang/article/category/838604/3 Android系统开发中LOG的使用 在程序开发过程中,LOG是广泛使用的 ...

最新文章

  1. Windows10快捷应用指令
  2. objc@interface的设计哲学与设计技巧
  3. iOS面试题 第一天
  4. A different twist on pre-compiling JSPs--reference
  5. python变量无需创建赋值_Python 第 2 章 变量及赋值运算符
  6. python导入模块--案例
  7. mybatis 查询之神坑
  8. 产品总监的日常:管好团队必须先“正三观”
  9. 当我们写Controller时,VisitRefer注解是干什么的。
  10. VMwar配置静态ip
  11. Access数据库使用DateAdd函数更新日期信息
  12. 有哪些管理类书籍值得推荐?
  13. java实现 猜数字游戏
  14. Android进阶之路 - 毛玻璃遮罩层
  15. 【CCF会议期刊推荐】CCF推荐国际学术期刊/会议(计算机图形学与多媒体)
  16. SSL数字证书下载流程是怎么样的
  17. sql server 查看死锁,以及执行语句
  18. [LUOGU] P4363 [九省联考2018]一双木棋chess
  19. fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突——我的解决方案
  20. 机械键盘知多少:Mac党的福音键盘

热门文章

  1. Android——Xlistview上拉刷新下拉加载
  2. 织梦模板下载:政府供销社类部门织梦网站模板
  3. 第三方客户端登录QQ邮箱遇到“无法验证账户名或密码”问题解决
  4. c语言中字符串输入输出的几种方式
  5. 【转载】用Word编辑论文的几个建议
  6. unbound:安装与配置
  7. Debian用户获取ROOT权限与安装sudo配置
  8. 用友NC57移动审批、可集成钉钉和企业微信
  9. 电机学-交流绕组-基本要求
  10. 【数据结构】图的基本概念—无/有向图、权和网、完全图、路径与回路