[C++] Log文件遍历与关键词提取

程序员日常工作中少不了要debug,根据Log来分析code。 然而在大型项目中常常有log模块,我们根据自己的需要加入特定的log即可, 然后分析。由于LOG里面内容太多,我通常都会在Log时加上一个特定的字符串,这样得到log以后便于自己查找。但是往往log文件通常很多,为了看Log方便,特意编写了一个小程序,可以将多个相同格式的文件,如 .txt, 或者.log, 根据关键词索引提取到一个相同的文件中,大大的提高了自己debug的效率。

平时用的language 是 C++, 因此这个程序是用C++在VS2015的环境下编写的。代码很简单,主要用到就是windows 下的文件遍历 以及C++中的文件流操作。
Flowchart
源代码如下:

#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<io.h>
#include <ctime>

#define TIME_BUFFER 255
using namespace std;

//根据用户输入的文件格式,txt或者log,在对应输入的文件路径下进行搜索,得到对应的files vector。
bool GetAllFormatFiles(string path, vector<string>& files, string format)
{
    long   hFile = 0;
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1)
    {
        do
        {
            //是否包含文件夹,如果包含文件夹,即目录,则递归搜索该目录
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                {
                    GetAllFormatFiles(p.assign(path).append("\\").append(fileinfo.name), files, format);
                }
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
        return true;
    }
    else
    {
        cout << "No such File, please make sure your input path is correct!" << endl;
        return false;
    }
}
//获取文件路径,添加斜杠。用户直接复制粘贴地址只有单斜杠,方便操作。
void GetPath(string & refFilePath)
{
    int nPathSize = refFilePath.size();
    for (int i = 0; i < nPathSize; i++)
    {
        if (refFilePath[i] == '\\')
            refFilePath.insert(i, "\\");
        i++;
    }
}
//给输出文件名增加当前时间作为后缀用以区分
char * GetCurSysTimeString()
{
    time_t now = time(0);
    struct tm CurrentLocalTime;
    localtime_s(&CurrentLocalTime,&now);
    char cTimeNow[TIME_BUFFER];
    strftime(cTimeNow, TIME_BUFFER, "%Y%m%d%H%M%S",&CurrentLocalTime);
    char * pOutputFileName = cTimeNow;
    return pOutputFileName;
}
void GetOutputFileName(string &OutputFileName)
{
    OutputFileName = GetCurSysTimeString();
    OutputFileName.insert(0, "Combine");
    OutputFileName.append(".txt");
    return;
}
int main()
{
    bool bStop;
    string szFilePath;
    string szFormat;
    string szReadLine;
    string szSearchCondition;
    string szOutFileName;
    char  cStop;
    vector<string> files;
    char * pDisAllFiles = "AllFiles.txt";
    while (1)
    {
        cin.clear();
        cin.sync();
        cout << "Please input file palth, (hint: You can use copy paste the file path):" << endl;
        getline(cin, szFilePath);
        GetPath(szFilePath);
        cout << "Please input wantted search file format,eg ,'txt' :" << endl;
        getline(cin, szFormat);
        szFormat.insert(0, 1, '.');
        if (GetAllFormatFiles(szFilePath, files, szFormat))
        {
            cout << "Please input wanted filter key words(Noted:sensitve to upper and lower cases):" << endl;
            getline(cin, szSearchCondition);
            ofstream fout(pDisAllFiles);
            fout << files.size() << endl;
            for (int i = 0; i<files.size(); i++)
            {
                fout << files[i] << endl;
                cout << files[i] << endl;
            }
            fout.close();
            GetOutputFileName(szOutFileName);
            ofstream out(szOutFileName);
            for (int i = 0; i < files.size(); i++)
            {
                ifstream in(files[i]);
                if (in)
                {
                    while (getline(in, szReadLine))
                    {
                        if (szReadLine.find(szSearchCondition) != string::npos)
                            out << szReadLine << endl;
                    }
                    if ((i + 1) != files.size())
                        out << "*********************Change to next " << files[i + 1] << "***********************" << endl;
                }
                cout << files[i] << " was extracted!" << endl;
            }
            cout << "Extract success!" << endl;
            out.close();
        }
        else
        {
            continue;
        }

        cout << "Do you want keep extract other file logs?" << endl;
        cout << "If yes, please input 'y', others ipnut will quit this program! " << endl;
        cin >> cStop;
        if (cStop != 'y')
        {
            break;
        }
        else
        {
            cout << "*************************** New search begin! **************************" << endl;
            files.clear();
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值