Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43452
  • 博文数量: 9
  • 博客积分: 1617
  • 博客等级: 上尉
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-21 00:56
文章分类
文章存档

2010年(9)

分类: C/C++

2010-04-29 18:03:12

这一部分是文件操作函数,自己放这里,以后可能能够用的上,一步步积累自己的函数,以后要用稍微改改o(∩_∩)o


/***************************************************
 * 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;
}



阅读(779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~