--- lbbs/src/log.c 2025/11/17 11:22:16 1.35 +++ lbbs/src/log.c 2025/12/19 06:16:27 1.38 @@ -27,8 +27,8 @@ enum _log_constant_t static char path_common_log[FILE_PATH_LEN]; static char path_error_log[FILE_PATH_LEN]; -static FILE *fp_common_log; -static FILE *fp_error_log; +static FILE *fp_common_log = NULL; +static FILE *fp_error_log = NULL; static int redir_common_log = 0; static int redir_error_log = 0; @@ -42,14 +42,16 @@ int log_begin(const char *common_log_fil fp_common_log = fopen(path_common_log, "a"); if (fp_common_log == NULL) { - fprintf(stderr, "fopen(%s) error: %d\n", path_common_log, errno); + fprintf(stderr, "fopen(%s) error: %s\n", path_common_log, strerror(errno)); return -1; } fp_error_log = fopen(path_error_log, "a"); if (fp_error_log == NULL) { - fprintf(stderr, "fopen(%s) error: %d\n", path_error_log, errno); + fprintf(stderr, "fopen(%s) error: %s\n", path_error_log, strerror(errno)); + fclose(fp_common_log); + fp_common_log = NULL; return -2; } @@ -61,15 +63,24 @@ int log_begin(const char *common_log_fil void log_end() { - fclose(fp_common_log); - fclose(fp_error_log); + if (fp_common_log) + { + fclose(fp_common_log); + fp_common_log = NULL; + } + if (fp_error_log) + { + fclose(fp_error_log); + fp_error_log = NULL; + } } -inline static void log_head(char *buf, size_t len, int log_level, const char *app_file, int app_line) +inline static int log_head(char *buf, size_t len, int log_level, const char *app_file, int app_line) { time_t t; struct tm gm_tm; char s_time[STR_LOG_TIME_MAX_LEN + 1]; + int ret; time(&t); gmtime_r(&t, &gm_tm); @@ -77,33 +88,68 @@ inline static void log_head(char *buf, s if (log_level == LOG_LEVEL_COMMON) { - snprintf(buf, len, "[%s] [%d] [INFO] ", s_time, getpid()); + ret = snprintf(buf, len, "[%s] [%d] [INFO] ", s_time, getpid()); + } + else if (log_level == LOG_LEVEL_ERROR) + { + ret = snprintf(buf, len, "[%s] [%d] [ERROR] [%s:%d] ", s_time, getpid(), app_file, app_line); } - else // if (log_level == LOG_LEVEL_ERROR) + else // if (log_level == LOG_LEVEL_DEBUG) { - snprintf(buf, len, "[%s] [%d] [ERROR] [%s:%d] ", s_time, getpid(), app_file, app_line); + ret = snprintf(buf, len, "[%s] [%d] [DEBUG] [%s:%d] ", s_time, getpid(), app_file, app_line); } + + return ret; } int log_printf(enum log_level_t log_level, const char *app_file, int app_line, const char *format, ...) { va_list args; - int retval; char buf[LINE_BUFFER_LEN]; FILE *fp_log; + int offset; + int ret; - fp_log = (log_level == LOG_LEVEL_ERROR ? fp_error_log : fp_common_log); + fp_log = (log_level == LOG_LEVEL_COMMON ? fp_common_log : fp_error_log); - log_head(buf, sizeof(buf), log_level, app_file, app_line); - strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); + offset = log_head(buf, sizeof(buf), log_level, app_file, app_line); va_start(args, format); - retval = vfprintf(fp_log, buf, args); + ret = vsnprintf(buf + offset, sizeof(buf) - (size_t)offset, format, args); va_end(args); - fflush(fp_log); + if (ret < 0) + { + // Encoding error + return -1; + } + else if (offset + ret + 1 >= sizeof(buf)) + { + buf[sizeof(buf) - 2] = '\n'; // Add newline for truncated messages + buf[sizeof(buf) - 1] = '\0'; // Ensure null termination + if (fputs(buf, fp_log) == EOF) + { + return -3; // Write error + } + ret = -2; // Indicate truncation + } + else + { + buf[offset + ret] = '\n'; // Add newline + buf[offset + ret + 1] = '\0'; // Ensure null termination + if (fputs(buf, fp_log) == EOF) + { + return -3; // Write error + } + ret = offset + ret + 1; // Return number of characters written (including newline) + } + + if (fflush(fp_log) == EOF) + { + return -4; // Flush error + } - return retval; + return ret; } int log_common_redir(int fd) @@ -120,30 +166,43 @@ int log_error_redir(int fd) int log_restart(void) { - FILE *fp; + FILE *fp_common = NULL; + FILE *fp_error = NULL; if (!redir_common_log) { - fp = fopen(path_common_log, "a"); - if (fp == NULL) + fp_common = fopen(path_common_log, "a"); + if (fp_common == NULL) { - log_error("fopen(%s) error: %d\n", path_common_log, errno); + log_error("fopen(%s) error: %s", path_common_log, strerror(errno)); return -1; } - fclose(fp_common_log); - fp_common_log = fp; } if (!redir_error_log) { - fp = fopen(path_error_log, "a"); - if (fp == NULL) + fp_error = fopen(path_error_log, "a"); + if (fp_error == NULL) { - log_error("fopen(%s) error: %d\n", path_error_log, errno); + log_error("fopen(%s) error: %s", path_error_log, strerror(errno)); + if (fp_common) + { + fclose(fp_common); + } return -2; } + } + + // Apply new file pointers + if (fp_common) + { + fclose(fp_common_log); + fp_common_log = fp_common; + } + if (fp_error) + { fclose(fp_error_log); - fp_error_log = fp; + fp_error_log = fp_error; } return 0;