| 1 |
/***************************************************************************
|
| 2 |
log.c - description
|
| 3 |
-------------------
|
| 4 |
begin : Mon Oct 18 2004
|
| 5 |
copyright : (C) 2004 by leaf
|
| 6 |
email : leaflet@leafok.com
|
| 7 |
***************************************************************************/
|
| 8 |
|
| 9 |
/***************************************************************************
|
| 10 |
* *
|
| 11 |
* This program is free software; you can redistribute it and/or modify *
|
| 12 |
* it under the terms of the GNU General Public License as published by *
|
| 13 |
* the Free Software Foundation; either version 2 of the License, or *
|
| 14 |
* (at your option) any later version. *
|
| 15 |
* *
|
| 16 |
***************************************************************************/
|
| 17 |
|
| 18 |
#include "io.h"
|
| 19 |
#include <stdio.h>
|
| 20 |
#include <string.h>
|
| 21 |
#include <stdarg.h>
|
| 22 |
#include <sys/types.h>
|
| 23 |
#include <time.h>
|
| 24 |
#include <unistd.h>
|
| 25 |
|
| 26 |
FILE *fp_log_std;
|
| 27 |
FILE *fp_log_err;
|
| 28 |
|
| 29 |
int log_begin(char *file_log_std, char *file_log_err)
|
| 30 |
{
|
| 31 |
fp_log_std = fopen(file_log_std, "a");
|
| 32 |
if (fp_log_std == NULL)
|
| 33 |
{
|
| 34 |
perror("log_begin failed\n");
|
| 35 |
return -1;
|
| 36 |
}
|
| 37 |
|
| 38 |
fp_log_err = fopen(file_log_err, "a");
|
| 39 |
if (fp_log_err == NULL)
|
| 40 |
{
|
| 41 |
perror("log_begin failed\n");
|
| 42 |
return -2;
|
| 43 |
}
|
| 44 |
|
| 45 |
return 0;
|
| 46 |
}
|
| 47 |
|
| 48 |
void log_end()
|
| 49 |
{
|
| 50 |
fclose(fp_log_std);
|
| 51 |
fclose(fp_log_err);
|
| 52 |
}
|
| 53 |
|
| 54 |
int log_head(char *buf)
|
| 55 |
{
|
| 56 |
time_t t;
|
| 57 |
char s_time[256];
|
| 58 |
t = time(0);
|
| 59 |
|
| 60 |
strftime(s_time, sizeof(s_time), "%Y-%m-%d %H:%M:%S", localtime(&t));
|
| 61 |
sprintf(buf, "[%s] [%d] ", s_time, getpid());
|
| 62 |
|
| 63 |
return 0;
|
| 64 |
}
|
| 65 |
|
| 66 |
int log_std(const char *format, ...)
|
| 67 |
{
|
| 68 |
va_list args;
|
| 69 |
int retval;
|
| 70 |
char buf[1024];
|
| 71 |
|
| 72 |
log_head(buf);
|
| 73 |
strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf)));
|
| 74 |
|
| 75 |
va_start(args, format);
|
| 76 |
retval = vfprintf(fp_log_std, buf, args);
|
| 77 |
va_end(args);
|
| 78 |
|
| 79 |
fflush(fp_log_std);
|
| 80 |
|
| 81 |
return retval;
|
| 82 |
}
|
| 83 |
|
| 84 |
int log_error(const char *format, ...)
|
| 85 |
{
|
| 86 |
va_list args;
|
| 87 |
int retval;
|
| 88 |
char buf[1024];
|
| 89 |
|
| 90 |
log_head(buf);
|
| 91 |
strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf)));
|
| 92 |
|
| 93 |
va_start(args, format);
|
| 94 |
retval = vfprintf(fp_log_err, buf, args);
|
| 95 |
va_end(args);
|
| 96 |
|
| 97 |
fflush(fp_log_err);
|
| 98 |
|
| 99 |
return retval;
|
| 100 |
}
|
| 101 |
|
| 102 |
int log_std_redirect(int fd)
|
| 103 |
{
|
| 104 |
int ret;
|
| 105 |
close(fileno(fp_log_std));
|
| 106 |
ret = dup2(fd, fileno(fp_log_std));
|
| 107 |
return ret;
|
| 108 |
}
|
| 109 |
|
| 110 |
int log_err_redirect(int fd)
|
| 111 |
{
|
| 112 |
int ret;
|
| 113 |
close(fileno(fp_log_err));
|
| 114 |
ret = dup2(fd, fileno(fp_log_err));
|
| 115 |
return ret;
|
| 116 |
}
|