Skip to content

Instantly share code, notes, and snippets.

@inkedawn inkedawn/logSatatics.cpp
Last active Feb 3, 2016

Embed
What would you like to do?
好雨云日志分析-流量统计 https://www.goodrain.com/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <set>
#include <string>
#include <regex>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <windows.h>
using namespace std;
typedef unsigned long long DATASIZE;
const char CUTOFF_LINE[] = "=============================================";
//const string logPattern = "\\w+?: \"(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\" \"(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\" \\[.*?\\] \".*?\" \\d{3} (\\d+) \".*?\" .*";
const string logPattern = ".*?\" \\d{3} (\\d+) \\d*.*?\".*?";
string friendlyDataSize(DATASIZE nBytes)
{
const char* units[] = { "Bytes", "KB", "MB", "GB", "TB" };
const size_t len= sizeof(units) / sizeof(units[0]);
DATASIZE converted = nBytes;
int cnt = 0;
while (cnt < len-1 && converted >= 1024)
{
converted /= 1024;
++cnt;
}
stringstream s;
s << converted << units[cnt];
return s.str();
}
DATASIZE getSize(string content)
{
DATASIZE result = 0;
regex pattern(logPattern);
smatch matches;
//clog << logPattern <<endl ;
//cout << content << endl;
//cout << "====================" << endl;
try
{
if (regex_match(content, matches, pattern))
{
//clog << "request from " << matches[1] << " via " << matches[2] << ", transport " << matches[3] << " bytes" << endl;
stringstream s;
s << matches[1];
s >> result;
}
}
catch (std::regex_error e)
{
cerr<< e.what();
return result;
}
return result;
}
bool getFileList(set<string> &result, string path)
{
WIN32_FIND_DATA p;
HANDLE h= FindFirstFileEx("*.log", FindExInfoStandard, &p, FindExSearchNameMatch, NULL, 0);
if (h == INVALID_HANDLE_VALUE)
{
cerr << "Found file failed" << GetLastError() << endl;
return false;
}
//clog << "Found file " << p.cFileName << endl;
result.insert(p.cFileName);
while (FindNextFile(h, &p))
{
result.insert(p.cFileName);
//clog << "Found file " << p.cFileName << endl;
}
FindClose(h);
return true;
}
#include <cassert>
void test()
{
assert(true == getFileList(*(new set<string>)));
assert(2868 == getSize("07e5c4fc02ff: \"128.199.222.9\" \"10.3.1.6\" [03/Jan/2016:00:00:06 +0800] \"GET / usr / plugins / Reposidget / src / reposidget.js HTTP / 1.1\" 200 2868 \"http://moew.xyz/\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2593.0 Safari/53"));
assert("54Bytes" == friendlyDataSize(54));
assert("1023Bytes" == friendlyDataSize(1023));
assert("1KB" == friendlyDataSize(1024));
assert("1KB" == friendlyDataSize(1025));
assert("2MB" == friendlyDataSize(2 * 1024 * 1024));
assert("3GB" == friendlyDataSize(3ULL * 1024 * 1024 * 1024));
assert("1TB" == friendlyDataSize(1ULL * 1024 * 1024 * 1024 * 1024));
assert("1024TB" == friendlyDataSize(1024ULL * 1024 * 1024 * 1024 * 1024));
}
int main()
{
#if _TEST
test();
#endif
DATASIZE sum = 0;
set<string> filelist;
if (!getFileList(filelist))
{
cerr << "Not found any file!" << endl;
return false;
}
for (auto iter = filelist.begin(); iter != filelist.end(); ++iter )
{
ifstream fs;
//clog << CUTOFF_LINE << endl;
clog << *iter << " ";
fs.clear();
fs.open(*iter, ios::in);
sum += [&fs]
{
DATASIZE filesum = 0;
string data;
while (getline(fs, data))
{
DATASIZE size = getSize(data);
if (size > 0)
{
filesum += size;
}
//size || system("pause");
}
clog << friendlyDataSize(filesum) << endl;
filesum || system("pause");
return filesum;
}();
}
clog << CUTOFF_LINE << endl;
cout << "In total " << friendlyDataSize(sum) << " Bytes" << endl;
#ifndef _DEBUG
system("pause");
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.