| 15 |
* * |
* * |
| 16 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
|
|
| 18 |
#include "common.h" |
#include "io.h" |
| 19 |
|
#include <stdio.h> |
| 20 |
|
#include <stdarg.h> |
| 21 |
|
#include <time.h> |
| 22 |
|
|
| 23 |
FILE *fp_log_std; |
FILE *fp_log_std; |
| 24 |
FILE *fp_log_err; |
FILE *fp_log_err; |
| 37 |
if (fp_log_err == NULL) |
if (fp_log_err == NULL) |
| 38 |
{ |
{ |
| 39 |
perror ("log_begin failed\n"); |
perror ("log_begin failed\n"); |
| 40 |
return -1; |
return -2; |
| 41 |
} |
} |
| 42 |
|
|
| 43 |
return 0; |
return 0; |
| 58 |
t = time(0); |
t = time(0); |
| 59 |
|
|
| 60 |
strftime(s_time,256,"%Y-%m-%d %H:%M:%S", localtime (&t)); |
strftime(s_time,256,"%Y-%m-%d %H:%M:%S", localtime (&t)); |
| 61 |
sprintf(buf,"[%s] [%d] ", s_time, getpid()); |
sprintf(buf,"[%s] [%d] ", s_time, getpid ()); |
| 62 |
|
|
| 63 |
return 0; |
return 0; |
| 64 |
} |
} |
| 65 |
|
|
| 66 |
int |
int |
| 67 |
log_std (char *msg) |
log_std(const char * format, ...) |
| 68 |
{ |
{ |
| 69 |
|
va_list args; |
| 70 |
|
int retval; |
| 71 |
char buf[1024]; |
char buf[1024]; |
| 72 |
|
|
|
if (fp_log_std == NULL) |
|
|
{ |
|
|
perror ("log_std failed\n"); |
|
|
return -1; |
|
|
} |
|
|
|
|
| 73 |
log_head(buf); |
log_head(buf); |
| 74 |
strcat(buf,msg); |
strcat(buf, format); |
| 75 |
|
|
| 76 |
if (fprintf (fp_log_std, buf)<0) |
va_start (args, format); |
| 77 |
{ |
retval = vfprintf (fp_log_std, buf, args); |
| 78 |
perror ("log_std failed\n"); |
va_end (args); |
|
return -2; |
|
|
} |
|
| 79 |
|
|
| 80 |
fflush(fp_log_std); |
fflush(fp_log_std); |
| 81 |
|
|
| 82 |
return 0; |
return retval; |
| 83 |
} |
} |
| 84 |
|
|
| 85 |
int |
int |
| 86 |
log_error (char *error_msg) |
log_error (const char * format, ...) |
| 87 |
{ |
{ |
| 88 |
|
va_list args; |
| 89 |
|
int retval; |
| 90 |
char buf[1024]; |
char buf[1024]; |
|
|
|
|
if (fp_log_err == NULL) |
|
|
{ |
|
|
perror ("log_error failed\n"); |
|
|
return -1; |
|
|
} |
|
| 91 |
|
|
| 92 |
log_head(buf); |
log_head(buf); |
| 93 |
strcat(buf,error_msg); |
strcat(buf, format); |
| 94 |
|
|
| 95 |
|
va_start (args, format); |
| 96 |
|
retval = vfprintf (fp_log_err, buf, args); |
| 97 |
|
va_end (args); |
| 98 |
|
|
|
if (fprintf (fp_log_err, buf)<0) |
|
|
{ |
|
|
perror ("log_error failed\n"); |
|
|
return -2; |
|
|
} |
|
|
|
|
| 99 |
fflush(fp_log_err); |
fflush(fp_log_err); |
| 100 |
|
|
| 101 |
return 0; |
return retval; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
int |
| 105 |
|
log_std_redirect(int fd) |
| 106 |
|
{ |
| 107 |
|
int ret; |
| 108 |
|
close (fileno(fp_log_std)); |
| 109 |
|
ret = dup2(fd, fileno(fp_log_std)); |
| 110 |
|
return ret; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
int |
| 114 |
|
log_err_redirect(int fd) |
| 115 |
|
{ |
| 116 |
|
int ret; |
| 117 |
|
close (fileno(fp_log_err)); |
| 118 |
|
ret = dup2(fd, fileno(fp_log_err)); |
| 119 |
|
return ret; |
| 120 |
} |
} |