make logging thread safe
This commit is contained in:
parent
180ca4b785
commit
99beb82014
|
@ -12,27 +12,37 @@ GwLog::GwLog(int level, GwLogWriter *writer){
|
|||
logLevel=level;
|
||||
if (writer == NULL) writer=new DefaultLogWriter();
|
||||
this->writer=writer;
|
||||
locker = xSemaphoreCreateMutex();
|
||||
}
|
||||
GwLog::~GwLog(){
|
||||
vSemaphoreDelete(locker);
|
||||
}
|
||||
void GwLog::logString(const char *fmt,...){
|
||||
if (! writer) return;
|
||||
va_list args;
|
||||
va_start(args,fmt);
|
||||
xSemaphoreTake(locker, portMAX_DELAY);
|
||||
vsnprintf(buffer,99,fmt,args);
|
||||
if (! writer) return;
|
||||
writer->write(prefix.c_str());
|
||||
writer->write(buffer);
|
||||
writer->write("\n");
|
||||
xSemaphoreGive(locker);
|
||||
}
|
||||
void GwLog::logDebug(int level,const char *fmt,...){
|
||||
if (level > logLevel) return;
|
||||
if (! writer) return;
|
||||
va_list args;
|
||||
va_start(args,fmt);
|
||||
xSemaphoreTake(locker, portMAX_DELAY);
|
||||
vsnprintf(buffer,99,fmt,args);
|
||||
if (! writer) return;
|
||||
writer->write(prefix.c_str());
|
||||
writer->write(buffer);
|
||||
writer->write("\n");
|
||||
xSemaphoreGive(locker);
|
||||
}
|
||||
void GwLog::setWriter(GwLogWriter *writer){
|
||||
xSemaphoreTake(locker, portMAX_DELAY);
|
||||
if (this->writer) delete this->writer;
|
||||
this->writer=writer;
|
||||
xSemaphoreGive(locker);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ class GwLog{
|
|||
char buffer[100];
|
||||
int logLevel=1;
|
||||
GwLogWriter *writer;
|
||||
SemaphoreHandle_t locker;
|
||||
public:
|
||||
static const int LOG=1;
|
||||
static const int ERROR=0;
|
||||
|
@ -19,6 +20,7 @@ class GwLog{
|
|||
static const int TRACE=2;
|
||||
String prefix="LOG:";
|
||||
GwLog(int level=LOG, GwLogWriter *writer=NULL);
|
||||
~GwLog();
|
||||
void setWriter(GwLogWriter *writer);
|
||||
void logString(const char *fmt,...);
|
||||
void logDebug(int level, const char *fmt,...);
|
||||
|
|
Loading…
Reference in New Issue