glog 使用: 03-条件日志(Conditional / Occasional Logging) 与 Debug模式日志 (Debug Mode Support)

条件输出

glog定义了一系列具有逻辑控制条件的日志函数来控制日志输出。常用的几个如下:

  • 条件输出
int num_cookies=11;  
LOG_IF(INFO, num_cookies>10) <<"Got lots of cookies";
  • 间隔输出
for(int i = 0; i < 100; i++)
{
    //在google::COUNTER 等于 1、11、21... 时输出
    LOG_EVERY_N(INFO, 10) << "LOG_EVERY_N " << google::COUNTER << "th";
}
//另一个间隔输出,google::COUNTER 已重置0
LOG_EVERY_N(INFO, 10) << "another LOG_EVERY_N " << google::COUNTER << "th";
  • 条件间隔输出
for(int i = 0; i <= 100; i++)
{
    //在条件为真时,做一次计数,然后到达指定间隔时输出。
    //在google::COUNTER 等于 0、100 时输出
    LOG_IF_EVERY_N(INFO, i % 10 == 0, 10) << "LOG_IF_EVERY_N " << google::COUNTER << "th";
}
//这个 google::COUNTER 还是有点意思的,每个LOG输出语句都有一个自己的COUNTER,从0开始计数
LOG(INFO) << "LOG " << google::COUNTER << "th";
  • 输出前N条日志
for(int i = 0; i < 100; i++)
{
    //输出前 10 条日志
    LOG_FIRST_N(INFO, 10) << "LOG_FIRST_N " << google::COUNTER << "th";
}
  • 基于时间间隔的输出。

不是严格的定时输出,而是一种超时比较的机制进行采样输出。

for(int i = 0; i < 100; i++)
{
    //单位是秒
    //1 us 的间隔输出,粒度很小,所有i值都打印了。
    //这里也可以观察到,日志打印太频繁也影响了计数循环的性能
    //LOG_EVERY_T(INFO, 0.000001) << "LOG_EVERY_T 1 us,i:" << i ;
    
    //5 us 间隔较大,i值 随机跳跃打印
    LOG_EVERY_T(INFO, 0.000005) << "LOG_EVERY_T 5 us,i:" << i ;
}

调试模式支持

日志输出本身是对性能有所损耗的,可以控制仅在 debug 模式输出影响性能的日志。具体方式是使用 D 前缀的相关日志函数。

涉及两个宏定义 NDEBUG 和 DCHECK_ALWAYS_ON
需要在 #include <glog/logging.h> 前添加宏定义,或者在编译时指定 -DNDEBUG
根据 logging.h line 538 :#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
若定义 NDEBUG 并且没有定义 DCHECK_ALWAYS_ON,则为非 debug模式,D开头的方法将失效
若没有定义 NDEBUG 或者 定义 NDEBUG 但是也定义 DCHECK_ALWAYS_ON ,则为 debug模式,D开头的方法将生效

//控制标识设置 非debug模式
#define NDEBUG
#include <glog/logging.h>

void function()
{
    //此时不会输出
    DLOG(INFO) << "DLOG Found cookies";
    DLOG_IF(INFO, num_cookies > 10) << "DLOG_IF Got lots of cookies";
    DLOG_EVERY_N(INFO, 10) << "DLOG_EVERY_N Got the " << google::COUNTER << "th cookie";

}
/Users/dten/code/glog-ci-macos-15-runner/src/glog/types.h:40:15: warning: alias declarations are a C++11 extension [-Wc++11-extensions] using int32 = std::int32_t; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/types.h:41:16: warning: alias declarations are a C++11 extension [-Wc++11-extensions] using uint32 = std::uint32_t; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/types.h:42:15: warning: alias declarations are a C++11 extension [-Wc++11-extensions] using int64 = std::int64_t; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/types.h:43:16: warning: alias declarations are a C++11 extension [-Wc++11-extensions] using uint64 = std::uint64_t; ^ In file included from /Users/dten/code/glog-ci-macos-15-runner/src/logging_unittest.cc:61: In file included from /Users/dten/code/glog-ci-macos-15-runner/src/base/commandlineflags.h:63: In file included from /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:77: /Users/dten/code/glog-ci-macos-15-runner/src/glog/log_severity.h:89:1: error: unknown type name 'constexpr' constexpr int NUM_SEVERITIES = 4; ^ In file included from /Users/dten/code/glog-ci-macos-15-runner/src/logging_unittest.cc:61: In file included from /Users/dten/code/glog-ci-macos-15-runner/src/base/commandlineflags.h:63: /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:86:60: error: expected ';' at end of declaration list const std::chrono::system_clock::time_point& when() const noexcept { ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:473:33: warning: alias declarations are a C++11 extension [-Wc++11-extensions] using PrefixFormatterCallback = void (*)(std::ostream&, const LogMessage&, ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:563:3: warning: explicit conversion functions are a C++11 extension [-Wc++11-extensions] explicit operator bool() const noexcept { ^~~~~~~~ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:563:33: error: expected ';' at end of declaration list explicit operator bool() const noexcept { ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:562:53: error: member initializer 'str_' does not name a non-static data member or base class CheckOpString(std::unique_ptr<std::string> str) : str_(std::move(str)) {} ^~~~~~~~~~~~~~~~~~~~ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:713:23: warning: alias declarations are a C++11 extension [-Wc++11-extensions] using _Check_string = std::string; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:983:22: error: unknown type name 'constexpr' GLOG_INLINE_VARIABLE constexpr Counter_t COUNTER{}; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:983:41: error: expected ';' after top level declarator GLOG_INLINE_VARIABLE constexpr Counter_t COUNTER{}; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1210:24: warning: rvalue references are a C++11 extension [-Wc++11-extensions] LogStream(LogStream&& other) noexcept ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1210:33: error: expected ';' at end of declaration list LogStream(LogStream&& other) noexcept ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1235:46: warning: deleted function definitions are a C++11 extension [-Wc++11-extensions] LogStream& operator=(const LogStream&) = delete; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1289:16: error: expected ';' at end of declaration list ~LogMessage() noexcept(false); ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1306:4: error: expected expression [[noreturn]] static void Fail(); ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1306:16: error: expected member name or ';' after declaration specifiers [[noreturn]] static void Fail(); ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1315:31: error: expected ';' at end of declaration list LogSeverity severity() const noexcept; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1316:19: error: expected ';' at end of declaration list int line() const noexcept; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1317:43: error: expected ';' at end of declaration list const std::thread::id& thread_id() const noexcept; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1318:31: error: expected ';' at end of declaration list const char* fullname() const noexcept; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1319:31: error: expected ';' at end of declaration list const char* basename() const noexcept; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1320:37: error: expected ';' at end of declaration list const LogMessageTime& time() const noexcept; ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1322:35: warning: deleted function definitions are a C++11 extension [-Wc++11-extensions] LogMessage(const LogMessage&) = delete; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1323:46: warning: deleted function definitions are a C++11 extension [-Wc++11-extensions] LogMessage& operator=(const LogMessage&) = delete; ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1361:4: error: expected expression [[noreturn]] ~LogMessageFatal() noexcept(false); ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1361:34: error: expected ';' at end of declaration list [[noreturn]] ~LogMessageFatal() noexcept(false); ^ ; /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1412:64: warning: rvalue references are a C++11 extension [-Wc++11-extensions] T CheckNotNull(const char* file, int line, const char* names, T&& t) { ^ /Users/dten/code/glog-ci-macos-15-runner/src/glog/logging.h:1414:38: error: no member named 'make_unique' in namespace 'std' LogMessageFatal(file, line, std::make_unique<std::string>(names)); ~~~~~^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 12 warnings and 20 errors generated. make[2]: *** [CMakeFiles/logging_unittest.dir/src/logging_unittest.cc.o] Error 1 make[1]: *** [CMakeFiles/logging_unittest.dir/all] Error 2 make: *** [all] Error 2 我编译glog报错
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值