--- lbbs/src/log.c 2004/10/19 17:11:39 1.4 +++ lbbs/src/log.c 2025/06/04 14:01:29 1.21 @@ -1,113 +1,121 @@ /*************************************************************************** - 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 "log.h" +#include "io.h" #include "common.h" +#include +#include +#include +#include +#include -FILE *fp_log_std; -FILE *fp_log_err; +#define _POSIX_C_SOURCE 200809L +#include -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; - } +FILE *fp_common_log; +FILE *fp_error_log; - fp_log_err = fopen (file_log_err, "a"); - if (fp_log_err == NULL) - { - perror ("log_begin failed\n"); - return -2; - } +int log_begin(char *common_log_file, char *error_log_file) +{ + fp_common_log = fopen(common_log_file, "a"); + if (fp_common_log == NULL) + { + perror("log_begin failed\n"); + return -1; + } + + 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) +int log_head(char *buf, size_t len) { - 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; + time_t t; + struct tm gm_tm; + char s_time[256]; + + time(&t); + gmtime_r(&t, &gm_tm); + strftime(s_time, sizeof(s_time), "%Y-%m-%d %H:%M:%S", &gm_tm); + snprintf(buf, len, "[%s] [%d] ", s_time, getpid()); + + return 0; } -int -log_std (char *msg) +int log_common(const char *format, ...) { - char buf[1024]; + va_list args; + int retval; + char buf[LINE_BUFFER_LEN]; - if (fp_log_std == NULL) - { - perror ("log_std failed\n"); - return -1; - } + log_head(buf, sizeof(buf)); + strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); - log_head(buf); + va_start(args, format); + retval = vfprintf(fp_common_log, buf, args); + va_end(args); - strcat(buf,msg); - - if (fprintf (fp_log_std, buf)<0) - { - perror ("log_std failed\n"); - return -2; - } + fflush(fp_common_log); - fflush(fp_log_std); - - return 0; + return retval; } -int -log_error (char *error_msg) +int log_error(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]; - log_head(buf); + log_head(buf, sizeof(buf)); + strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); - strcat(buf,error_msg); + va_start(args, format); + retval = vfprintf(fp_error_log, buf, args); + va_end(args); - if (fprintf (fp_log_err, buf)<0) - { - perror ("log_error failed\n"); - return -2; - } - - fflush(fp_log_err); + fflush(fp_error_log); + + return retval; +} - return 0; +int log_common_redir(int fd) +{ + int ret; + close(fileno(fp_common_log)); + ret = dup2(fd, fileno(fp_common_log)); + return ret; +} + +int log_error_redir(int fd) +{ + int ret; + close(fileno(fp_error_log)); + ret = dup2(fd, fileno(fp_error_log)); + return ret; }