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_++] =