--- lbbs/src/log.c 2004/10/21 17:28:46 1.5 +++ lbbs/src/log.c 2025/11/17 11:17:37 1.34 @@ -1,131 +1,149 @@ -/*************************************************************************** - 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 "log.h" +#include +#include +#include +#include +#include -FILE *fp_log_std; -FILE *fp_log_err; +enum _log_constant_t +{ + STR_LOG_TIME_MAX_LEN = 50, +}; + +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 int redir_common_log = 0; +static int redir_error_log = 0; -int -log_begin (char *file_log_std, char *file_log_err) +int log_begin(const char *common_log_file, const char *error_log_file) { - fp_log_std = fopen (file_log_std, "a"); - if (fp_log_std == NULL) - { - perror ("log_begin failed\n"); - return -1; - } + 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_log_err = fopen (file_log_err, "a"); - if (fp_log_err == NULL) - { - perror ("log_begin failed\n"); - return -2; - } + fp_common_log = fopen(path_common_log, "a"); + if (fp_common_log == NULL) + { + fprintf(stderr, "fopen(%s) error\n", path_common_log); + return -1; + } - return 0; -} + fp_error_log = fopen(path_error_log, "a"); + if (fp_error_log == NULL) + { + fprintf(stderr, "fopen(%s) error\n", path_error_log); + return -2; + } -int -log_end () -{ - fclose (fp_log_std); - fclose (fp_log_err); + redir_common_log = 0; + redir_error_log = 0; + + return 0; } -int -log_head (char *buf) +void log_end() { - 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; + fclose(fp_common_log); + fclose(fp_error_log); } -int -log_std (char *msg) +inline static void log_head(char *buf, size_t len, int log_level, const char *app_file, int app_line) { - char buf[1024]; - - if (fp_log_std == NULL) - { - perror ("log_std failed\n"); - return -1; - } - - log_head(buf); + time_t t; + struct tm gm_tm; + char s_time[STR_LOG_TIME_MAX_LEN + 1]; - strcat(buf,msg); - - if (fprintf (fp_log_std, buf)<0) - { - perror ("log_std failed\n"); - return -2; - } + time(&t); + gmtime_r(&t, &gm_tm); + strftime(s_time, sizeof(s_time), "%Y-%m-%d %H:%M:%S", &gm_tm); - fflush(fp_log_std); - - return 0; + if (log_level == LOG_LEVEL_COMMON) + { + snprintf(buf, len, "[%s] [%d] [INFO] ", s_time, getpid()); + } + else // if (log_level == LOG_LEVEL_ERROR) + { + snprintf(buf, len, "[%s] [%d] [ERROR] [%s:%d] ", s_time, getpid(), app_file, app_line); + } } -int -log_error (char *error_msg) +int log_printf(enum log_level_t log_level, const char *app_file, int app_line, const char *format, ...) { - char buf[1024]; - - if (fp_log_err == NULL) - { - perror ("log_error failed\n"); - return -1; - } + va_list args; + int retval; + char buf[LINE_BUFFER_LEN]; + FILE *fp_log; - log_head(buf); + fp_log = (log_level == LOG_LEVEL_ERROR ? fp_error_log : fp_common_log); - strcat(buf,error_msg); + log_head(buf, sizeof(buf), log_level, app_file, app_line); + strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); - if (fprintf (fp_log_err, buf)<0) - { - perror ("log_error failed\n"); - return -2; - } - - fflush(fp_log_err); + va_start(args, format); + retval = vfprintf(fp_log, buf, args); + va_end(args); - return 0; + fflush(fp_log); + + return retval; +} + +int log_common_redir(int fd) +{ + redir_common_log = 1; + return dup2(fd, fileno(fp_common_log)); } -int -log_std_redirect(int fd) +int log_error_redir(int fd) { - int ret; - close (fileno(fp_log_std)); - ret = dup2(fd, fileno(fp_log_std)); - return ret; + redir_error_log = 1; + return dup2(fd, fileno(fp_error_log)); } -int -log_err_redirect(int fd) +int log_restart(void) { - int ret; - close (fileno(fp_log_err)); - ret = dup2(fd, fileno(fp_log_err)); - return ret; + FILE *fp; + + if (!redir_common_log) + { + fp = fopen(path_common_log, "a"); + if (fp == NULL) + { + log_error("fopen(%s) error\n", path_common_log); + return -1; + } + fclose(fp_common_log); + fp_common_log = fp; + } + + if (!redir_error_log) + { + fp = fopen(path_error_log, "a"); + if (fp == NULL) + { + log_error("fopen(%s) error\n", path_error_log); + return -2; + } + fclose(fp_error_log); + fp_error_log = fp; + } + + return 0; }