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:
@@ -28,6 +28,7 @@ void handleLeds(void *param){
|
||||
GwLog *logger=api->getLogger();
|
||||
#ifndef GWLED_FASTLED
|
||||
LOG_DEBUG(GwLog::LOG,"currently only fastled handling");
|
||||
delay(50);
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
#else
|
||||
|
||||
@@ -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__);}
|
||||
|
||||
|
||||
@@ -46,23 +46,24 @@ int GwSerial::setup(int baud, int rxpin, int txpin)
|
||||
return 0;
|
||||
}
|
||||
bool GwSerial::isInitialized() { return initialized; }
|
||||
size_t GwSerial::enqueue(const uint8_t *data, size_t len)
|
||||
size_t GwSerial::enqueue(const uint8_t *data, size_t len, bool partial)
|
||||
{
|
||||
if (! isInitialized()) return 0;
|
||||
return buffer->addData(data, len);
|
||||
return buffer->addData(data, len,partial);
|
||||
}
|
||||
GwBuffer::WriteStatus GwSerial::write(){
|
||||
if (! isInitialized()) return GwBuffer::ERROR;
|
||||
return buffer->fetchData(writer,-1,false);
|
||||
}
|
||||
void GwSerial::sendToClients(const char *buf,int sourceId){
|
||||
if ( sourceId == id) return;
|
||||
size_t GwSerial::sendToClients(const char *buf,int sourceId,bool partial){
|
||||
if ( sourceId == id) return 0;
|
||||
size_t len=strlen(buf);
|
||||
size_t enqueued=enqueue((const uint8_t*)buf,len);
|
||||
if (enqueued != len){
|
||||
size_t enqueued=enqueue((const uint8_t*)buf,len,partial);
|
||||
if (enqueued != len && ! partial){
|
||||
LOG_DEBUG(GwLog::DEBUG,"GwSerial overflow on channel %d",id);
|
||||
overflows++;
|
||||
}
|
||||
return enqueued;
|
||||
}
|
||||
void GwSerial::loop(bool handleRead){
|
||||
write();
|
||||
|
||||
@@ -16,7 +16,7 @@ class GwSerial{
|
||||
GwBuffer::WriteStatus write();
|
||||
int id=-1;
|
||||
int overflows=0;
|
||||
size_t enqueue(const uint8_t *data, size_t len);
|
||||
size_t enqueue(const uint8_t *data, size_t len,bool partial=false);
|
||||
HardwareSerial *serial;
|
||||
public:
|
||||
static const int bufferSize=200;
|
||||
@@ -24,7 +24,7 @@ class GwSerial{
|
||||
~GwSerial();
|
||||
int setup(int baud,int rxpin,int txpin);
|
||||
bool isInitialized();
|
||||
void sendToClients(const char *buf,int sourceId);
|
||||
size_t sendToClients(const char *buf,int sourceId,bool partial=false);
|
||||
void loop(bool handleRead=true);
|
||||
bool readMessages(GwBufferWriter *writer);
|
||||
void flush();
|
||||
|
||||
Reference in New Issue
Block a user