--- lbbs/src/log.c 2004/10/19 02:08:35 1.2 +++ lbbs/src/log.c 2025/05/26 12:06:05 1.18 @@ -1,111 +1,117 @@ /*************************************************************************** - 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 +#include +#include +#include +#include FILE *fp_log_std; FILE *fp_log_err; -int -log_begin (char *file_log_std, char *file_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 -1; - } - - return 0; -} - -int -log_end () -{ - 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 (char *msg) -{ - char buf[1024]; - - if (fp_log_std == NULL) - { - perror ("log_std failed\n"); - return -1; - } - - log_head(buf); - strcat(buf,msg); - - if (fprintf (fp_log_std, buf)<0) - { - perror ("log_std failed\n"); - return -2; - } - - fflush(fp_log_std); - - return 0; -} - -int -log_error (char *error_msg) -{ - char buf[1024]; - - if (fp_log_err == NULL) - { - perror ("log_error failed\n"); - return -1; - } - - log_head(buf); - strcat(buf,error_msg); - - if (fprintf (fp_log_err, buf)<0) - { - perror ("log_error failed\n"); - return -2; - } - - fflush(fp_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; + return 0; +} + +void log_end() +{ + fclose(fp_log_std); + fclose(fp_log_err); +} + +int log_head(char *buf, size_t len) +{ + 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(const char *format, ...) +{ + va_list args; + int retval; + char buf[1024]; + + log_head(buf, sizeof(buf)); + strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); + + 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]; + + log_head(buf, sizeof(buf)); + strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); + + 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; }