I was looking at glog‘s documentation and got confused at the way the stream operator is being used.
LOG(INFO) << "Found " << num_cookies << " cookies";
How can it deduce that all those calls are to form a single log message?
From my understanding, this:
cout << foo << bar << baz;
is equivalent to:
cout << foo;
cout << bar;
cout << baz;
How is it able to group together the chained calls like that without a special delimiter?
EDIT:
I know that you can chain them and why is works, that was not my question.
I’m asking how glog can take
LOG(INFO) << "Found " << num_cookies << " cookies";
what looks to me like 3 separate calls to the << overload and know that
"Found 3 cookies"
is a single log message as opposed to 3.
cout << fooreturnscoutSo it then becomes
cout << bar << baz;and so on.
Another way to see it clearer is
((cout.operator<<(foo)).operator<<(bar)).operator<<(baz);which is exactly what is happening
You can see this from http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
ostream& operator<< (double val);Edit
After the discussion, it appears that the question is more about how gLogs seperate each log into its own log entry without a terminator or any
endltype object.It appears from glancing over
logging.h, that each individual log might be written by a different stream object.This means that while using the same log object, you write to the same log entry. And every other call to Log actually creates a new stream object. RAII will take care of actually writing the finished log, as this is a temporary object and it is destroyed after the line of code is executed.
I would need to go deeper into the code to understand better but it looks like this is an accurate if brief answer. The question remains if (and if so how) this is optimized..