143 lines
3.0 KiB
C
143 lines
3.0 KiB
C
#ifndef TEST
|
|
#include <main.h>
|
|
#include <usart.h>
|
|
#include <show.h>
|
|
#include <PontCoopScheduler.h>
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <logger.h>
|
|
#include <ringbuffer.h>
|
|
|
|
|
|
#ifdef TEST
|
|
#define LOGBUFFER_SIZE 32
|
|
#define MSGBUFFER_SIZE 16
|
|
#else
|
|
#define LOGBUFFER_SIZE 1024
|
|
#define MSGBUFFER_SIZE 128
|
|
#endif // TEST
|
|
|
|
|
|
static ringbuffer_t logBuffer;
|
|
|
|
void logInit() {
|
|
ringbufferInit(&logBuffer, LOGBUFFER_SIZE);
|
|
}
|
|
|
|
void logFree() {
|
|
ringbufferFree(&logBuffer);
|
|
}
|
|
|
|
int logExec() {
|
|
int c = -1;
|
|
#ifndef TEST
|
|
if (__HAL_UART_GET_FLAG(&debugUart, UART_FLAG_TXE)) { // is the TX channel free
|
|
#endif // TEST
|
|
c = ringbufferGetOne(&logBuffer);
|
|
if (c > 0) {
|
|
#ifndef TEST
|
|
// transfer to TX channel
|
|
uint8_t cc = (uint8_t) c;
|
|
HAL_UART_Transmit(&debugUart, &cc, 1, HAL_MAX_DELAY);
|
|
#endif // TEST
|
|
}
|
|
#ifndef TEST
|
|
}
|
|
#endif // TEST
|
|
return c;
|
|
}
|
|
|
|
#ifndef TEST
|
|
static void flashGreenLed(void *handle) {
|
|
show(LED_GREEN, TOGGLE);
|
|
}
|
|
#endif // TEST
|
|
|
|
static int innerLogMsg(const char *pre, const char *post, const char *format, va_list vl) {
|
|
int res = -1;
|
|
int offset = 0;
|
|
char msgBuffer[MSGBUFFER_SIZE+20];
|
|
memset(msgBuffer, 0, MSGBUFFER_SIZE+20);
|
|
|
|
if (pre) {
|
|
strcpy(msgBuffer, pre);
|
|
offset = strlen(pre);
|
|
}
|
|
int vcnt = vsnprintf(msgBuffer+offset, MSGBUFFER_SIZE, format, vl);
|
|
|
|
if (vcnt < MSGBUFFER_SIZE) {
|
|
if (post) {
|
|
strcat(msgBuffer, post);
|
|
}
|
|
|
|
if (-1 == (res = ringbufferPut(&logBuffer, (uint8_t*) msgBuffer, strlen(msgBuffer)))) {
|
|
#ifndef TEST
|
|
// blink the green light or so
|
|
flashGreenLed(NULL);
|
|
schAdd(flashGreenLed, NULL, 100, 0);
|
|
#else
|
|
printf("\n*** green blink ***\n");
|
|
#endif // TEST
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
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 static char YELLOW[] = "\x1b[33;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;
|
|
case LOG_YELLOW:
|
|
pre = YELLOW;
|
|
break;
|
|
}
|
|
va_list vl;
|
|
va_start(vl, format);
|
|
int res = innerLogMsg(pre, POST, format, vl);
|
|
va_end(vl);
|
|
return res;
|
|
}
|
|
|
|
|
|
|