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_
#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

View File

@ -8,10 +8,8 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef TEST
#include <stdarg.h>
#include <stdio.h>
#endif
#include <logger.h>
@ -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;
}

View File

@ -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);
}