c++glog

How does glog LOG(INFO) << "something" work?


I was trying to source debug (only have windows) glog code. One part which i am unable to unearth was how does

LOG(INFO) << "something" 

move the string into the buffer?

From what i could make out it is,

LogMessageData::message_text_

which needs to be populated with the log message. But i could not find out how the message gets pushed into it.


Solution

  • Basically LOG(INFO) is returning you a reference to std::ostream; to which you are writing your string. Quick look at the glog code shows how it is implemented.

    In src/windows/glog/logging.h - you would see LOG() is a Macro, acccepting severity as a parameter. This one just appends INFO to COMPACT_GOOGLE_LOG_ and calls stream() of it...

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

    the same file has definition of COMPACT_GOOGLE_LOG_INFO and it shows this is nothing but a function call - google::LogMessage().stream();

    398 #if GOOGLE_STRIP_LOG == 0
    399 #define COMPACT_GOOGLE_LOG_INFO google::LogMessage( \                                                                                                                                                            400       __FILE__, __LINE__)
    401 #define LOG_TO_STRING_INFO(message) google::LogMessage( \
    402       __FILE__, __LINE__, google::GLOG_INFO, message)
    403 #else
    404 #define COMPACT_GOOGLE_LOG_INFO google::NullStream()
    405 #define LOG_TO_STRING_INFO(message) google::NullStream()
    406 #endif
    

    Now, look at the src/logging.cc file for LogMessage's stream function.. This is returning you a reference to ostream to which you are redirecting your string using <<

    LOG(INFO)<<"something";

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

    here data_->stream_ is an object of LogStream which is derived from std::ostream.