Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are google log entries wrapped by a mutex?

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.

like image 530
Wes Miller Avatar asked Jul 13 '12 19:07

Wes Miller


2 Answers

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.
like image 108
A S Avatar answered Nov 10 '22 06:11

A S


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

like image 3
A T Avatar answered Nov 10 '22 05:11

A T