任务名称:log信息提取程序
任务需求:读取给定文件夹内的log文件,统计每天的委托、成交、撤单信息
1.配置:编译好的程序需要读取配置文件,使用时通过配置文件配置log文件路径、统计结果保存路径。配置文件的格式不限,配置项最好是中文
2.任务过程中可能需要的功能(仅供参考):
①获取文件夹内的所有文件
②正则匹配(不会正则也可以用字符串查找函数)
3.输出文件格式:
输出文件类型为csv,文件名参照原log文件名,需要的字段:
合约代码,报单时间,报单方向,报单数量,报单价格,报单档位,请求撤单时间,撤单/成交时间,成交数量,撤单数量,最终状态
时间格式为 yyyy-mm-dd HH:MM:SS,以log每一行开头的时间为准
部分委托可能被打回,这种情况将打回时间作为撤单时间,最终状态为废单
最终状态类型:已报、全撤、部撤、部成、全成、废单。
已报表示报单成功,且没有成交;
全撤表示已经撤单成功,且没有成交;
部撤表示已经撤单成功,且成交了一部分;
部成表示未撤单,且成交了一部分;
全成表示未撤单,且全部成交;
废单表示报单失败
4.需要关注的信息:【报单】【状态】【成交回报】【请求撤单】
5.附加功能:统计委托失败的合约(含仓位不足未发单的)出现的时间
log_get.cpp:
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>#include <fstream>
#include <sstream>
using namespace std;class FileFind
{
private:string str;ifstream file_r;ofstream file_w,file_w_trust_fail;public:FileFind(string file_str_r, string file_str_w,string file_str_w_trust_fail){file_r.open(file_str_r);if (!file_r.is_open()){cout << "read open fail" << endl;}file_w.open(file_str_w);if (!file_w.is_open()){cout << "write open fail" << endl;}file_w_trust_fail.open(file_str_w_trust_fail);if (!file_w_trust_fail.is_open()){cout << "write stust open fail" << endl;}file_w << "合约代码 报单时间 报单方向 报单数量 报单价格 报单档位 请求撤单时间 撤单/成交时间 成交数量 撤单数量 最终状态" << endl;file_w_trust_fail<<"委托失败时间"<<endl;}void find(){while (1){if (file_r.eof())break;getline(file_r, str);count_stust_fail_time();find_baodan();find_zhuangtai();find_chengjiao();find_chedan();}}void find_baodan();void find_zhuangtai();void find_chengjiao();void find_chedan();void count_stust_fail_time();~FileFind(){file_r.close();file_w.close();file_w_trust_fail.close();}
};
void FileFind::count_stust_fail_time()
{if (str.find("仓位不足") != string::npos || str.find("未发单") != string::npos){string sub;sub = str.substr(0, 23);file_w_trust_fail<<sub<<endl;}
}
void FileFind::find_baodan()
{string sub;int index;if (str.find("【报单】") != string::npos){// cout << str << endl;// 输入信息到文件中// 输入合约代码index = str.find("正在提交报单");sub = str.substr(index + 21, 6);file_w << sub << " ";// 输入报单时间sub = str.substr(0, 19);// cout << sub << endl;file_w << sub << " ";// 输入买卖方向index = str.find("买卖方向:");// cout<<index<<endl;sub = str.substr(index + 15, 100);index = sub.find(",");sub = sub.substr(0, index);file_w << sub << " ";// 输入报单数量index = str.find("报单数量:");sub = str.substr(index + 15, 100);index = sub.find(",");sub = sub.substr(0, index);// cout<<sub<<endl;file_w << sub << " ";// 输入报单价格index = str.find("报单价:");sub = str.substr(index + 12, 100);index = sub.find(",");sub = sub.substr(0, index);// cout<<sub<<endl;file_w << sub << " ";// 输入档位index = str.find("报单档位:");sub = str.substr(index + 13, 100);index = sub.find(",");sub = sub.substr(0, index);// cout<<sub<<endl;file_w << sub << " ";// 输入请求撤单时间file_w << "无"<< " ";// 输入撤单/成交时间file_w << "无"<< " ";// 输入成交数量file_w << "无"<< " ";// 输入撤单数量file_w << "无"<< " ";// 输入最终状态file_w << "已报"<< " ";file_w << endl;}
}
void FileFind::find_zhuangtai()
{string sub;int index;if (str.find("【状态】") != string::npos){// cout << str << endl;// 输入信息到文件中// 输入合约代码index = str.find("合约代码:");sub = str.substr(index + 15, 6);file_w << sub << " ";// 输入报单时间sub = str.substr(0, 11);// cout << sub << endl;file_w << sub;index = str.find("报单时间:");sub = str.substr(index + 15, 100);index = sub.find(" ");sub = sub.substr(0, index);file_w << sub << " ";// 输入买卖方向index = str.find("买卖方向:");file_w << "无"<< " ";// 输入报单数量index = str.find("报单手数:");sub = str.substr(index + 15, 100);index = sub.find(" ");sub = sub.substr(0, index);// cout<<sub<<endl;file_w << sub << " ";// 输入报单价格index = str.find("报单价格:");sub = str.substr(index + 15, 100);index = sub.find(" ");sub = sub.substr(0, index);// cout<<sub<<endl;file_w << sub << " ";// 输入档位file_w << "无"<< " ";// 输入请求撤单时间file_w << "无"<< " ";// 输入撤单/成交时间sub = str.substr(0, 19);// cout << sub << endl;file_w << sub << " ";// cout<<sub<<endl;// 输入成交数量file_w << "无"<< " ";// // 输入撤单数量file_w << "无"<< " ";// 输入最终状态index = str.find("返回状态:");sub = str.substr(index + 13, 100);index = sub.find(" ");sub = sub.substr(0, index);if (sub == "未知"){sub = "未知";}else if (sub == "未成交还在队列中"){sub = "已报";}else if (sub == "部分成交还在队列中"){sub = "部成";}else if (sub == "全部成交"){sub = "全成";}else if (sub == "撤单"){sub = "全撤";}file_w << sub << " ";file_w << endl;}
}
void FileFind::find_chengjiao()
{string sub;int index;if (str.find("【成交回报】") != string::npos){// cout << str << endl;// 输入信息到文件中// 输入合约代码index = str.find("合约代码:");sub = str.substr(index + 15, 6);file_w << sub << " ";// 输入报单时间file_w << "无"<< " ";// 输入买卖方向index = str.find("方向:");// cout<<index<<endl;sub = str.substr(index + 9, 100);index = sub.find(",");sub = sub.substr(0, index);file_w << sub << " ";// 输入报单数量file_w << "无"<< " ";// 输入报单价格file_w << "无"<< " ";// 输入档位file_w << "无"<< " ";// 输入请求撤单时间file_w << "无"<< " ";// 输入撤单/成交时间sub = str.substr(0, 19);file_w << sub << " ";// 输入成交数量index = str.find("手数:");// cout<<index<<endl;sub = str.substr(index + 9, 100);index = sub.find(",");sub = sub.substr(0, index);file_w << sub << " ";// 输入撤单数量file_w << "无"<< " ";// 输入最终状态file_w << "未知"<< " ";file_w << endl;}
}
void FileFind::find_chedan()
{string sub;int index;if (str.find("【请求撤单】") != string::npos){// cout << str << endl;// 输入信息到文件中// 输入合约代码index = str.find("合约代码:");sub = str.substr(index + 13, 100);index = sub.find(",");sub = sub.substr(0, index);file_w << sub << " ";// 输入报单时间file_w << "无"<< " ";// 输入买卖方向file_w << "无"<< " ";// 输入报单数量file_w << "无"<< " ";// 输入报单价格file_w << "无"<< " ";// 输入档位file_w << "无"<< " ";// 输入请求撤单时间sub = str.substr(0, 19);// cout << sub << endl;file_w << sub << " ";// 输入撤单/成交时间file_w << "无"<< " ";// // 输入成交数量file_w << "无"<< " ";// // 输入撤单数量file_w << "无"<< " ";// // 输入最终状态file_w << "未知"<< " ";file_w << endl;}
}// 读取配置文件读路径
string getConfigFileRead()
{ifstream configFile;string str,sub;int index;configFile.open("./pathConfig.conf");if (!configFile.is_open()){cout << "read open fail" << endl;}while (1){if (configFile.eof())break;getline(configFile, str);if (str.find("读文件路径:") != string::npos){sub = str.substr(18, 100);index = sub.find("\n");sub = sub.substr(0, index);configFile.close();return sub;}}
}
// 读取配置文件写路径
string getConfigFileWrite()
{ifstream configFile;string str,sub;int index;configFile.open("./pathConfig.conf");if (!configFile.is_open()){cout << "write open fail" << endl;}while (1){if (configFile.eof())break;getline(configFile, str);if (str.find("写文件路径:") != string::npos){sub = str.substr(18, 100);index = sub.find("\n");sub = sub.substr(0, index);configFile.close();return sub;}}
}int main(int argc, const char *argv[])
{string read_path=getConfigFileRead();string write_path=getConfigFileWrite();// cout<<read_path+"/20230223_log.log"<<endl;// cout<<write_path+"/result.csv"<<endl;// 创建一个文件查找对象FileFind f(read_path+"/20230223_log.log", write_path+"/result.csv",write_path+"/trust_fail.csv");f.find();// 读取配置文件return 0;
}