5 changed files with 269 additions and 18 deletions
@ -0,0 +1,16 @@ |
|||
/*
|
|||
* logger.h |
|||
* |
|||
* Created on: Jul 4, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#ifndef INC_LOGGER_H_ |
|||
#define INC_LOGGER_H_ |
|||
|
|||
void log_debug(const char * format, int nargs, ...); |
|||
void log_info(const char * format, int nargs, ...); |
|||
void log_warning(const char * format, int nargs, ...); |
|||
void log_error(const char * format, int nargs, ...); |
|||
|
|||
#endif /* INC_LOGGER_H_ */ |
|||
@ -0,0 +1,118 @@ |
|||
/*
|
|||
* logger.c |
|||
* |
|||
* Created on: Jul 4, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#include <stdarg.h> |
|||
#include <string.h> |
|||
|
|||
#include "usbd_cdc_if.h" |
|||
#include "logger.h" |
|||
|
|||
#define BUFFER_MAX 512 |
|||
#define COMMAND_HEADER_SIZE 4 |
|||
#define LOG_COMMAND_HEADER_SIZE 1 |
|||
#define HEADER_SIZE (COMMAND_HEADER_SIZE + LOG_COMMAND_HEADER_SIZE) |
|||
#define MESSAGE_BUFFER_START (COMMAND_HEADER_SIZE + LOG_COMMAND_HEADER_SIZE) |
|||
#define MAX_LOG_MESSAGE_SIZE (BUFFER_MAX - COMMAND_HEADER_SIZE - LOG_COMMAND_HEADER_SIZE - 1) |
|||
|
|||
#define min(a,b) \ |
|||
({ __typeof__ (a) _a = (a); \ |
|||
__typeof__ (b) _b = (b); \ |
|||
_a < _b ? _a : _b; }) |
|||
|
|||
|
|||
typedef struct |
|||
{ |
|||
uint8_t LOG_LEVEL_DEBUG; |
|||
uint8_t LOG_LEVEL_INFO; |
|||
uint8_t LOG_LEVEL_WARNING; |
|||
uint8_t LOG_LEVEL_ERROR; |
|||
} LOGGER_SEVERTITY_T; |
|||
|
|||
LOGGER_SEVERTITY_T LOGGER_SEVERTITY = { |
|||
.LOG_LEVEL_DEBUG = 10, |
|||
.LOG_LEVEL_INFO = 20, |
|||
.LOG_LEVEL_WARNING = 30, |
|||
.LOG_LEVEL_ERROR = 40, |
|||
}; |
|||
|
|||
/*
|
|||
* A command is 4 bytes long |
|||
* |
|||
* | 1 byte | 2 bytes | 1 byte | N bytes | 1 byte | |
|||
* | command ID | length N | reserved | data | stop byte | |
|||
* */ |
|||
|
|||
/*
|
|||
* command length = N |
|||
* | 1 byte | N-1 bytes | |
|||
* | severtity | message | |
|||
* */ |
|||
char buffer[BUFFER_MAX]; |
|||
|
|||
void log_message(uint8_t severtiy) { |
|||
int message_length = min(strlen(buffer + MESSAGE_BUFFER_START), MAX_LOG_MESSAGE_SIZE); |
|||
int command_length = message_length + 1; |
|||
buffer[0] = 0xFF; // log command
|
|||
buffer[1] = (command_length & 0x0000FF00) >> 8; |
|||
buffer[2] = command_length & 0x000000FF; |
|||
buffer[3] = 0x00; // reserved
|
|||
buffer[4] = severtiy; |
|||
buffer[5 + message_length] = 0xFF; // stop byte
|
|||
CDC_Transmit_FS((uint8_t*)buffer, (uint16_t)(HEADER_SIZE + message_length + 1)); |
|||
} |
|||
|
|||
void log_debug(const char * format, int nargs, ...) { |
|||
va_list args; |
|||
va_start(args, nargs); |
|||
vsnprintf( |
|||
buffer + MESSAGE_BUFFER_START, |
|||
MAX_LOG_MESSAGE_SIZE, |
|||
format, |
|||
args |
|||
); |
|||
va_end(args); |
|||
log_message(LOGGER_SEVERTITY.LOG_LEVEL_DEBUG); |
|||
} |
|||
|
|||
void log_info(const char * format, int nargs, ...) { |
|||
va_list args; |
|||
va_start(args, nargs); |
|||
vsnprintf( |
|||
buffer + MESSAGE_BUFFER_START, |
|||
MAX_LOG_MESSAGE_SIZE, |
|||
format, |
|||
args |
|||
); |
|||
va_end(args); |
|||
log_message(LOGGER_SEVERTITY.LOG_LEVEL_INFO); |
|||
} |
|||
|
|||
void log_warning(const char * format, int nargs, ...) { |
|||
va_list args; |
|||
va_start(args, nargs); |
|||
vsnprintf( |
|||
buffer + MESSAGE_BUFFER_START, |
|||
MAX_LOG_MESSAGE_SIZE, |
|||
format, |
|||
args |
|||
); |
|||
va_end(args); |
|||
log_message(LOGGER_SEVERTITY.LOG_LEVEL_WARNING); |
|||
} |
|||
|
|||
void log_error(const char * format, int nargs, ...) { |
|||
va_list args; |
|||
va_start(args, nargs); |
|||
vsnprintf( |
|||
buffer + MESSAGE_BUFFER_START, |
|||
MAX_LOG_MESSAGE_SIZE, |
|||
format, |
|||
args |
|||
); |
|||
va_end(args); |
|||
log_message(LOGGER_SEVERTITY.LOG_LEVEL_ERROR); |
|||
} |
|||
@ -1 +1,2 @@ |
|||
pre-commit |
|||
pyserial>=3.5,<4 |
|||
|
|||
Loading…
Reference in new issue