/*
* File: dread.cpp
* Author: 肖武<[email protected]>
*
* Created on 2013年10月31日, 上午10:02
*
* 延时读取(delayed read)文件
*/
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
/*
*
*/
int main(int argc, char* argv[]) {
//参数变量初始化
string in_file;
int sleep_time = 3;
time_t end_time = 0;
//获取参数
int ch;
opterr = 0; //选项错误时不让报错
while ((ch = getopt(argc, argv, "s:t:f:")) != -1) {
switch (ch) {
case 'f': in_file = string(optarg);break;
case 't': end_time = atol(optarg); break;
case 's': sleep_time = atoi(optarg); break;
}
}
//参数检查
if (in_file == "") {
cout<<
"延时读取(delayed read)文件 v1.0\n"
"用法:"<<argv[0]<<" -f file_path [-s sleep_time] [-e end_time]\n"
"选项:\n"
"\t-f\t必须,读取的文件路径\n"
"\t-s\t可选,读到文件末尾时休眠的秒数。默认为3\n"
"\t-e\t可选,结束时间戳,在此时间之前读到文件末尾时不会退出,而是休眠sleep_time秒后再次读取。默认为0\n\n"
"作者:肖武<[email protected]>"
<<endl;
return 1;
}
ifstream ifs(in_file.c_str(), ios::in);
if (!ifs.is_open()) {
cerr<<"打开数据文件失败:"<<in_file<<endl;
return 1;
}
string row;
string val;
size_t seek = 0;
time_t now = 0;
while(1) {
if (ifs.peek() == EOF) {
time(&now);
if (now > end_time) {
break;
}
ifs.clear();
sleep(sleep_time);
ifs.seekg(seek, ios::beg);
continue;
}
getline(ifs, row);
seek = ifs.tellg();
cout<<row<<endl;
}
ifs.close();
return 0;
}
编译
$ g++ -o dread dread.cpp
使用
[root@localhost dread]# ./dread
延时读取(delayed read)文件 v1.0
用法:./dread -f file_path [-s sleep_time] [-e end_time]
选项:
-f 必须,读取的文件路径
-s 可选,读到文件末尾时休眠的秒数。默认为3
-e 可选,结束时间戳,在此时间之前读到文件末尾时不会退出,而是休眠sleep_time秒后再次读取。默认为0
作者:肖武<[email protected]>
使用场景举例:日志文件处理
日志实时产生,追加方式写入文件中,每小时一个文件。现需要每小时结束时尽快分析处理上个小时的日志数据,形成统计报表。那么单条日志的预处理工作就可以提前进行,如下
$ ./dread -f log/2013/10/31/12.txt -t `date -d "2013-10-31 13:01"` |awk '{#日志处理.....}' > ret.txt
为了数据上的完整,考虑到13点整时可能还有数据未写入,可以多延迟等待1分钟