views:

71

answers:

4

I have to implement a threadsafe logging mechanism in C++ under Windows and I want to use the streams library.

I wanted to know if anyone could give me some advice regarding doing this and what would be the best way?

The logging messages will most likely be a few lines long each time, so I'd probably need to write them to a buffer first and then flush to file?

This logging should be fairly fast and not affect the performance of the application too much when its used, so I was thinking of doing the actual writing to the file in its own thread. Is that a good idea?

Any advice and hints are appreciated.

+4  A: 

Rather than write your own, why not use an existing one:

pantheios

Boost

Mitch Wheat
Two other existing libraries: log4cpp (http://log4cpp.sourceforge.net/) and log4cxx (http://logging.apache.org/log4cxx/index.html).
Bart van Ingen Schenau
@Bart van Ingen Schenau: I've seen mention that those 2 might leak memory....
Mitch Wheat
http://www.templog.org/
sbi
@Bart: Pantheios allows to use log4cpp as a backend.
Matthieu M.
+1  A: 

Yes - write to temporary std::ostringstream objects first then flush that out to the log ofstream. It's not just to avoid holding the lock too long, it's so that the stream manipulators from different threads aren't affecting each other, and so that individual calls to operator<< don't intersperse their output in the log. The temporary ostringstream lets you deal in complete, meaningful, multiline messages and make sure they're written in one piece.

I wouldn't bother with a dedicated thread until you've profiled it and seen if it's an issue. Simple is good.

(A common performance mistake: streaming the log message into an object before deciding if the logging level requires that message to be logged. To avoid this, use preprocessor macros to wrap an if around the streaming.)

Tony
A: 

Next to Mitch's advice to check frameworks:
Streaming to a buffer and sending the buffer to a thread is a realistic approach. If you do conditional logging (based on severity-levels), macro's can help avoid streaming when not needed (they can also help with meta-data: file,line,function).
Remember that critical logging will be lost when crashing if you log these in a seperate thread too.

stefaanv
A: 

One option is to place your logging code in a separate app or thread with a hidden window and to use PostMessage to send your logging data.

ChrisBD