Implements rudimentary thread-safety for EventBus and Logger classes.

This commit is contained in:
Sven Luethi committed 2021-04-01 00:17:38 +02:00
1 parent 34e1dd0c33
commit fb35fbd350
6 files changed
+66 -31

No files matched your search

+21 -12
View File
@@ -63,17 +63,19 @@ Logger::Sink::Sink (const Level level, Logger& parent) :
_lineIsPrefixed(false) {}
void Logger::Sink::ln ( const string& msg ) {
ln(msg.c_str());
ln (msg.c_str());
}
void Logger::Sink::ln ( const char* msg ) {
_parent._mutex.lock();
if ( _level >= _parent._logLevel ) {
// If line has already been started, abort it and start new one:
if(_lineIsPrefixed) *_parent._output << endl;
prefixLine(true);
*_parent._output << msg << endl;
*(_parent._output) << msg << endl;
cout << msg << CC_RESET "\n";
}
_parent._mutex.unlock();
}
void Logger::Sink::prefixLine(bool resetToFalse) {
@@ -82,19 +84,19 @@ void Logger::Sink::prefixLine(bool resetToFalse) {
switch ( _level)
{
case DEBUG:
*_parent._output << timestamp << " " << "DEBUG ";
*(_parent._output) << timestamp << " " << "DEBUG ";
cout << timestamp << " " << CC_BLUE "DEBUG ";
break;
case INFO:
*_parent._output << timestamp << " " << "INFO ";
*(_parent._output) << timestamp << " " << "INFO ";
cout << timestamp << " " << CC_GREEN "INFO ";
break;
case WARN:
*_parent._output << timestamp << " " << "WARN ";
*(_parent._output) << timestamp << " " << "WARN ";
cout << timestamp << " " << CC_YELLOW "WARN ";
break;
case FATAL:
*_parent._output << timestamp << " " << "FATAL ";
*(_parent._output) << timestamp << " " << "FATAL ";
cout << timestamp << " " << CC_RED "FATAL ";
break;
}
@@ -103,61 +105,68 @@ void Logger::Sink::prefixLine(bool resetToFalse) {
}
Logger::Sink& Logger::Sink::operator<<(const string& msg) {
_parent._mutex.try_lock();
if ( _level >= _parent._logLevel ) {
prefixLine();
*_parent._output << msg;
*(_parent._output) << msg;
cout << msg;
}
return *this;
}
Logger::Sink& Logger::Sink::operator<<(const char* msg) {
_parent._mutex.try_lock();
if ( _level >= _parent._logLevel ) {
prefixLine();
*_parent._output << msg;
*(_parent._output) << msg;
cout << msg;
}
return *this;
}
Logger::Sink& Logger::Sink::operator<<(const int msg) {
_parent._mutex.try_lock();
if ( _level >= _parent._logLevel ) {
prefixLine();
*_parent._output << msg;
*(_parent._output) << msg;
cout << msg;
}
return *this;
}
Logger::Sink& Logger::Sink::operator<<(const long msg) {
_parent._mutex.try_lock();
if ( _level >= _parent._logLevel ) {
prefixLine();
*_parent._output << msg;
*(_parent._output) << msg;
cout << msg;
}
return *this;
}
Logger::Sink& Logger::Sink::operator<<(const unsigned long msg) {
_parent._mutex.try_lock();
if ( _level >= _parent._logLevel ) {
prefixLine();
*_parent._output << msg;
*(_parent._output) << msg;
cout << msg;
}
return *this;
}
Logger::Sink& Logger::Sink::operator<<(const char character) {
_parent._mutex.try_lock();
if ( _level >= _parent._logLevel ) {
switch(character) {
// COMMIT - End message line
case 0:
*_parent._output << endl;
*(_parent._output) << endl;
cout << CC_RESET "\n";
_lineIsPrefixed = false;
break;
}
}
_parent._mutex.unlock();
return *this;
}