GLOG不初始化同样会输出到stderr以及LOG(INFO)解析

博客详细探讨了GLOG的工作原理,即使未经初始化也会默认输出到stderr。通过分析源码,解释了LogMessage类在日志析构时如何触发写入。还介绍了Caffe项目中如何在不同exe文件中通过InitGoogleLogging()初始化GLOG,以及未初始化时日志行为。
摘要由CSDN通过智能技术生成
glog的LOG(INFO)<<"xx";到底是怎样执行的?

1)源码

#define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()

可知
LOG(INFO) == COMPACT_GOOGLE_LOG_ INFO.stream()
2)源码

#if GOOGLE_STRIP_LOG == 0
#define COMPACT_GOOGLE_LOG_INFO google::LogMessage( \
      __FILE__, __LINE__)
#define LOG_TO_STRING_INFO(message) google::LogMessage( \
      __FILE__, __LINE__, google::GLOG_INFO, message)
#else

可知
LOG(INFO) == google::LogMessage( __FILE__, __LINE__).stream()
3)源码

ostream& LogMessage::stream() {
   
  return data_->stream_;
}

可知,LOG(INFO)<<"data..."将要写得信息data…插入到data_->stream_流里面。
4)源码

LogMessage::~LogMessage() {
   
  Flush();
  delete allocated_;
}

每次写日志得时候,创建一个LogMessage类,什么时候写呐,就是当LogMessage析构得时候,调用Flush()写。
5)源码

void LogMessage::Flush() {
   
  if (data_->has_been_flushed_ || data_->severity_ < FLAGS_minloglevel)
    return;

  data_->num_chars_to_log_ = data_->stream_.pcount();
  data_->num_chars_to_syslog_ =
    data_->num_chars_to_log_ - data_->num_prefix_chars_;

  // Do we need to add a \n to the end of this message?
  bool append_newline =
      (data_->message_text_[data_->num_chars_to_log_-1] != '\n');
  char original_final_char = '\0';

  // If we do need to add a \n, we'll do it by violating the memory of the
  // ostrstream buffer.  This is quick, and we'll make sure to undo our
  // modification before anything else is done with the ostrstream.  It
  // would be preferable not to do things this way, but it seems to be
  // the best way to deal with this.
  if (append_newline) {
   
    original_final_char = data_->message_text_[data_->num_chars_to_log_];
    data_->message_text_[data_->num_chars_to_log_++] = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值