/***************************************************
* file name: pubfun_I_O.c creat on 29-4-2010 *
* ----------------------------------------------- *
* function: a collection of the I/O handle funct- *
* ions. *
* ------------------------------------------------*
* author: Wayne Lee company: Congine. Suzhou. *
* *************************************************/
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MEM_MAX 1024
#define GATE 8388608L
#define STR_MAX 200
/* statement of the string handle functions */
int rec_log(char*, int, const char*, ...); //record the error message anytime there exits errors.
//record the error message anytime there exits errors.
// the start of the rec_log function.
int rec_log(char* fn, int lin, const char* fmt, ...) {
long fd;
char cpid[MEM_MAX];
char line[MEM_MAX];
char lp[MEM_MAX];
char argbuf[MEM_MAX];
char cmdbuf[MEM_MAX];
time_t timep;
struct tm *p;
struct stat fst;
va_list ap;
memset(lp, 0x00, sizeof(lp));
memset(argbuf, 0x00, sizeof(argbuf));
memset(cmdbuf, 0x00, sizeof(cmdbuf));
memset(line, 0x00, sizeof(line));
sprintf(line, "%d", lin);
va_start(ap, fmt);
vsprintf(argbuf, fmt, ap);
va_end(ap);
time(&timep);
p = localtime(&timep);
strcat(lp, "/home/bsp/train2010/liw/log/err_d.log");
sprintf(cmdbuf, "mv /home/bsp/train2010/liw/log/err_d.log /home/bsp/train2010/liw/log/err_d[%d%d%d%d%d%d].log", (1990+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
sprintf(cpid, "%ld", getpid());
fd = open(lp, O_RDWR | O_APPEND | O_CREAT, 0666);
if (fd == -1) {
printf("Sorry for failed open! the error is: [%s]\n", strerror(errno));
exit(1);
}
write(fd, "--------------------------------------------------\n", 51);
write(fd, &cpid, strlen(cpid));
write(fd, "\t", sizeof("\t"));
write(fd, fn, strlen(fn));
write(fd, "\t", sizeof("\t"));
write(fd, line, strlen(line));
write(fd, "\n", sizeof("\n"));
write(fd, argbuf, sizeof(argbuf));
write(fd, "\n--------------------------------------------------\n", 52);
stat(lp, &fst);
if (fst.st_size > GATE - 512) {
system(cmdbuf);
}
close(fd);
return 0;
}
|