feat(core): Expose message size on logger as parameter

Make this configurable. Normally the buffer size
doesn’t need to be 64k, that’s only required for
very verbose debug/development logging output.

Furthermore, the parameter needs to shared and
aligned with the async logger which has it’s own
buffer size. This avoids tight coupling between
the async sink and the logger.
This commit is contained in:
icex2 2024-08-15 11:34:31 +02:00
parent 903bddddce
commit c3d95b0ffd
2 changed files with 35 additions and 17 deletions

View File

@ -12,10 +12,9 @@
#include "util/mem.h"
#include "util/str.h"
/* 64k so we can log data dumps of rs232 without crashing */
#define CORE_LOG_MSG_SIZE_MAX 65536
#define CORE_LOG_TIMESTAMP_SIZE_MAX 64
static uint32_t _core_log_bt_msg_buffer_size;
static enum core_log_bt_log_level _core_log_bt_log_level;
static core_log_sink_t *_core_log_bt_sink;
@ -28,8 +27,8 @@ static void _core_log_bt_vformat_timestamp_log_level_write(
static const char chars[] = "FFWIM";
char timestamp[CORE_LOG_TIMESTAMP_SIZE_MAX];
char msg[CORE_LOG_MSG_SIZE_MAX];
char line[CORE_LOG_MSG_SIZE_MAX];
char msg[_core_log_bt_msg_buffer_size];
char line[_core_log_bt_msg_buffer_size];
int result;
time_t curtime;
@ -41,6 +40,8 @@ static void _core_log_bt_vformat_timestamp_log_level_write(
curtime = time(NULL);
tm = localtime(&curtime);
// TODO truncate messages if too large and emit log warning message this happened
strftime(timestamp, sizeof(timestamp), "[%Y/%m/%d %H:%M:%S]", tm);
str_vformat(msg, sizeof(msg), fmt, ap);
@ -61,6 +62,9 @@ static void _core_log_bt_log_misc(const char *module, const char *fmt, ...)
{
va_list args;
// Cut off logging as early as possible if the logging level isn't configured
// This saves processing time that might have performance impact on the
// calling thread
if (_core_log_bt_log_level >= CORE_LOG_BT_LOG_LEVEL_MISC) {
va_start(args, fmt);
_core_log_bt_vformat_timestamp_log_level_write(
@ -73,6 +77,9 @@ static void _core_log_bt_log_info(const char *module, const char *fmt, ...)
{
va_list args;
// Cut off logging as early as possible if the logging level isn't configured
// This saves processing time that might have performance impact on the
// calling thread
if (_core_log_bt_log_level >= CORE_LOG_BT_LOG_LEVEL_INFO) {
va_start(args, fmt);
_core_log_bt_vformat_timestamp_log_level_write(
@ -85,6 +92,9 @@ static void _core_log_bt_log_warning(const char *module, const char *fmt, ...)
{
va_list args;
// Cut off logging as early as possible if the logging level isn't configured
// This saves processing time that might have performance impact on the
// calling thread
if (_core_log_bt_log_level >= CORE_LOG_BT_LOG_LEVEL_WARNING) {
va_start(args, fmt);
_core_log_bt_vformat_timestamp_log_level_write(
@ -97,6 +107,9 @@ static void _core_log_bt_log_fatal(const char *module, const char *fmt, ...)
{
va_list args;
// Cut off logging as early as possible if the logging level isn't configured
// This saves processing time that might have performance impact on the
// calling thread
if (_core_log_bt_log_level >= CORE_LOG_BT_LOG_LEVEL_FATAL) {
va_start(args, fmt);
_core_log_bt_vformat_timestamp_log_level_write(
@ -125,32 +138,35 @@ void core_log_bt_core_api_set()
bt_core_log_api_set(&api);
}
void core_log_bt_init(const core_log_sink_t *sink)
void core_log_bt_init(
size_t msg_buffer_size,
const core_log_sink_t *sink)
{
// Sanity check for sizes we consider too much or too less
log_assert(msg_buffer_size > 1024); // 1 kB
log_assert(msg_buffer_size <= 1024 * 1024); // 1 MB
log_assert(sink);
log_misc("Init");
_core_log_bt_msg_buffer_size = msg_buffer_size;
_core_log_bt_sink = xmalloc(sizeof(core_log_sink_t));
memcpy(_core_log_bt_sink, sink, sizeof(core_log_sink_t));
_core_log_bt_log_level = CORE_LOG_BT_LOG_LEVEL_OFF;
log_misc("Init: msg_buffer_size %d", msg_buffer_size);
}
void core_log_bt_reinit(const core_log_sink_t *sink)
void core_log_bt_reinit(
size_t msg_buffer_size,
const core_log_sink_t *sink)
{
log_assert(_core_log_bt_sink);
log_misc("Re-init");
_core_log_bt_sink->close(_core_log_bt_sink->ctx);
free(_core_log_bt_sink);
_core_log_bt_sink = xmalloc(sizeof(core_log_sink_t));
memcpy(_core_log_bt_sink, sink, sizeof(core_log_sink_t));
_core_log_bt_log_level = CORE_LOG_BT_LOG_LEVEL_OFF;
core_log_bt_fini();
core_log_bt_init(msg_buffer_size, sink);
}
void core_log_bt_level_set(enum core_log_bt_log_level level)

View File

@ -36,10 +36,12 @@ void core_log_bt_core_api_set();
* @param sink Pointer to a log sink implementation. The caller owns the memory
* of this.
*/
void core_log_bt_init(const core_log_sink_t *sink);
void core_log_bt_init(
size_t msg_buffer_size,
const core_log_sink_t *sink);
// special version to use when switching loggers
void core_log_bt_reinit(const core_log_sink_t *sink);
void core_log_bt_reinit(size_t msg_buffer_size, const core_log_sink_t *sink);
/**
* Set the current logging level. This can be changed at any given time, e.g.