--- lbbs/src/log.c 2025/04/28 03:30:59 1.11 +++ lbbs/src/log.c 2025/11/16 00:19:42 1.32 @@ -1,41 +1,43 @@ -/*************************************************************************** - 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; +enum _log_constant_t +{ + STR_LOG_TIME_MAX_LEN = 50, +}; -int log_begin(char *file_log_std, char *file_log_err) +static FILE *fp_common_log; +static FILE *fp_error_log; + +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) + fp_common_log = fopen(common_log_file, "a"); + if (fp_common_log == NULL) { perror("log_begin failed\n"); return -1; } - fp_log_err = fopen(file_log_err, "a"); - if (fp_log_err == NULL) + fp_error_log = fopen(error_log_file, "a"); + if (fp_error_log == NULL) { perror("log_begin failed\n"); return -2; @@ -44,72 +46,59 @@ int log_begin(char *file_log_std, char * return 0; } -int log_end() +void log_end() { - fclose(fp_log_std); - fclose(fp_log_err); + fclose(fp_common_log); + fclose(fp_error_log); } -int log_head(char *buf) +inline static void log_head(char *buf, size_t len, int log_level, const char *app_file, int app_line) { time_t t; - char s_time[256]; - t = time(0); + struct tm gm_tm; + char s_time[STR_LOG_TIME_MAX_LEN + 1]; - strftime(s_time, 256, "%Y-%m-%d %H:%M:%S", localtime(&t)); - sprintf(buf, "[%s] [%d] ", s_time, getpid()); + time(&t); + gmtime_r(&t, &gm_tm); + strftime(s_time, sizeof(s_time), "%Y-%m-%d %H:%M:%S", &gm_tm); - 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_std(const char *format, ...) +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[1024]; - - log_head(buf); - strcat(buf, format); + char buf[LINE_BUFFER_LEN]; + FILE *fp_log; - va_start(args, format); - retval = vfprintf(fp_log_std, buf, args); - va_end(args); - - fflush(fp_log_std); - - return retval; -} - -int log_error(const char *format, ...) -{ - va_list args; - int retval; - char buf[1024]; + fp_log = (log_level == LOG_LEVEL_ERROR ? fp_error_log : fp_common_log); - log_head(buf); - strcat(buf, format); + log_head(buf, sizeof(buf), log_level, app_file, app_line); + strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); va_start(args, format); - retval = vfprintf(fp_log_err, buf, args); + retval = vfprintf(fp_log, buf, args); va_end(args); - fflush(fp_log_err); + fflush(fp_log); return retval; } -int log_std_redirect(int fd) +int log_common_redir(int fd) { - int ret; - close(fileno(fp_log_std)); - ret = dup2(fd, fileno(fp_log_std)); - return ret; + return dup2(fd, fileno(fp_common_log)); } -int log_err_redirect(int fd) +int log_error_redir(int fd) { - int ret; - close(fileno(fp_log_err)); - ret = dup2(fd, fileno(fp_log_err)); - return ret; + return dup2(fd, fileno(fp_error_log)); }