/* * init_running_print, if sucess then return fp, else return NULL */ FILE * init_running_print() { int i=0, k=0; FILE *fp, *fptmp; char line[LEN_10240] = {0}; char filename[LEN_128] = {0}; /* will print tail*/ conf.print_tail = 1; fp = fopen(conf.output_file_path, "r");//读取/var/log/tsar.data文件 if (!fp) { do_debug(LOG_FATAL, "unable to open the log file %s\n", conf.output_file_path); } /*log number to use for print*/ conf.print_file_number = -1; /* find start offset will print from tsar.data */ k=find_offset_from_start(fp, i);//在tsar.data文件中,用二分查找,找出要开始打印的行的位置放在print_file_number。 if (k == 1) {//如果返回1,代表当前的文件里面没有数据,数据在后面的文件中。用同样的方法扫描后面的文件。 /*find all possible record*/ for (i=1; ; i++) { memset(filename, 0, sizeof(filename)); sprintf(filename, "%s.%d", conf.output_file_path, i); fptmp = fopen(filename, "r"); if (!fptmp) { conf.print_file_number = i - 1; break; } k=find_offset_from_start(fptmp, i); if (k==0 || k==4) { if (fclose(fp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } fp=fptmp; break; } if (k== 2) { if (fseek(fp, 0, SEEK_SET) != 0) { do_debug(LOG_FATAL, "fseek error:%s", strerror(errno)); } if (fclose(fptmp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } break; } if (k == 1) { if (fclose(fp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } fp=fptmp; continue; } if (k == 5 || k == 6) { do_debug(LOG_FATAL, "log format error or find_offset_from_start have a bug. error code=%d\n", k); } } } if (k == 5 || k == 6) { do_debug(LOG_FATAL, "log format error or find_offset_from_start have a bug. error code=%d\n", k); } /* get record */ if (!fgets(line, LEN_10240, fp)) { do_debug(LOG_FATAL, "can't get enough log info\n"); } /* read one line to init module parameter */ read_line_to_module_record(line);//读取一行数据到每个模块的mod->record /* print header */ print_header();//打印头部信息 /* set struct module fields */ init_module_fields(); set_record_time(line);//更新conf.print_interval的间隔时间 return fp; }
/* * print mode, print data from tsar.data */ void running_print() { int print_num = 1, re_p_hdr = 0; char line[LEN_10240] = {0}; char filename[LEN_128] = {0}; long n_record = 0, s_time; FILE *fp; fp = init_running_print();//初始化,找到数据在哪个文件的什么部位。打印头部信息等。 /* skip first record */ if (collect_record_stat() == 0) { do_debug(LOG_INFO, "collect_record_stat warn\n"); } while (1) { if (!fgets(line, LEN_10240, fp)) { if (conf.print_file_number <= 0) { break; } else {//得读取下一个文件了。 conf.print_file_number = conf.print_file_number - 1; memset(filename, 0, sizeof(filename)); if (conf.print_file_number == 0) { sprintf(filename, "%s", conf.output_file_path); } else { sprintf(filename, "%s.%d", conf.output_file_path, conf.print_file_number); } if (fclose(fp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } fp = fopen(filename, "r"); if (!fp) { do_debug(LOG_FATAL, "unable to open the log file %s.\n", filename); } continue; } } int k = check_time(line); if (k == 1) { continue; } if (k == 3) { break; } /* collect data then set mod->summary */ read_line_to_module_record(line); if (!(print_num % DEFAULT_PRINT_NUM) || re_p_hdr) { /* get the header will print every DEFAULT_PRINT_NUM */ print_header(); re_p_hdr = 0; print_num = 1; } /* exclued the two record have same time */ if (!(s_time = set_record_time(line))) { continue; } /* reprint header because of n_item's modifing */ if (!collect_record_stat()) { re_p_hdr = 1; continue; } print_record_time(s_time); print_record(); n_record++; print_num++; memset(line, 0, sizeof(line)); } if (n_record) { printf("\n"); print_tail(TAIL_MAX); print_tail(TAIL_MEAN); print_tail(TAIL_MIN); } if (fclose(fp) < 0) { do_debug(LOG_FATAL, "fclose error:%s", strerror(errno)); } fp = NULL; }
/* * init_running_print, if sucess then return fp, else return NULL */ FILE *init_running_print() { int i=0,k=0; FILE *fp,*fptmp; char line[LEN_10240] = {0}; char filename[LEN_128] = {0}; /* will print tail*/ conf.print_tail = 1; fp = fopen(conf.output_file_path, "r"); if (!fp){ do_debug(LOG_FATAL, "unable to open the log file %s\n",conf.output_file_path); } /*log number to use for print*/ conf.print_file_number = -1; /* find start offset will print from tsar.data */ k=find_offset_from_start(fp,i); if(k==1){ /*find all possible record*/ for(i=1;;i++){ memset(filename,0,sizeof(filename)); sprintf(filename,"%s.%d",conf.output_file_path,i); fptmp = fopen(filename, "r"); if(!fptmp){ conf.print_file_number = i - 1; break; } k=find_offset_from_start(fptmp,i); if(k==0 || k==4){ fclose(fp); fp=fptmp; break; } if(k==2){ fseek(fp,0,SEEK_SET); fclose(fptmp); break; } if(k==1){ fclose(fp); fp=fptmp; continue; } if(k==5 || k==6){ do_debug(LOG_FATAL, "log format error or find_offset_from_start have a bug. error code=%d\n",k); } } } if(k==5 || k==6){ do_debug(LOG_FATAL, "log format error or find_offset_from_start have a bug. error code=%d\n",k); } /* get record */ if (!fgets(line, LEN_10240, fp)) { do_debug(LOG_FATAL, "can't get enough log info\n"); } /* read one line to init module parameter */ read_line_to_module_record(line); /* print header */ print_header(); /* set struct module fields */ init_module_fields(); set_record_time(line); return fp; }