diff --git a/doc/release_notes.md b/doc/release_notes.md new file mode 100644 index 0000000..dfedaf7 --- /dev/null +++ b/doc/release_notes.md @@ -0,0 +1,30 @@ +# Release Notes + +| date | version | +| ---- | ---- | +| 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 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..762d8d7 --- /dev/null +++ b/test/long_message/test_long_message.c @@ -0,0 +1,99 @@ +#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); + +#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() +{ + UNITY_BEGIN(); + + RUN_TEST(test_long_message); + + return UNITY_END(); +} 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