valist in logMsg, test on platform

This commit is contained in:
2020-10-29 20:50:50 +01:00
parent b55d74a9cd
commit e476930526
3 changed files with 24 additions and 13 deletions

View File

@ -1,6 +1,8 @@
#ifndef _LOGGER_H_ #ifndef _LOGGER_H_
#define _LOGGER_H_ #define _LOGGER_H_
// initialize the logger, creates a ringbuffer // initialize the logger, creates a ringbuffer
void logInit(); void logInit();
@ -9,7 +11,7 @@ void logFree();
// log a message, make sure it is a null-terminated string // log a message, make sure it is a null-terminated string
// return value can be ignored, it is only used in test // 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 // reads the ringbuffer and transfers data to output channel
// call this from the idle-loop // call this from the idle-loop

View File

@ -8,10 +8,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#ifdef TEST
#include <stdio.h> #include <stdio.h>
#endif
#include <logger.h> #include <logger.h>
@ -20,8 +18,10 @@
#ifdef TEST #ifdef TEST
#define LOGBUFFER_SIZE 32 #define LOGBUFFER_SIZE 32
#define MSGBUFFER_SIZE 16
#else #else
#define LOGBUFFER_SIZE 1024 #define LOGBUFFER_SIZE 1024
#define MSGBUFFER_SIZE 64
#endif // TEST #endif // TEST
@ -60,10 +60,17 @@ static void flashGreenLed(void *handle) {
} }
#endif // TEST #endif // TEST
int logMsg(char *msg) { int logMsg(const char *format, ...) {
int res = -1; 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 #ifndef TEST
// blink the green light or so // blink the green light or so
flashGreenLed(NULL); flashGreenLed(NULL);
@ -72,6 +79,6 @@ int logMsg(char *msg) {
printf("\n*** green blink ***\n"); printf("\n*** green blink ***\n");
#endif // TEST #endif // TEST
} }
}
return res; return res;
} }

View File

@ -24,7 +24,9 @@ void my_errorHandler() {
} }
void helloWorld(void *handle) { 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) { void helloMeterbus(void *handle) {
@ -36,7 +38,7 @@ void my_setup_2() {
led(RED, OFF); led(RED, OFF);
led(GREEN, ON); led(GREEN, ON);
schAdd(helloWorld, NULL, 0, 5000); schAdd(helloWorld, NULL, 0, 1000);
schAdd(helloMeterbus, NULL, 0, 10000); schAdd(helloMeterbus, NULL, 0, 10000);
} }