logger, implementation and test
This commit is contained in:
parent
08c4907526
commit
e5a77ccc00
@ -1,6 +1,19 @@
|
|||||||
#ifndef _LOGGER_H_
|
#ifndef _LOGGER_H_
|
||||||
#define _LOGGER_H_
|
#define _LOGGER_H_
|
||||||
|
|
||||||
void log(char *msg);
|
// initialize the logger, creates a ringbuffer
|
||||||
|
void logInit();
|
||||||
|
|
||||||
|
// de-initialize the logger, free the ringbuffer
|
||||||
|
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);
|
||||||
|
|
||||||
|
// reads the ringbuffer and transfers data to output channel
|
||||||
|
// call this from the idle-loop
|
||||||
|
// return value can be ignored, it is only used in test
|
||||||
|
int logExecute();
|
||||||
|
|
||||||
#endif // _LOGGER_H_
|
#endif // _LOGGER_H_
|
||||||
|
@ -14,10 +14,13 @@ typedef struct {
|
|||||||
|
|
||||||
void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize);
|
void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize);
|
||||||
void ringbufferFree(ringbuffer_t *handle);
|
void ringbufferFree(ringbuffer_t *handle);
|
||||||
|
|
||||||
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen);
|
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen);
|
||||||
uint8_t *ringbufferGet(ringbuffer_t *handle, uint32_t dataLen);
|
|
||||||
bool ringbufferEmpty(ringbuffer_t *handle);
|
bool ringbufferEmpty(ringbuffer_t *handle);
|
||||||
int ringbufferGetOne(ringbuffer_t *handle); // if positive, cast to uint8_t and be happy, if negative error
|
int ringbufferGetOne(ringbuffer_t *handle); // if positive, cast to uint8_t and be happy, if negative error
|
||||||
|
|
||||||
|
// not yet implemented
|
||||||
|
uint8_t *ringbufferGet(ringbuffer_t *handle, uint32_t dataLen);
|
||||||
|
|
||||||
|
|
||||||
#endif // _RINGBUFFER_H_
|
#endif // _RINGBUFFER_H_
|
||||||
|
@ -1,17 +1,65 @@
|
|||||||
|
#ifndef TEST
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
#include <usart.h>
|
#include <usart.h>
|
||||||
#include <logger.h>
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void log(char *msg) {
|
#ifdef TEST
|
||||||
uint16_t len = strlen(msg);
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
if (HAL_UART_Transmit_IT(&debugUart, msg, len) != HAL_OK) {
|
|
||||||
if(RingBuffer_Write(&txBuf, msg, len) != RING_BUFFER_OK)
|
#include <logger.h>
|
||||||
return 0;
|
#include <ringbuffer.h>
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
|
#ifdef TEST
|
||||||
|
#define LOGBUFFER_SIZE 32
|
||||||
|
#else
|
||||||
|
#define LOGBUFFER_SIZE 1024
|
||||||
|
#endif // TEST
|
||||||
|
|
||||||
|
|
||||||
|
static ringbuffer_t logBuffer;
|
||||||
|
|
||||||
|
void logInit() {
|
||||||
|
ringbufferInit(&logBuffer, LOGBUFFER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void logFree() {
|
||||||
|
ringbufferFree(&logBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
int logExecute() {
|
||||||
|
int c = -1;
|
||||||
|
#ifndef TEST
|
||||||
|
if (false) { // is the TX channel free
|
||||||
|
#endif // TEST
|
||||||
|
c = ringbufferGetOne(&logBuffer);
|
||||||
|
if (c > 0) {
|
||||||
|
#ifndef TEST
|
||||||
|
// transfer to TX channel
|
||||||
|
#endif // TEST
|
||||||
|
}
|
||||||
|
#ifndef TEST
|
||||||
|
}
|
||||||
|
#endif // TEST
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int logMsg(char *msg) {
|
||||||
|
int res = -1;
|
||||||
|
|
||||||
|
if (-1 == (res = ringbufferPut(&logBuffer, msg, strlen(msg)))) {
|
||||||
|
#ifndef TEST
|
||||||
|
// blink the red light or so
|
||||||
|
#else
|
||||||
|
printf("\n*** red blink ***\n");
|
||||||
|
#endif // TEST
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,6 @@
|
|||||||
#include <ringbuffer.h>
|
#include <ringbuffer.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
typedef struct {
|
|
||||||
uint32_t bufferReadIdx;
|
|
||||||
uint32_t bufferWriteIdx;
|
|
||||||
uint32_t bufferSize;
|
|
||||||
uint8_t* buffer;
|
|
||||||
} ringbuffer_t;
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize) {
|
void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize) {
|
||||||
handle->bufferSize = bufferSize;
|
handle->bufferSize = bufferSize;
|
||||||
handle->buffer = (uint8_t*) malloc(handle->bufferSize);
|
handle->buffer = (uint8_t*) malloc(handle->bufferSize);
|
||||||
@ -31,27 +19,6 @@ void ringbufferFree(ringbuffer_t *handle) {
|
|||||||
handle->bufferWriteIdx = 0;
|
handle->bufferWriteIdx = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
void ringbufferPut(ringbuffer_t *handle, void *f) {
|
|
||||||
if (handle->bufferWriteIdx == (BUFFER_SIZE - 1)) {
|
|
||||||
while (handle->bufferReadIdx == BUFFER_SIZE);
|
|
||||||
} else {
|
|
||||||
while (handle->bufferReadIdx == (handle->bufferWriteIdx + 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
handle->buffer[handle->bufferWriteIdx] = f;
|
|
||||||
handle->bufferWriteIdx++;
|
|
||||||
|
|
||||||
if (handle->bufferWriteIdx > BUFFER_SIZE) {
|
|
||||||
handle->bufferWriteIdx = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&(handle->eventMutex));
|
|
||||||
pthread_cond_signal(&(handle->eventSignal));
|
|
||||||
pthread_mutex_unlock(&(handle->eventMutex));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen) {
|
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen) {
|
||||||
uint32_t freeSpace = 0;
|
uint32_t freeSpace = 0;
|
||||||
if (handle->bufferReadIdx == handle->bufferWriteIdx) {
|
if (handle->bufferReadIdx == handle->bufferWriteIdx) {
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
CFLAGS=-I../cube/User/Inc
|
CFLAGS=-I../cube/User/Inc -DTEST
|
||||||
|
|
||||||
test: ringbuffer.o test.o
|
test: ringbuffer.o logger.o test.o
|
||||||
gcc -o $@ -lcunit $^
|
gcc -o $@ -lcunit $^
|
||||||
|
|
||||||
ringbuffer.o: ../cube/User/Src/ringbuffer.c
|
ringbuffer.o: ../cube/User/Src/ringbuffer.c
|
||||||
gcc -c -o $@ $(CFLAGS) $^
|
gcc -c -o $@ $(CFLAGS) $^
|
||||||
|
|
||||||
|
logger.o: ../cube/User/Src/logger.c
|
||||||
|
gcc -c -o $@ $(CFLAGS) $^
|
||||||
|
|
||||||
test.o: test.c
|
test.o: test.c
|
||||||
gcc -c -o $@ $(CFLAGS) $^
|
gcc -c -o $@ $(CFLAGS) $^
|
||||||
|
|
||||||
|
145
tests/test.c
145
tests/test.c
@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <ringbuffer.h>
|
#include <ringbuffer.h>
|
||||||
|
#include <logger.h>
|
||||||
|
|
||||||
|
|
||||||
// #define DEBUG
|
// #define DEBUG
|
||||||
@ -815,6 +816,134 @@ void testRingbuffer99() {
|
|||||||
CU_ASSERT(rb.bufferReadIdx == 0);
|
CU_ASSERT(rb.bufferReadIdx == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int init_suite_logger(void) {
|
||||||
|
logInit();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int clean_suite_logger(void) {
|
||||||
|
logFree();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testLogger0() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("\nLog a message\n");
|
||||||
|
#endif
|
||||||
|
char goldValue1[] = "Wolfgang\n";
|
||||||
|
logMsg(goldValue1);
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("\nRead all chars from logging\n");
|
||||||
|
#endif
|
||||||
|
char buffer[128];
|
||||||
|
int c;
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
memset(buffer, 0, 128);
|
||||||
|
i = 0;
|
||||||
|
while (0 < (c = logExecute())) {
|
||||||
|
buffer[i] = (uint8_t) c;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
CU_ASSERT(strcmp(goldValue1, buffer) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testLogger1() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("\nLog some messages\n");
|
||||||
|
#endif
|
||||||
|
char goldValueFull[128];
|
||||||
|
memset(goldValueFull, 0, 128);
|
||||||
|
char goldValue1[] = "Wolfgang\n"; // 9
|
||||||
|
strcat(goldValueFull, goldValue1);
|
||||||
|
logMsg(goldValue1);
|
||||||
|
char goldValue2[] = "Andreas\n"; // +8 = 17
|
||||||
|
strcat(goldValueFull, goldValue2);
|
||||||
|
logMsg(goldValue2);
|
||||||
|
char goldValue3[] = "Frank\n"; // +6 = 23
|
||||||
|
strcat(goldValueFull, goldValue3);
|
||||||
|
logMsg(goldValue3);
|
||||||
|
char goldValue4[] = "Thomas\n"; // +7 = 30
|
||||||
|
strcat(goldValueFull, goldValue4);
|
||||||
|
logMsg(goldValue4);
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("\nRead all chars from logging\n");
|
||||||
|
#endif
|
||||||
|
char buffer[128];
|
||||||
|
int c;
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
memset(buffer, 0, 128);
|
||||||
|
i = 0;
|
||||||
|
while (0 < (c = logExecute())) {
|
||||||
|
buffer[i] = (uint8_t) c;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// printf("Buffer: %s\n", buffer);
|
||||||
|
CU_ASSERT(strcmp(goldValueFull, buffer) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testLogger2() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("\nLog some messages, a bit more than space in buffer\n");
|
||||||
|
#endif
|
||||||
|
char goldValueFullNotOk[128];
|
||||||
|
memset(goldValueFullNotOk, 0, 128);
|
||||||
|
char goldValueFullOk[128];
|
||||||
|
memset(goldValueFullOk, 0, 128);
|
||||||
|
char goldValue1[] = "Wolfgang\n"; // 9
|
||||||
|
strcat(goldValueFullNotOk, goldValue1);
|
||||||
|
strcat(goldValueFullOk, goldValue1);
|
||||||
|
int r = logMsg(goldValue1);
|
||||||
|
CU_ASSERT(r == 0);
|
||||||
|
char goldValue2[] = "Andreas\n"; // +8 = 17
|
||||||
|
strcat(goldValueFullNotOk, goldValue2);
|
||||||
|
strcat(goldValueFullOk, goldValue2);
|
||||||
|
r = logMsg(goldValue2);
|
||||||
|
CU_ASSERT(r == 0);
|
||||||
|
char goldValue3[] = "Frank\n"; // +6 = 23
|
||||||
|
strcat(goldValueFullNotOk, goldValue3);
|
||||||
|
strcat(goldValueFullOk, goldValue3);
|
||||||
|
r = logMsg(goldValue3);
|
||||||
|
CU_ASSERT(r == 0);
|
||||||
|
char goldValue4[] = "Thomas\n"; // +7 = 30
|
||||||
|
strcat(goldValueFullNotOk, goldValue4);
|
||||||
|
strcat(goldValueFullOk, goldValue4);
|
||||||
|
r = logMsg(goldValue4);
|
||||||
|
CU_ASSERT(r == 0);
|
||||||
|
char goldValue5[] = "Barbara\n"; // +8 = 38, too much
|
||||||
|
strcat(goldValueFullNotOk, goldValue5);
|
||||||
|
r = logMsg(goldValue5);
|
||||||
|
CU_ASSERT(r == -1);
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("\nRead all chars from logging\n");
|
||||||
|
#endif
|
||||||
|
char buffer[128];
|
||||||
|
int c;
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
memset(buffer, 0, 128);
|
||||||
|
i = 0;
|
||||||
|
while (0 < (c = logExecute())) {
|
||||||
|
buffer[i] = (uint8_t) c;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// printf("Buffer: %s\n", buffer);
|
||||||
|
CU_ASSERT(strcmp(goldValueFullNotOk, buffer) != 0);
|
||||||
|
CU_ASSERT(strcmp(goldValueFullOk, buffer) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (CUE_SUCCESS != CU_initialize_registry())
|
if (CUE_SUCCESS != CU_initialize_registry())
|
||||||
return CU_get_error();
|
return CU_get_error();
|
||||||
@ -848,6 +977,22 @@ int main() {
|
|||||||
return CU_get_error();
|
return CU_get_error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CU_pSuite loggerSuite = CU_add_suite("Suite_Logger", init_suite_logger, clean_suite_logger);
|
||||||
|
if (NULL == loggerSuite) {
|
||||||
|
CU_cleanup_registry();
|
||||||
|
return CU_get_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
(NULL == CU_add_test(loggerSuite, "test 0 of logger", testLogger0)) ||
|
||||||
|
(NULL == CU_add_test(loggerSuite, "test 1 of logger", testLogger1)) ||
|
||||||
|
(NULL == CU_add_test(loggerSuite, "test 2 of logger", testLogger2)) ||
|
||||||
|
0 ) {
|
||||||
|
CU_cleanup_registry();
|
||||||
|
return CU_get_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
CU_basic_set_mode(CU_BRM_VERBOSE);
|
CU_basic_set_mode(CU_BRM_VERBOSE);
|
||||||
CU_basic_run_tests();
|
CU_basic_run_tests();
|
||||||
CU_cleanup_registry();
|
CU_cleanup_registry();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user