143 lines
3.0 KiB
C
Raw Normal View History

2020-10-29 14:30:55 +01:00
#ifndef TEST
2020-10-28 19:40:08 +01:00
#include <main.h>
#include <usart.h>
2020-11-03 10:05:45 +01:00
#include <show.h>
2020-10-29 15:44:13 +01:00
#include <PontCoopScheduler.h>
2020-10-29 14:30:55 +01:00
#endif
2020-10-28 19:40:08 +01:00
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
2020-10-29 20:50:50 +01:00
#include <stdarg.h>
2020-10-29 14:30:55 +01:00
#include <stdio.h>
#include <logger.h>
#include <ringbuffer.h>
2020-10-28 19:40:08 +01:00
2020-10-29 14:30:55 +01:00
#ifdef TEST
#define LOGBUFFER_SIZE 32
2020-10-29 20:50:50 +01:00
#define MSGBUFFER_SIZE 16
2020-10-29 14:30:55 +01:00
#else
2020-11-03 10:51:02 +01:00
#define LOGBUFFER_SIZE 1024
#define MSGBUFFER_SIZE 128
2020-10-29 14:30:55 +01:00
#endif // TEST
static ringbuffer_t logBuffer;
void logInit() {
ringbufferInit(&logBuffer, LOGBUFFER_SIZE);
}
void logFree() {
ringbufferFree(&logBuffer);
}
2020-10-29 15:44:13 +01:00
int logExec() {
2020-10-29 14:30:55 +01:00
int c = -1;
#ifndef TEST
2020-10-29 15:44:13 +01:00
if (__HAL_UART_GET_FLAG(&debugUart, UART_FLAG_TXE)) { // is the TX channel free
2020-10-29 14:30:55 +01:00
#endif // TEST
c = ringbufferGetOne(&logBuffer);
if (c > 0) {
#ifndef TEST
// transfer to TX channel
2020-10-29 15:44:13 +01:00
uint8_t cc = (uint8_t) c;
HAL_UART_Transmit(&debugUart, &cc, 1, HAL_MAX_DELAY);
2020-10-29 14:30:55 +01:00
#endif // TEST
}
#ifndef TEST
}
#endif // TEST
return c;
}
2020-10-29 15:44:13 +01:00
#ifndef TEST
static void flashGreenLed(void *handle) {
2020-11-03 10:05:45 +01:00
show(LED_GREEN, TOGGLE);
2020-10-29 15:44:13 +01:00
}
#endif // TEST
2020-11-03 17:30:01 +01:00
static int innerLogMsg(const char *pre, const char *post, const char *format, va_list vl) {
2020-10-29 14:30:55 +01:00
int res = -1;
2020-11-03 17:30:01 +01:00
int offset = 0;
char msgBuffer[MSGBUFFER_SIZE+20];
memset(msgBuffer, 0, MSGBUFFER_SIZE+20);
2020-10-29 14:30:55 +01:00
2020-11-03 17:30:01 +01:00
if (pre) {
strcpy(msgBuffer, pre);
offset = strlen(pre);
}
int vcnt = vsnprintf(msgBuffer+offset, MSGBUFFER_SIZE, format, vl);
2020-10-29 20:50:50 +01:00
if (vcnt < MSGBUFFER_SIZE) {
2020-11-03 17:30:01 +01:00
if (post) {
strcat(msgBuffer, post);
}
2020-10-29 21:02:04 +01:00
2020-10-29 20:50:50 +01:00
if (-1 == (res = ringbufferPut(&logBuffer, (uint8_t*) msgBuffer, strlen(msgBuffer)))) {
2020-10-29 14:30:55 +01:00
#ifndef TEST
2020-10-29 20:50:50 +01:00
// blink the green light or so
flashGreenLed(NULL);
schAdd(flashGreenLed, NULL, 100, 0);
2020-10-29 14:30:55 +01:00
#else
2020-10-29 20:50:50 +01:00
printf("\n*** green blink ***\n");
2020-10-29 14:30:55 +01:00
#endif // TEST
2020-10-29 20:50:50 +01:00
}
2020-10-28 19:40:08 +01:00
}
2020-10-29 14:30:55 +01:00
return res;
2020-10-28 19:40:08 +01:00
}
2020-11-03 17:30:01 +01:00
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;
}
2020-11-03 17:57:03 +01:00
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";
2020-11-03 18:02:36 +01:00
const static char YELLOW[] = "\x1b[33;1m";
2020-11-03 17:57:03 +01:00
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;
2020-11-03 18:02:36 +01:00
case LOG_YELLOW:
pre = YELLOW;
break;
2020-11-03 17:57:03 +01:00
}
va_list vl;
va_start(vl, format);
int res = innerLogMsg(pre, POST, format, vl);
va_end(vl);
return res;
}
2020-11-03 17:30:01 +01:00