From 454e5759248334538ff0ee962fa79f443d1f62ff Mon Sep 17 00:00:00 2001 From: Muggle Wei Date: Thu, 5 Sep 2024 10:40:25 +0800 Subject: [PATCH 1/4] fix: long log line overflow --- haclog/haclog_context.c | 6 +- haclog/haclog_log.c | 4 +- haclog/haclog_vsprintf.c | 21 ++++++ test/long_message/test_long_message.c | 94 +++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 test/long_message/test_long_message.c diff --git a/haclog/haclog_context.c b/haclog/haclog_context.c index a102989..f787527 100644 --- a/haclog/haclog_context.c +++ b/haclog/haclog_context.c @@ -91,9 +91,9 @@ unsigned long haclog_context_get_bytes_buf_size() void haclog_context_set_msg_buf_size(unsigned long bufsize) { - if (bufsize < HACLOG_DEFAULT_MSG_BUF_SIZE) { - bufsize = HACLOG_DEFAULT_MSG_BUF_SIZE; - } + // if (bufsize < HACLOG_DEFAULT_MSG_BUF_SIZE) { + // bufsize = HACLOG_DEFAULT_MSG_BUF_SIZE; + // } haclog_context_t *ctx = haclog_context_get(); ctx->msg_buf_size = bufsize; } diff --git a/haclog/haclog_log.c b/haclog/haclog_log.c index e429950..b4d650e 100644 --- a/haclog/haclog_log.c +++ b/haclog/haclog_log.c @@ -100,8 +100,8 @@ static haclog_thread_ret_t s_haclog_backend_func(void *args) } unsigned long bufsize = ctx->msg_buf_size; - if (bufsize < 2048) { - bufsize = 2048; + if (bufsize < 8) { + bufsize = 8; } char *buf = (char *)malloc(bufsize); diff --git a/haclog/haclog_vsprintf.c b/haclog/haclog_vsprintf.c index 26cb5e2..6303aff 100644 --- a/haclog/haclog_vsprintf.c +++ b/haclog/haclog_vsprintf.c @@ -1373,8 +1373,29 @@ int haclog_printf_primitive_format(haclog_bytes_buffer_t *bytes_buf, } break; } + // NOTE: + // snprintf return the number of caracters would have been written to + // the fianl string **if enough space had been available** + // so need to manual compare n and buf_remain!!! + // + // e.g. + // char buf[4]; + // memset(buf, 0, sizeof(buf)); + // int n = snprintf(buf, sizeof(buf), "%s", "0123456789"); + // + // * the result: + // buf = "012\0" + // n = 10 + if (n > buf_remain) { + n = buf_remain; + } + total_bytes += n; buf_remain -= n; + + if (buf_remain <= 0) { + break; + } } if (fmt_pos != primitive->fmt_len) { diff --git a/test/long_message/test_long_message.c b/test/long_message/test_long_message.c new file mode 100644 index 0000000..f92b3be --- /dev/null +++ b/test/long_message/test_long_message.c @@ -0,0 +1,94 @@ +#include "haclog/haclog.h" +#include "unity.h" +#include +#include +#include + +#define LINE_SIZE 8 +#define FILE_PATH "logs/test_long_message.log" + +void setUp() +{ +} + +void tearDown() +{ +} + +int my_write_meta(struct haclog_handler *handler, haclog_meta_info_t *meta) +{ + // do nothing + HACLOG_UNUSED(handler); + HACLOG_UNUSED(meta); + return 0; +} + +void add_console_handler() +{ + static haclog_console_handler_t handler; + memset(&handler, 0, sizeof(handler)); + if (haclog_console_handler_init(&handler, 1) != 0) { + fprintf(stderr, "failed init console handler"); + exit(EXIT_FAILURE); + } + + haclog_handler_set_level((haclog_handler_t *)&handler, HACLOG_LEVEL_INFO); + haclog_handler_set_fn_write_meta((haclog_handler_t *)&handler, + my_write_meta); + haclog_context_add_handler((haclog_handler_t *)&handler); +} + +void add_file_handler() +{ + static haclog_file_handler_t handler; + memset(&handler, 0, sizeof(handler)); + if (haclog_file_handler_init(&handler, FILE_PATH, "w") != 0) { + fprintf(stderr, "failed init file handler"); + exit(EXIT_FAILURE); + } + haclog_handler_set_level((haclog_handler_t *)&handler, HACLOG_LEVEL_DEBUG); + haclog_handler_set_fn_write_meta((haclog_handler_t *)&handler, + my_write_meta); + haclog_context_add_handler((haclog_handler_t *)&handler); +} + +void write_log() +{ + add_console_handler(); + add_file_handler(); + haclog_backend_run(); + + haclog_context_set_msg_buf_size(LINE_SIZE); + char buf[LINE_SIZE * 2]; + for (int i = 0; i < LINE_SIZE * 2 - 1; ++i) { + buf[i] = '0'; + } + buf[LINE_SIZE * 2 - 1] = '\0'; + + haclog_thread_context_init(); + + HACLOG_INFO("%s", buf); + + haclog_thread_context_cleanup(); +} + +void test_long_message() +{ + write_log(); + + FILE *fp = fopen(FILE_PATH, "rb"); + fseek(fp, 0L, SEEK_END); + long n = ftell(fp); + fclose(fp); + + TEST_ASSERT_TRUE(n <= LINE_SIZE); +} + +int main() +{ + UNITY_BEGIN(); + + RUN_TEST(test_long_message); + + return UNITY_END(); +} From 600876f197829e1970a85f2861d95fb15e31ed58 Mon Sep 17 00:00:00 2001 From: Muggle Wei Date: Thu, 5 Sep 2024 10:51:56 +0800 Subject: [PATCH 2/4] update test_long_log in windows --- test/long_message/test_long_message.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/long_message/test_long_message.c b/test/long_message/test_long_message.c index f92b3be..762d8d7 100644 --- a/test/long_message/test_long_message.c +++ b/test/long_message/test_long_message.c @@ -81,7 +81,12 @@ void test_long_message() long n = ftell(fp); fclose(fp); +#if HACLOG_PLATFORM_WINDOWS + // NOTE: in windows, new line is '\r''\n' + TEST_ASSERT_TRUE(n - 1 <= LINE_SIZE); +#else TEST_ASSERT_TRUE(n <= LINE_SIZE); +#endif } int main() From d933ee16cb8ec80b6bc6d4cd36bf049991ef697e Mon Sep 17 00:00:00 2001 From: Muggle Wei Date: Thu, 5 Sep 2024 10:57:45 +0800 Subject: [PATCH 3/4] update version to 0.3.1 --- doc/release_notes.md | 9 +++++++++ version.txt | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 doc/release_notes.md diff --git a/doc/release_notes.md b/doc/release_notes.md new file mode 100644 index 0000000..1ec0f11 --- /dev/null +++ b/doc/release_notes.md @@ -0,0 +1,9 @@ +# Release Notes + +| date | version | +| ---- | ---- | +| 2024-09-05 | 0.3.1| + +--- +## v0.3.1 +* fix: long log line overflow diff --git a/version.txt b/version.txt index 0d91a54..9e11b32 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.3.0 +0.3.1 From 8eb705a8f68a34c83f05dcc3d7d3c6b9e46e0883 Mon Sep 17 00:00:00 2001 From: Muggle Wei Date: Thu, 5 Sep 2024 11:13:36 +0800 Subject: [PATCH 4/4] update doc/release_notes --- doc/release_notes.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/doc/release_notes.md b/doc/release_notes.md index 1ec0f11..dfedaf7 100644 --- a/doc/release_notes.md +++ b/doc/release_notes.md @@ -2,8 +2,29 @@ | date | version | | ---- | ---- | -| 2024-09-05 | 0.3.1| +| 2024-01-11 | v0.1.5 | +| 2024-01-13 | v0.1.6 | +| 2024-03-22 | v0.2.0 | +| 2024-08-21 | v0.3.0 | +| 2024-09-05 | v0.3.1 | --- ## v0.3.1 * fix: long log line overflow + +--- +## v0.3.0 +* update: allow to disable thread context auto initialize + +--- +## v0.2.0 +* update: support callback function before backend run + +--- +## v0.1.6 +* update: hint non-standard(c11) '%lf' specifier +* fix: serialize '%%' + +--- +## v0.1.5 +* fix: haclog context init