LoggerCpp icon indicating copy to clipboard operation
LoggerCpp copied to clipboard

Not thread safe

Open SRombauts opened this issue 8 years ago • 3 comments

Unfortunately this library is in fact not thread safe: I am using fprintf() for file and console output, which is thread safe on POSIX systems, but I also added features like file rotation or console color, which are not thread safe. => it would need to add some locking (critical section or mutex...) into the low level "output()" methods

SRombauts avatar Aug 31 '16 18:08 SRombauts

may be something like

> void OutputFile::output(const Channel::Ptr& aChannelPtr, const Log& aLog) const {
>   aLog.getTheMutex().Lock();
>       if (nullptr != mpFile) {
>         // uses fprintf for atomic thread-safe operation
>         int nbWritten = fprintf(mpFile, "[%.4u-%.2u-%.2u %.2u:%.2u:%.2u.%.3u]  %-12s %s %s\n",
>                                 time.year, time.month, time.day,
>                                 time.hour, time.minute, time.second, time.ms,
>                                 aChannelPtr->getName().c_str(), 
>                               Log::toString(aLog.getSeverity()),
>                                 (aLog.getStream()).str().c_str());
>         fflush(stdout);
> 
>         mSize += nbWritten;
>     }
>   aLog.getTheMutex().Unlock(); 
> }

abhit011 avatar Sep 04 '16 08:09 abhit011

Well, yes, but even better with RAII "scoped lock" with something more like this:

{
  ScopedLock Lock(aLog.getTheMutex()); // Create a temporary variable Locking the mutex in its constructor
  ...
} // Destructor of the ScopedLock release the mutex (when exiting, but also in case of an exception)
...

SRombauts avatar Sep 05 '16 07:09 SRombauts

Hey @SRombauts is it possible to do something like , rotate a new file, once it crosses "max_size" config ?. So that once the log file exceeds max_size , it will roll out a new file.. I feel this feature will be benefical

Abhi

abhit011 avatar Mar 10 '17 03:03 abhit011