--- lbbs/src/log.c 2005/03/20 17:37:14 1.9 +++ lbbs/src/log.c 2025/06/21 02:15:18 1.26 @@ -1,120 +1,111 @@ /*************************************************************************** - log.c - description - ------------------- - begin : Mon Oct 18 2004 - copyright : (C) 2004 by leaf - email : leaflet@leafok.com + log.c - description + ------------------- + Copyright : (C) 2004-2025 by Leaflet + 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 * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ +#include "common.h" #include "io.h" -#include +#include "log.h" #include +#include #include +#include +#include -FILE *fp_log_std; -FILE *fp_log_err; +#define 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(char *common_log_file, char *error_log_file) { - fp_log_std = fopen (file_log_std, "a"); - if (fp_log_std == NULL) - { - perror ("log_begin failed\n"); - return -1; - } + 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) - { - perror ("log_begin failed\n"); - return -2; - } + fp_error_log = fopen(error_log_file, "a"); + if (fp_error_log == NULL) + { + perror("log_begin failed\n"); + return -2; + } - return 0; + 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); + time_t t; + 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(int 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); - - va_start (args, format); - retval = vfprintf (fp_log_std, buf, args); - va_end (args); - - fflush (fp_log_std); + va_list args; + int retval; + char buf[LINE_BUFFER_LEN]; + FILE *fp_log; - 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); - va_end (args); + va_start(args, format); + retval = vfprintf(fp_log, buf, args); + va_end(args); - fflush (fp_log_err); + fflush(fp_log); - return retval; + 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; + int ret; + close(fileno(fp_common_log)); + ret = dup2(fd, fileno(fp_common_log)); + return ret; } -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; + int ret; + close(fileno(fp_error_log)); + ret = dup2(fd, fileno(fp_error_log)); + return ret; }