6

Using Google's logging library (glog-0.3.2), are the individual entries sent to the log wrapped by a mutex? That is, can other entries corrupt the entry currently being saved?

I guess that translates to: is glog threadsafe?

And if the logger is set to echo to console as well as to file, short of having my own mutex, is there any way to block printf/cout from corrupting the output from LOG()? I suspect not but wondered if perhaps there is a way to lock the mutex that can wrap several statements.

phoenix
  • 7,988
  • 6
  • 39
  • 45
Wes Miller
  • 2,191
  • 2
  • 38
  • 64

2 Answers2

17

Unfortunately, the "Raw Logging" info from the glog docs is misleading. raw_logging is a reduced-functionality logging mechanism in GLOG that is useful for situations that are prone to deadlock or other possible re-entrant situations.

If you look at logging.h and logging.c, you'll see that the normal, full-featured logging in glog is also thread-safe. This is achieved as follows:

  • glog goes out of its way to construct a new, unique log object for each LOG line, to prevent interleaving of output between two different threads (as can happen with fwrite() or write())
  • glog takes a mutex when it queues a completed log line to the log sink.
  • glog requires that log sinks are thread safe.
A S
  • 171
  • 1
  • 2
3

Yes, glog can be threadsafe.

Raw Logging

The header file can be used for thread-safe logging, which does not allocate any memory or acquire any locks. Therefore, the macros defined in this header file can be used by low-level memory allocation and synchronization code. Please check src/glog/raw_logging.h.in for detail.

http://google-glog.googlecode.com/svn/trunk/doc/glog.html

A T
  • 13,008
  • 21
  • 97
  • 158