diff --git a/cube/User/Inc/logger.h b/cube/User/Inc/logger.h index d051bdf..48dfa41 100644 --- a/cube/User/Inc/logger.h +++ b/cube/User/Inc/logger.h @@ -1,6 +1,8 @@ #ifndef _LOGGER_H_ #define _LOGGER_H_ + + // initialize the logger, creates a ringbuffer void logInit(); @@ -9,7 +11,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(char *msg); +int logMsg(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 62627cf..5cdcffa 100644 --- a/cube/User/Src/logger.c +++ b/cube/User/Src/logger.c @@ -8,10 +8,8 @@ #include #include #include - -#ifdef TEST +#include #include -#endif #include @@ -20,8 +18,10 @@ #ifdef TEST #define LOGBUFFER_SIZE 32 +#define MSGBUFFER_SIZE 16 #else #define LOGBUFFER_SIZE 1024 +#define MSGBUFFER_SIZE 64 #endif // TEST @@ -60,18 +60,25 @@ static void flashGreenLed(void *handle) { } #endif // TEST -int logMsg(char *msg) { +int logMsg(const char *format, ...) { int res = -1; + char msgBuffer[MSGBUFFER_SIZE]; - if (-1 == (res = ringbufferPut(&logBuffer, (uint8_t*) msg, strlen(msg)))) { + va_list vl; + va_start(vl, format); + int vcnt = vsnprintf(msgBuffer, MSGBUFFER_SIZE, format, vl); + va_end(vl); + + if (vcnt < MSGBUFFER_SIZE) { + 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); + // blink the green light or so + flashGreenLed(NULL); + schAdd(flashGreenLed, NULL, 100, 0); #else - printf("\n*** green blink ***\n"); + printf("\n*** green blink ***\n"); #endif // TEST + } } - return res; } diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index ef6ed53..c599b95 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -24,7 +24,9 @@ void my_errorHandler() { } void helloWorld(void *handle) { - logMsg("Hello World\n\r"); + static uint32_t cnt = 0; + logMsg("Hello World, %ld\n\r", cnt); + cnt++; } void helloMeterbus(void *handle) { @@ -36,7 +38,7 @@ void my_setup_2() { led(RED, OFF); led(GREEN, ON); - schAdd(helloWorld, NULL, 0, 5000); + schAdd(helloWorld, NULL, 0, 1000); schAdd(helloMeterbus, NULL, 0, 10000); }