plog
plog copied to clipboard
buffered asynchronous logging
is it possible to implement a buffer that accumulates logs and then writes to file ( either after N number of logs accumulated OR after certain elapse of timer ) using a thread asynchronously ? Could you please provide some insights on how / where in the code can this be achieved?
Of course it's possible. But you have to understand what and why you're doing.
Here are some points to consider:
- completely removing file operations from the Performance sample makes the code about 2 times faster (on the ordinary PC)
- accumulated logs will be lost in case of an application crash
- some platforms may have no threads
The best way is to make Record copyable and to create an asynchronous appender-wrapper that will accumulate records and send them to the real appender asynchronously:
class AsyncAppender : public IAppender
{
public:
AsyncAppender(IAppender* realAppender);
Probably easier but not so versatile approach is to create AsyncFile and use it instead of File in RollingFileAppender.
Just my 2Cents:
How about taking the first part of the title: Buffered
And adding that to the util::File as an optional parameter (of say 4k increments).
And then setting the buffer used by the wsopen handle if it is greater then 0 ?
Another few cents. For the Asynch part: How about just having the "write" part being sent to an async lambda ? (Like async writes in C# etc.) ? This way, the rest of the code can stay as it is, (i.e. no need to make copy constructors etc), and only the "Slow disk / Ip transport etc) stuff is asynch in "Maybe" a fire and forget fashion ?