From 3f4dab5fd6f602d048d03ea7d6fd6779798804dc Mon Sep 17 00:00:00 2001 From: icex2 Date: Thu, 15 Aug 2024 11:34:31 +0200 Subject: [PATCH] feat: Add helpers to easily setup the async logging sink --- src/main/core/log-bt-ext.c | 54 +++++++++++++++++++++++++++++++++----- src/main/core/log-bt-ext.h | 5 ++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/core/log-bt-ext.c b/src/main/core/log-bt-ext.c index 24cab6e..ca2d5bd 100644 --- a/src/main/core/log-bt-ext.c +++ b/src/main/core/log-bt-ext.c @@ -3,18 +3,24 @@ #include #include "core/log-bt.h" +#include "core/log-sink-async.h" #include "core/log-sink-debug.h" #include "core/log-sink-file.h" #include "core/log-sink-list.h" #include "core/log-sink-mutex.h" #include "core/log-sink-std.h" +// so we can log data dumps of rs232 streams without crashing +#define CORE_LOG_BT_EXT_MSG_BUFFER_SIZE 1024 * 64 +// 64 kb * 64 = 4 MB for logging total +#define CORE_LOG_BT_EXT_ASYNC_QUEUE_LENGTH 64 + void core_log_bt_ext_init_with_stdout() { core_log_sink_t sink; core_log_sink_std_out_open(true, &sink); - core_log_bt_init(&sink); + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink); } void core_log_bt_ext_init_with_stderr() @@ -22,7 +28,7 @@ void core_log_bt_ext_init_with_stderr() core_log_sink_t sink; core_log_sink_std_err_open(true, &sink); - core_log_bt_init(&sink); + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink); } void core_log_bt_ext_init_with_debug() @@ -30,7 +36,7 @@ void core_log_bt_ext_init_with_debug() core_log_sink_t sink; core_log_sink_debug_open(&sink); - core_log_bt_init(&sink); + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink); } void core_log_bt_ext_init_with_file( @@ -39,7 +45,7 @@ void core_log_bt_ext_init_with_file( core_log_sink_t sink; core_log_sink_file_open(path, append, rotate, max_rotations, &sink); - core_log_bt_init(&sink); + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink); } void core_log_bt_ext_init_with_stdout_and_file( @@ -55,7 +61,7 @@ void core_log_bt_ext_init_with_stdout_and_file( core_log_sink_mutex_open(&sink_composed, &sink_mutex); - core_log_bt_init(&sink_mutex); + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink_mutex); } void core_log_bt_ext_init_with_stderr_and_file( @@ -71,5 +77,41 @@ void core_log_bt_ext_init_with_stderr_and_file( core_log_sink_mutex_open(&sink_composed, &sink_mutex); - core_log_bt_init(&sink_mutex); + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink_mutex); +} + +void core_log_bt_ext_init_async_with_stderr() +{ + core_log_sink_t sink; + core_log_sink_t sink_async; + + core_log_sink_std_err_open(true, &sink); + + core_log_sink_async_open( + CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, + CORE_LOG_BT_EXT_ASYNC_QUEUE_LENGTH, + CORE_LOG_SINK_ASYNC_OVERFLOW_POLICY_DISCARD_NEW, + &sink, &sink_async); + + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink_async); +} + +void core_log_bt_ext_init_async_with_stderr_and_file( + const char *path, bool append, bool rotate, uint8_t max_rotations) +{ + core_log_sink_t sinks[2]; + core_log_sink_t sink_composed; + core_log_sink_t sink_async; + + core_log_sink_std_err_open(true, &sinks[0]); + core_log_sink_file_open(path, append, rotate, max_rotations, &sinks[1]); + core_log_sink_list_open(sinks, 2, &sink_composed); + + core_log_sink_async_open( + CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, + CORE_LOG_BT_EXT_ASYNC_QUEUE_LENGTH, + CORE_LOG_SINK_ASYNC_OVERFLOW_POLICY_DISCARD_NEW, + &sink_composed, &sink_async); + + core_log_bt_init(CORE_LOG_BT_EXT_MSG_BUFFER_SIZE, &sink_async); } \ No newline at end of file diff --git a/src/main/core/log-bt-ext.h b/src/main/core/log-bt-ext.h index 320c971..094fcf1 100644 --- a/src/main/core/log-bt-ext.h +++ b/src/main/core/log-bt-ext.h @@ -53,4 +53,9 @@ void core_log_bt_ext_init_with_stdout_and_file( void core_log_bt_ext_init_with_stderr_and_file( const char *path, bool append, bool rotate, uint8_t max_rotations); +void core_log_bt_ext_init_async_with_stderr(); + +void core_log_bt_ext_init_async_with_stderr_and_file( + const char *path, bool append, bool rotate, uint8_t max_rotations); + #endif \ No newline at end of file