--- lbbs/src/log.c 2004/10/19 15:22:01 1.3 +++ lbbs/src/log.c 2005/03/20 14:37:37 1.8 @@ -15,7 +15,10 @@ * * ***************************************************************************/ -#include "common.h" +#include "io.h" +#include +#include +#include FILE *fp_log_std; FILE *fp_log_err; @@ -34,7 +37,7 @@ log_begin (char *file_log_std, char *fil if (fp_log_err == NULL) { perror ("log_begin failed\n"); - return -1; + return -2; } return 0; @@ -55,59 +58,63 @@ log_head (char *buf) t = time(0); strftime(s_time,256,"%Y-%m-%d %H:%M:%S", localtime (&t)); - sprintf(buf,"[%s] [%d] ", s_time, getpid()); + sprintf(buf,"[%s] [%d] ", s_time, getpid ()); return 0; } int -log_std (char *msg) +log_std(const char * format, ...) { + va_list args; + int retval; char buf[1024]; - if (fp_log_std == NULL) - { - perror ("log_std failed\n"); - return -1; - } - log_head(buf); + strcat(buf, format); - strcat(buf,msg); - - if (fprintf (fp_log_std, buf)<0) - { - perror ("log_std failed\n"); - return -2; - } + va_start (args, format); + retval = vfprintf (fp_log_std, buf, args); + va_end (args); fflush(fp_log_std); - - return 0; + + return retval; } int -log_error (char *error_msg) +log_error (const char * format, ...) { + va_list args; + int retval; char buf[1024]; - - if (fp_log_err == NULL) - { - perror ("log_error failed\n"); - return -1; - } log_head(buf); + strcat(buf, format); - strcat(buf,error_msg); + va_start (args, format); + retval = vfprintf (fp_log_err, buf, args); + va_end (args); - if (fprintf (fp_log_err, buf)<0) - { - perror ("log_error failed\n"); - return -2; - } - fflush(fp_log_err); - return 0; + 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; }