--- lbbs/src/log.c 2005/03/20 17:37:14 1.9 +++ lbbs/src/log.c 2025/12/18 14:47:00 1.37 @@ -1,120 +1,207 @@ -/*************************************************************************** - log.c - description - ------------------- - begin : Mon Oct 18 2004 - copyright : (C) 2004 by leaf - email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * log + * - logger + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "common.h" #include "io.h" -#include +#include "log.h" +#include #include +#include #include +#include +#include -FILE *fp_log_std; -FILE *fp_log_err; - -int -log_begin (char *file_log_std, char *file_log_err) -{ - fp_log_std = fopen (file_log_std, "a"); - if (fp_log_std == NULL) - { - perror ("log_begin failed\n"); - return -1; - } - - fp_log_err = fopen (file_log_err, "a"); - if (fp_log_err == NULL) - { - perror ("log_begin failed\n"); - return -2; - } - - return 0; -} - -int -log_end () +enum _log_constant_t { - fclose (fp_log_std); - fclose (fp_log_err); -} - -int -log_head (char *buf) -{ - time_t t; - char s_time[256]; - t = time (0); - - strftime (s_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&t)); - sprintf (buf, "[%s] [%d] ", s_time, getpid ()); - - return 0; -} - -int -log_std (const char *format, ...) -{ - va_list args; - int retval; - char buf[1024]; - - log_head (buf); - strcat (buf, format); + STR_LOG_TIME_MAX_LEN = 50, +}; - va_start (args, format); - retval = vfprintf (fp_log_std, buf, args); - va_end (args); +static char path_common_log[FILE_PATH_LEN]; +static char path_error_log[FILE_PATH_LEN]; +static FILE *fp_common_log = NULL; +static FILE *fp_error_log = NULL; +static int redir_common_log = 0; +static int redir_error_log = 0; + +int log_begin(const char *common_log_file, const char *error_log_file) +{ + strncpy(path_common_log, common_log_file, sizeof(path_common_log) - 1); + path_common_log[sizeof(path_common_log) - 1] = '\0'; + strncpy(path_error_log, error_log_file, sizeof(path_error_log) - 1); + path_error_log[sizeof(path_error_log) - 1] = '\0'; + + fp_common_log = fopen(path_common_log, "a"); + if (fp_common_log == NULL) + { + 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: %s\n", path_error_log, strerror(errno)); + fclose(fp_common_log); + fp_common_log = NULL; + return -2; + } + + redir_common_log = 0; + redir_error_log = 0; + + return 0; +} + +void log_end() +{ + 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 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); + strftime(s_time, sizeof(s_time), "%Y-%m-%d %H:%M:%S", &gm_tm); + + if (log_level == LOG_LEVEL_COMMON) + { + 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_DEBUG) + { + 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; + char buf[LINE_BUFFER_LEN]; + FILE *fp_log; + int offset; + int ret; + + fp_log = (log_level == LOG_LEVEL_COMMON ? fp_common_log : fp_error_log); + + offset = log_head(buf, sizeof(buf), log_level, app_file, app_line); + + va_start(args, format); + ret = vsnprintf(buf + offset, sizeof(buf) - (size_t)offset, format, args); + va_end(args); + + if (ret < 0) + { + // Encoding error + return -1; + } + else if (offset + ret >= 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 + { + if (fputs(buf, fp_log) == EOF) + { + return -3; // Write error + } + ret = offset + ret; + } + + if (fflush(fp_log) == EOF) + { + return -4; // Flush error + } + + return ret; +} + +int log_common_redir(int fd) +{ + redir_common_log = 1; + return dup2(fd, fileno(fp_common_log)); +} + +int log_error_redir(int fd) +{ + redir_error_log = 1; + return dup2(fd, fileno(fp_error_log)); +} + +int log_restart(void) +{ + FILE *fp_common = NULL; + FILE *fp_error = NULL; + + if (!redir_common_log) + { + fp_common = fopen(path_common_log, "a"); + if (fp_common == NULL) + { + log_error("fopen(%s) error: %s\n", path_common_log, strerror(errno)); + return -1; + } + } + + if (!redir_error_log) + { + fp_error = fopen(path_error_log, "a"); + if (fp_error == NULL) + { + log_error("fopen(%s) error: %s\n", 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_error; + } - fflush (fp_log_std); - - return retval; -} - -int -log_error (const char *format, ...) -{ - va_list args; - int retval; - char buf[1024]; - - log_head (buf); - strcat (buf, format); - - va_start (args, format); - retval = vfprintf (fp_log_err, buf, args); - va_end (args); - - fflush (fp_log_err); - - return retval; -} - -int -log_std_redirect (int fd) -{ - int ret; - close (fileno (fp_log_std)); - ret = dup2 (fd, fileno (fp_log_std)); - return ret; -} - -int -log_err_redirect (int fd) -{ - int ret; - close (fileno (fp_log_err)); - ret = dup2 (fd, fileno (fp_log_err)); - return ret; + return 0; }