LoggerCpp
LoggerCpp copied to clipboard
Not thread safe
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
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();
> }
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)
...
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