Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous thread-safe logging in C++

I'm looking for a way to do asynchronous and thread-safe logging in my C++ project, if possible to one file. I'm currently using cerr and clog for the task, but since they are synchronous, execution shortly pauses every time something is logged. It's a relatively graphics-heavy app, so this kind of thing is quite annoying.

The new logger should use asynchronous I/O to get rid of these pauses. Thread-safety would also be desirable as I intend to add some basic multithreading soon.

I considered a one-file-per-thread approach, but that seemed like it would make managing the logs a nightmare. Any suggestions?

like image 521
Electro Avatar asked May 28 '10 15:05

Electro


1 Answers

I noticed this 1 year+ old thread. Maybe the asynchronous logger I wrote could be of interest.

http://www.codeproject.com/KB/library/g2log.aspx

G2log uses a protected message queue to forward log entries to a background worker that the slow disk accesses.

I have tried it with a lock-free queue which increased the average time for a LOG call but decreased the worst case time, however I am using the protected queue now as it is cross-platform. It's tested on Windows/Visual Studio 2010 and Ubuntu 11.10/gcc4.6.

It's released as public domain so you can do with it what you want with no strings attached.

like image 136
Kjell Hedström Avatar answered Sep 20 '22 17:09

Kjell Hedström