From 47fe30aa8e10326518956d7c4e3c28032d28011e Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Mon, 28 Sep 2026 00:08:17 -0700 Subject: [PATCH 1/3] fix(hlog): logger_set_file switches an already-open log file logger_set_file only copied the new path. Once the logger had written a line, fp_ stayed open on the old file and logfile_shift reopened only on a day change, so a later hlog_set_file had no effect until midnight. It now closes the current file under the logger mutex; the next write opens the new one. logger_init initializes the mutex before its own logger_set_file call. unittest/hlog_test.c checks the switch after the first write. Co-Authored-By: Claude Opus 5.5 --- base/hlog.c | 11 ++++++++++- unittest/hlog_test.c | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/base/hlog.c b/base/hlog.c index 092fe6ae0..8173e2ac1 100644 --- a/base/hlog.c +++ b/base/hlog.c @@ -107,10 +107,10 @@ static void logger_init(logger_t* logger) { logger->truncate_percent = DEFAULT_LOG_TRUNCATE_PERCENT; logger->remain_days = DEFAULT_LOG_REMAIN_DAYS; logger->enable_fsync = 1; - logger_set_file(logger, DEFAULT_LOG_FILE); logger->last_logfile_ts = 0; logger->can_write_cnt = -1; hmutex_init(&logger->mutex_); + logger_set_file(logger, DEFAULT_LOG_FILE); } logger_t* logger_create() { @@ -193,12 +193,21 @@ void logger_enable_color(logger_t* logger, int on) { } void logger_set_file(logger_t* logger, const char* filepath) { + hmutex_lock(&logger->mutex_); strncpy(logger->filepath, filepath, sizeof(logger->filepath) - 1); // remove suffix .log char* suffix = strrchr(logger->filepath, '.'); if (suffix && strcmp(suffix, ".log") == 0) { *suffix = '\0'; } + // close the current logfile, the next write opens the new one + if (logger->fp_) { + fclose(logger->fp_); + logger->fp_ = NULL; + } + logger->last_logfile_ts = 0; + logger->can_write_cnt = -1; + hmutex_unlock(&logger->mutex_); } void logger_set_max_filesize(logger_t* logger, unsigned long long filesize) { diff --git a/unittest/hlog_test.c b/unittest/hlog_test.c index 38da63aa5..3d4b696a1 100644 --- a/unittest/hlog_test.c +++ b/unittest/hlog_test.c @@ -1,3 +1,6 @@ +#include +#include + #include "hlog.h" int main(int argc, char* argv[]) { @@ -18,5 +21,14 @@ int main(int argc, char* argv[]) { hloge("%s", "show error"); hlogf("%s", "show fatal"); + // test switch log file after the first write + hlog_set_file("hlog_test_switch.log"); + hlogi("%s", "show info in the switched file"); + const char* curfile = hlog_get_cur_file(); + if (strstr(curfile, "hlog_test_switch") == NULL) { + fprintf(stderr, "hlog_set_file did not switch the log file: %s\n", curfile); + return 1; + } + return 0; } From 6ca0f48ab9f3ddcf6070e65c5215a77d9325c012 Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Mon, 28 Sep 2026 00:14:13 -0700 Subject: [PATCH 2/3] fix(hlog): NUL-terminate filepath in logger_set_file The logger is malloc'd, so a path of sizeof(filepath) - 1 characters or more left filepath unterminated for the strrchr/strcmp that follow. Reword the switch test's comment: it checks the switch once the log file is open. Co-Authored-By: Claude Opus 5.5 --- base/hlog.c | 1 + unittest/hlog_test.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/base/hlog.c b/base/hlog.c index 8173e2ac1..5eed04bc0 100644 --- a/base/hlog.c +++ b/base/hlog.c @@ -195,6 +195,7 @@ void logger_enable_color(logger_t* logger, int on) { void logger_set_file(logger_t* logger, const char* filepath) { hmutex_lock(&logger->mutex_); strncpy(logger->filepath, filepath, sizeof(logger->filepath) - 1); + logger->filepath[sizeof(logger->filepath) - 1] = '\0'; // remove suffix .log char* suffix = strrchr(logger->filepath, '.'); if (suffix && strcmp(suffix, ".log") == 0) { diff --git a/unittest/hlog_test.c b/unittest/hlog_test.c index 3d4b696a1..1711620bb 100644 --- a/unittest/hlog_test.c +++ b/unittest/hlog_test.c @@ -21,7 +21,7 @@ int main(int argc, char* argv[]) { hloge("%s", "show error"); hlogf("%s", "show fatal"); - // test switch log file after the first write + // test switch log file after the log file is opened hlog_set_file("hlog_test_switch.log"); hlogi("%s", "show info in the switched file"); const char* curfile = hlog_get_cur_file(); From c5bdb901d4f3fd1a863d2314a21c7e77c530eeaf Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Mon, 28 Sep 2026 01:14:04 -0700 Subject: [PATCH 3/3] fix(hlog): logger_set_file clears cur_logfile with the closed file logger_get_cur_file returned the closed file's name until the next write reopened one; it now reads empty until then. Co-Authored-By: Claude Opus 5.5 --- base/hlog.c | 1 + 1 file changed, 1 insertion(+) diff --git a/base/hlog.c b/base/hlog.c index 5eed04bc0..19a456005 100644 --- a/base/hlog.c +++ b/base/hlog.c @@ -206,6 +206,7 @@ void logger_set_file(logger_t* logger, const char* filepath) { fclose(logger->fp_); logger->fp_ = NULL; } + logger->cur_logfile[0] = '\0'; logger->last_logfile_ts = 0; logger->can_write_cnt = -1; hmutex_unlock(&logger->mutex_);