mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
improve logging MT robustness
This commit is contained in:
@@ -34,7 +34,11 @@ void GwLog::logDebug(int level,const char *fmt,...){
|
||||
va_start(args,fmt);
|
||||
xSemaphoreTake(locker, portMAX_DELAY);
|
||||
vsnprintf(buffer,99,fmt,args);
|
||||
if (! writer) return;
|
||||
buffer[99]=0;
|
||||
if (! writer) {
|
||||
xSemaphoreGive(locker);
|
||||
return;
|
||||
}
|
||||
writer->write(prefix.c_str());
|
||||
writer->write(buffer);
|
||||
writer->write("\n");
|
||||
@@ -46,3 +50,13 @@ void GwLog::setWriter(GwLogWriter *writer){
|
||||
this->writer=writer;
|
||||
xSemaphoreGive(locker);
|
||||
}
|
||||
|
||||
void GwLog::flush(){
|
||||
xSemaphoreTake(locker, portMAX_DELAY);
|
||||
if (! this->writer) {
|
||||
xSemaphoreGive(locker);
|
||||
return;
|
||||
}
|
||||
this->writer->flush();
|
||||
xSemaphoreGive(locker);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ class GwLogWriter{
|
||||
public:
|
||||
virtual ~GwLogWriter(){}
|
||||
virtual void write(const char *data)=0;
|
||||
virtual void flush(){};
|
||||
};
|
||||
class GwLog{
|
||||
private:
|
||||
@@ -25,6 +26,7 @@ class GwLog{
|
||||
void logString(const char *fmt,...);
|
||||
void logDebug(int level, const char *fmt,...);
|
||||
int isActive(int level){return level <= logLevel;};
|
||||
void flush();
|
||||
};
|
||||
#define LOG_DEBUG(level,...){ if (logger != NULL && logger->isActive(level)) logger->logDebug(level,__VA_ARGS__);}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user