在后台开发中, 经常遇到服务波动的情况, 一般来说, 在监控系统中可以看到这种波动, 那是否与我们机器上的错误log对应起来了呢? 我们有必要对机器上的错误log进行统计!
规范log中一般会有时间, 假设时间格式为: xxx[2016-11-20 hh:mm:ss]yyy, 那么怎样统计错误log是否集中在某以时间段呢? 写个C++程序吧:
结果:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
string findTime(const string &str)
{
unsigned int pos = str.find("[201");
if(pos == string::npos)
{
return "[system error]";
}
return str.substr(pos, 21);
}
int main()
{
ifstream in("data.txt"); // data.txt为log文件
string filename;
string line;
vector<string> vecString;
map<string, unsigned int> m;
if(in) // 有该文件
{
while (getline (in, line)) // line中不包括每行的换行符
{
if(line == "")
{
continue;
}
vecStrin