在学习 chromium 源码时,我们经常需要增加调试日志,常见的用法一般是
// TurboNet.mm
133
134 LOG(INFO) << "TurboNet Engine started.";
135 LOG(INFO);
136 LOG(WARNING);
137 VLOG(1);
138 VLOG(2);
139 PLOG(INFO); // 会输出系统错误码信息
140 PLOG(WARNING);
141 DLOG(INFO);
142 DLOG(WARNING);
日志输出效果如下:
其中 INFO 代表当前这条日志的级别(详见下面 2 小节),使用的时候就是输入 INFO 就行。接下来我们在探索下这个宏背后的内容。 LOG 在 release 和 debug 模式均生效,DLOG 仅在 debug 模式生效。VLOG 支持模式匹配,比如指定特定文件的日志级别、特定目录的日志级别、特定前缀的文件的日志级别等,详见官方注释:
// There are "verbose level" logging macros. They look like
//
// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
//
// These always log at the INFO log level (when they log at all).
// The verbose logging can also be turned on module-by-module. For instance,
// --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
// will cause:
// a. VLOG(2) and lower messages to be printed from profile.{h,cc}
// b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
// c. VLOG(3) and lower messages to be printed from files prefixed with
// "browser"
// d. VLOG(4) and lower messages to be printed from files under a
// "chromeos" directory.
// e. VLOG(0) and lower messages to be printed from elsewhere
一、基本用法
LOG 本身是一个宏:
#define LOG(severity) LAZY_STREAM