From 6bbe4d7eaf0ac76b53642a6c902620462383db1b Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 3 Nov 2020 17:30:01 +0100 Subject: [PATCH] color refactoring --- cube/User/Inc/logger.h | 1 + cube/User/Src/logger.c | 37 ++++++++++++++++++++++++++++++------- cube/User/Src/mbusComm.c | 17 ++++++++--------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/cube/User/Inc/logger.h b/cube/User/Inc/logger.h index 48dfa41..85ed4af 100644 --- a/cube/User/Inc/logger.h +++ b/cube/User/Inc/logger.h @@ -12,6 +12,7 @@ void logFree(); // log a message, make sure it is a null-terminated string // return value can be ignored, it is only used in test int logMsg(const char *format, ...); +int errMsg(const char *format, ...); // reads the ringbuffer and transfers data to output channel // call this from the idle-loop diff --git a/cube/User/Src/logger.c b/cube/User/Src/logger.c index 4f41092..9ee1f7d 100644 --- a/cube/User/Src/logger.c +++ b/cube/User/Src/logger.c @@ -60,17 +60,22 @@ static void flashGreenLed(void *handle) { } #endif // TEST -int logMsg(const char *format, ...) { +static int innerLogMsg(const char *pre, const char *post, const char *format, va_list vl) { int res = -1; - char msgBuffer[MSGBUFFER_SIZE]; + int offset = 0; + char msgBuffer[MSGBUFFER_SIZE+20]; + memset(msgBuffer, 0, MSGBUFFER_SIZE+20); - va_list vl; - va_start(vl, format); - int vcnt = vsnprintf(msgBuffer, MSGBUFFER_SIZE-2, format, vl); - va_end(vl); + if (pre) { + strcpy(msgBuffer, pre); + offset = strlen(pre); + } + int vcnt = vsnprintf(msgBuffer+offset, MSGBUFFER_SIZE, format, vl); if (vcnt < MSGBUFFER_SIZE) { - strcat(msgBuffer, "\r\n"); + if (post) { + strcat(msgBuffer, post); + } if (-1 == (res = ringbufferPut(&logBuffer, (uint8_t*) msgBuffer, strlen(msgBuffer)))) { #ifndef TEST @@ -84,3 +89,21 @@ int logMsg(const char *format, ...) { } return res; } + +int logMsg(const char *format, ...) { + va_list vl; + va_start(vl, format); + int res = innerLogMsg(NULL, "\r\n", format, vl); + va_end(vl); + return res; +} + +int errMsg(const char *format, ...) { + va_list vl; + va_start(vl, format); + int res = innerLogMsg("\x1b[31;1m", "\x1b[0m\r\n", format, vl); + va_end(vl); + return res; +} + + diff --git a/cube/User/Src/mbusComm.c b/cube/User/Src/mbusComm.c index 8d6a07c..c077995 100644 --- a/cube/User/Src/mbusComm.c +++ b/cube/User/Src/mbusComm.c @@ -118,28 +118,28 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) { mbus_data_variable *data_var = &(frame_data.data_var); logMsg("papf sts: %02x", data_var->header.status); if ((data_var->header.status & 0x01)) { - logMsg("\x1b[31;1mpapf sts: Application Busy\x1b[0m"); + errMsg("papf sts: Application Busy"); } if ((data_var->header.status & 0x02)) { - logMsg("\x1b[31;1mpapf sts: Any Application Error\x1b[0m"); + errMsg("papf sts: Any Application Error"); } if ((data_var->header.status & 0x04)) { - logMsg("\x1b[31;1mpapf sts: Power Low\x1b[0m"); + errMsg("papf sts: Power Low"); } if ((data_var->header.status & 0x08)) { - logMsg("\x1b[31;1mpapf sts: Permanent Error\x1b[0m"); + errMsg("papf sts: Permanent Error"); } if ((data_var->header.status & 0x10)) { - logMsg("\x1b[31;1mpapf sts: Temporary Error\x1b[0m"); + errMsg("papf sts: Temporary Error"); } if ((data_var->header.status & 0x20)) { - logMsg("\x1b[31;1mpapf sts: Specific to manufacturer Error 1\x1b[0m"); + errMsg("papf sts: Specific to manufacturer Error 1"); } if ((data_var->header.status & 0x40)) { - logMsg("\x1b[31;1mpapf sts: Specific to manufacturer Error 2\x1b[0m"); + errMsg("papf sts: Specific to manufacturer Error 2"); } if ((data_var->header.status & 0x80)) { - logMsg("\x1b[31;1mpapf sts: Specific to manufacturer Error 3\x1b[0m"); + errMsg("papf sts: Specific to manufacturer Error 3"); } mbus_data_record *record; int i; @@ -161,7 +161,6 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) { } else { logMsg("papf err: unable to parse frame"); } - logMsg("\x1b[0m"); }