日志-demo

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>

typedef struct data {
  char *str;
  long time;
  int symbal;
} DATA;

int64_t getCurrentTime()
{
  struct timeval tv;
  gettimeofday(&tv,NULL); 
  return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

int write_log(char *c){
  FILE *fp;
  ong curTime = getCurrentTime();
  int line_count;

DATA *pt;
pt = (DATA *)malloc(sizeof(DATA)); 
// memcpy(pt->str, c, strlen(c)); 用数组的时候采用这种写法
pt->str = c;
pt->time = curTime;
pt->symbal = ;
printf("%s\n", pt->str);
fp=fopen("system.log", "r+");
if(NULL == fp) {
  fprintf(stderr,"open file system.log error! %s\n",strerror(errno));
  //doSystem("touch xxxfile.log");
  return 0;
}

while((line_count = fgetc(fp)) != EOF)
{
  if(line_count == '\n') {
  line_count++;
}

if (line_count < 100) {
  //fseek(fp, sizeof(*fp), SEEK_END);
  fwrite(pt,sizeof(DATA),1,fp);
} else {
  //doSystem(" sed -i '0,49d' system.log ");
  fwrite(pt,sizeof(DATA),1,fp);
}

free(pt);
fclose(fp);
return 0;
}


int main(int argc, char **argv)
{
  char *p = "As food is to the body";
  int ret = write_log(p);
  printf("%d\n", ret);
  return ret;
}

猜你喜欢

转载自www.cnblogs.com/Sam-2018/p/log.html