2

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.

Bktero
  • 722
  • 5
  • 15
Shenron
  • 37
  • 1
  • 4
  • You could look for the `LOG` and `INFO` symbols and see how they are defined and what they do. And use a debugger to step into the `LOG` "call" to see what it does as well. – Some programmer dude Dec 02 '19 at 07:56
  • At a guess it creates an object with an overloaded stream operator that streams the log message into whatever log destination you have configured – Alan Birtles Dec 02 '19 at 08:16
  • Look for `COMPACT_GOOGLE_LOG_INFO` (or other severity), it is basically alias for `google::LogMessage` or similar – pptaszni Dec 02 '19 at 08:23

2 Answers2

3

If you look in to the LOG(INFO) macro, it will expand to a c++ code like std::cout on pre-processing stage (before the compilation of code). You will have to set a compiler flag to generate macro expanded file to see substituted codes lines. (This is depend on the compiler you are using. Just search on the internet)

So,

LOG(INFO) << "something"

becomes some code like

std::cout << "something"

Note that in above example std::cout is used to elaborate what is going under the hood.In you source code there might be different code instead of std::cout

Rest is up to the overloading of operator <<. Which used as insertion operator in loggers and output streams. Internally this operator is overloaded insert given data to logger or any other output stream etc;

hasi90
  • 84
  • 8
3

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.

Shrikant
  • 744
  • 8
  • 18