plog icon indicating copy to clipboard operation
plog copied to clipboard

buffered asynchronous logging

Open iamshastry opened this issue 3 years ago • 3 comments

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?

iamshastry avatar May 08 '22 05:05 iamshastry

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.

SergiusTheBest avatar May 08 '22 07:05 SergiusTheBest

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 ?

Smurf-IV avatar Aug 15 '23 15:08 Smurf-IV

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 ?

Smurf-IV avatar Aug 16 '23 06:08 Smurf-IV