Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
MuggleWei committed Sep 5, 2024
2 parents a8267cb + 8eb705a commit 8fe45d8
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
30 changes: 30 additions & 0 deletions doc/release_notes.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions haclog/haclog_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions haclog/haclog_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions haclog/haclog_vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
99 changes: 99 additions & 0 deletions test/long_message/test_long_message.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "haclog/haclog.h"
#include "unity.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#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();
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.0
0.3.1

0 comments on commit 8fe45d8

Please sign in to comment.