color refactoring
This commit is contained in:
parent
6bbe4d7eaf
commit
747cbd3658
@ -3,6 +3,14 @@
|
||||
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
LOG_HIGH,
|
||||
LOG_RED,
|
||||
LOG_GREEN,
|
||||
LOG_BLUE
|
||||
} t_logColor;
|
||||
|
||||
// initialize the logger, creates a ringbuffer
|
||||
void logInit();
|
||||
|
||||
@ -12,8 +20,12 @@ 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, ...);
|
||||
|
||||
// in red
|
||||
int errMsg(const char *format, ...);
|
||||
|
||||
int coloredMsg(const t_logColor color, const char *format, ...);
|
||||
|
||||
// reads the ringbuffer and transfers data to output channel
|
||||
// call this from the idle-loop
|
||||
// return value can be ignored, it is only used in test
|
||||
|
@ -106,4 +106,33 @@ int errMsg(const char *format, ...) {
|
||||
return res;
|
||||
}
|
||||
|
||||
int coloredMsg(const t_logColor color, const char *format, ...) {
|
||||
const static char POST[] = "\x1b[0m\r\n";
|
||||
const static char HIGH[] = "\x1b[1m";
|
||||
const static char RED[] = "\x1b[31;1m";
|
||||
const static char GREEN[] = "\x1b[32;1m";
|
||||
const static char BLUE[] = "\x1b[34;1m";
|
||||
const char *pre = NULL;
|
||||
switch (color) {
|
||||
case LOG_HIGH:
|
||||
pre = HIGH;
|
||||
break;
|
||||
case LOG_RED:
|
||||
pre = RED;
|
||||
break;
|
||||
case LOG_BLUE:
|
||||
pre = BLUE;
|
||||
break;
|
||||
case LOG_GREEN:
|
||||
pre = GREEN;
|
||||
break;
|
||||
}
|
||||
va_list vl;
|
||||
va_start(vl, format);
|
||||
int res = innerLogMsg(pre, POST, format, vl);
|
||||
va_end(vl);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -173,7 +173,7 @@ void scheduleMBusRequest(void *handle) {
|
||||
if (devices[i].delay <= 0) {
|
||||
devices[i].delay = devices[i].period;
|
||||
devices[i].waiting = true;
|
||||
logMsg("\x1b[32;1m *** Scheduled: %s\x1b[0m ", devices[i].deviceName);
|
||||
coloredMsg(LOG_GREEN, "*** Scheduled: %s", devices[i].deviceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
for (uint8_t j = 0; j < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; j++) {
|
||||
if ((mbusCommHandle->device->consideredField[j].index == i) &&
|
||||
(strlen(mbusCommHandle->device->consideredField[j].label) > 0)) {
|
||||
logMsg("\x1b[1mpapf txt: I:%d, L:%s, U:%s V:%s\x1b[0m",
|
||||
coloredMsg(LOG_BLUE, "papf txt: I:%d, L:%s, U:%s V:%s",
|
||||
i,
|
||||
mbusCommHandle->device->consideredField[j].label,
|
||||
mbus_data_record_unit(record),
|
||||
@ -159,14 +159,14 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
}
|
||||
mbus_data_record_free(data_var->record);
|
||||
} else {
|
||||
logMsg("papf err: unable to parse frame");
|
||||
errMsg("papf err: unable to parse frame");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void handleRequestEngine(void *handle);
|
||||
static void timeoutHandler(void *handle) {
|
||||
logMsg("mbc timeout");
|
||||
errMsg("mbc timeout");
|
||||
t_mbusCommHandle *localMbusCommHandle = (t_mbusCommHandle*) handle;
|
||||
localMbusCommHandle->state = MBCS_TIMEOUT;
|
||||
localMbusCommHandle->receiving = false;
|
||||
@ -238,7 +238,7 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->frame.start1 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_LENGTH1;
|
||||
} else {
|
||||
logMsg("hre err: invalid start1 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: invalid start1 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__START1;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
}
|
||||
@ -248,14 +248,14 @@ static void handleRequestEngine(void *handle) {
|
||||
case MBCS_LENGTH1:
|
||||
//logMsg("hre state LENGTH1");
|
||||
if (localMbusCommHandle->receivedOctet <= 3) {
|
||||
logMsg("hre err: length to small %02x", localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: length to small %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__LENGTH1;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
localMbusCommHandle->frame.length1 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->frame.userdata = (uint8_t*) malloc(localMbusCommHandle->frame.length1 - 3);
|
||||
if (! localMbusCommHandle->frame.userdata) {
|
||||
logMsg("hre err: unable to allocate memory for userdata");
|
||||
errMsg("hre err: unable to allocate memory for userdata");
|
||||
localMbusCommHandle->result = MBCR_ERROR_OUT_OF_MEMORY__USERDATA;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -268,7 +268,7 @@ static void handleRequestEngine(void *handle) {
|
||||
case MBCS_LENGTH2:
|
||||
//logMsg("hre state LENGTH2");
|
||||
if (localMbusCommHandle->frame.length1 != localMbusCommHandle->receivedOctet) {
|
||||
logMsg("hre err: invalid length2 %02x vs. %02x", localMbusCommHandle->frame.length1, localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: invalid length2 %02x vs. %02x", localMbusCommHandle->frame.length1, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__LENGTH2;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -284,7 +284,7 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->frame.start2 = localMbusCommHandle->receivedOctet;
|
||||
localMbusCommHandle->state = MBCS_C_FIELD;
|
||||
} else {
|
||||
logMsg("hre err: invalid start2 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: invalid start2 symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__START2;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
}
|
||||
@ -329,7 +329,7 @@ static void handleRequestEngine(void *handle) {
|
||||
case MBCS_CHKSUM:
|
||||
//logMsg("hre state CHKSUM");
|
||||
if (localMbusCommHandle->receivedOctet != calculatedChksum) {
|
||||
logMsg("hre err: invalid checksum %02x vs %02x", calculatedChksum, localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: invalid checksum %02x vs %02x", calculatedChksum, localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__INVALID_CHKSUM;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
} else {
|
||||
@ -346,7 +346,7 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->state = MBCS_DONE;
|
||||
schAdd(handleRequestEngine, handle, 0, 0);
|
||||
} else {
|
||||
logMsg("hre err: invalid stop symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: invalid stop symbol %02x", localMbusCommHandle->receivedOctet);
|
||||
localMbusCommHandle->result = MBCR_ERROR_STATE_ENGINE__STOP;
|
||||
localMbusCommHandle->state = MBCS_ERROR;
|
||||
receiveNext(localMbusCommHandle);
|
||||
@ -367,14 +367,14 @@ static void handleRequestEngine(void *handle) {
|
||||
break;
|
||||
|
||||
case MBCS_ERROR:
|
||||
logMsg("hre state ERROR");
|
||||
errMsg("hre state ERROR");
|
||||
show(LED_RED, ON);
|
||||
logMsg("hre err: already error, read the rest (now: %02x) until timeout", localMbusCommHandle->receivedOctet);
|
||||
errMsg("hre err: already error, read the rest (now: %02x) until timeout", localMbusCommHandle->receivedOctet);
|
||||
receiveNext(localMbusCommHandle);
|
||||
break;
|
||||
|
||||
case MBCS_TIMEOUT:
|
||||
logMsg("hre state TIMEOUT");
|
||||
errMsg("hre state TIMEOUT");
|
||||
localMbusCommHandle->device->failures += 1;
|
||||
localMbusCommHandle->receiving = false;
|
||||
if (localMbusCommHandle->frame.userdata != NULL) {
|
||||
@ -385,7 +385,7 @@ static void handleRequestEngine(void *handle) {
|
||||
uint8_t kitchenSink[16];
|
||||
memset(kitchenSink, 0, 16);
|
||||
HAL_StatusTypeDef r = HAL_UART_Receive(&mbusUart, kitchenSink, 16, 100);
|
||||
logMsg("hre abort, last receive result: %02x", r);
|
||||
errMsg("hre abort, last receive result: %02x", r);
|
||||
// no break
|
||||
|
||||
case MBCS_DISABLE_FRONTEND:
|
||||
@ -410,7 +410,7 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
|
||||
mbusCommHandle.receiving = false;
|
||||
} else {
|
||||
logMsg("mcrx: received 0x%02x but not expected", mbusCommHandle.receivedOctet);
|
||||
errMsg("mcrx: received 0x%02x but not expected", mbusCommHandle.receivedOctet);
|
||||
}
|
||||
}
|
||||
|
||||
@ -420,7 +420,7 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
|
||||
if (mbusCommHandle.state == MBCS_IDLE) {
|
||||
logMsg("");
|
||||
logMsg("\x1b[34;1m*** NEW REQUEST %s R:%d F:%d C:%d ***\x1b[0m",
|
||||
coloredMsg(LOG_BLUE, "*** NEW REQUEST %s R:%d F:%d C:%d ***",
|
||||
mbusDevice->deviceName,
|
||||
mbusDevice->requests,
|
||||
mbusDevice->failures,
|
||||
|
Loading…
x
Reference in New Issue
Block a user