代码拉取完成,页面将自动刷新
// ApacheLogAnalyzerDoc.cpp : implementation of the CLogAnalyzerDoc class
//
#include "stdafx.h"
#include "ApacheLogAnalyzer.h"
#include "ApacheLogAnalyzerDoc.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CLogAnalyzerDoc* CLogAnalyzerDoc::m_pThis = NULL;
/////////////////////////////////////////////////////////////////////////////
// CLogAnalyzerDoc
IMPLEMENT_DYNCREATE(CLogAnalyzerDoc, CDocument)
BEGIN_MESSAGE_MAP(CLogAnalyzerDoc, CDocument)
//{{AFX_MSG_MAP(CLogAnalyzerDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLogAnalyzerDoc construction/destruction
CLogAnalyzerDoc::CLogAnalyzerDoc()
{
m_hThread = NULL;
}
CLogAnalyzerDoc::~CLogAnalyzerDoc()
{
m_errorCode.clear();
m_hostPageSize.clear();
m_hostVisitCount.clear();
m_errorLines.clear();
}
BOOL CLogAnalyzerDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
ResetVariant();
this->UpdateAllViews(NULL);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CLogAnalyzerDoc serialization
void CLogAnalyzerDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CLogAnalyzerDoc diagnostics
#ifdef _DEBUG
void CLogAnalyzerDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CLogAnalyzerDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLogAnalyzerDoc commands
UINT CLogAnalyzerDoc::StatApacheLogProc(LPVOID)
{
ASSERT(m_pThis != NULL);
CFile f(m_pThis->GetPathName(), CFile::modeRead);
DWORD fileBytes = f.GetLength();
f.Close();
ifstream logFile(m_pThis->GetPathName(), ios::in);
if(!logFile){
AfxMessageBox("文件打开错误!");
return FALSE;
}
m_pThis->ResetVariant();
//TODO: 显示耗费时间
SYSTEMTIME curTime;
GetSystemTime(&curTime);
CLogAnalyzerView* pView = m_pThis->GetView();
CProgressCtrl* progCtrl = (CProgressCtrl*)pView->GetDlgItem(IDC_STAT_PROGRESS);
progCtrl->SetRange32(0,fileBytes);
pView->OnUpdate(NULL, STAT_BEGIN, NULL);
DWORD nBytes = 0;
CString sIndicate;
string line;
int nLine = 0;
while(getline(logFile, line, '\n')){
if(m_pThis->m_bStop)
return FALSE;
nBytes += line.size();
m_pThis->handleLine(++nLine, line);
if(nLine % 300 == 0){
sIndicate.Format("%d",nLine);
pView->SetDlgItemText(IDC_STATIC_PROGRESS_INDICATE,sIndicate);
}
if(m_pThis->m_errorLines.size()>0){
sIndicate.Format("%d",m_pThis->m_errorLines.size());
pView->SetDlgItemText(IDC_STATIC_ERROR_COUNT,sIndicate);
}
//发送进度更新消息给视图
if(nLine % 3000 == 0)
progCtrl->SetPos(nBytes);
}
progCtrl->SetPos(fileBytes);
sIndicate.Format("%d",nLine);
pView->SetDlgItemText(IDC_STATIC_PROGRESS_INDICATE,sIndicate);
pView->OnUpdate(NULL, STAT_FINISH, NULL);
m_pThis->m_bStop = TRUE;
return TRUE;
}
CLogAnalyzerView* CLogAnalyzerDoc::GetView()
{
POSITION pos = GetFirstViewPosition();
CLogAnalyzerView* pView = (CLogAnalyzerView*)GetNextView(pos);
//ASSERT_VALID(pView);
return pView;
}
inline BOOL CLogAnalyzerDoc::handleLine(int nLine, string line){
//解析每一个字段的内容
int beginIdx = 0;
int endIdx = line.find_first_of(' ');
if(endIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
string host = line.substr(beginIdx,endIdx-beginIdx);
beginIdx = line.find_first_of('[');
if(beginIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
endIdx = line.find_first_of(']');
if(endIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
string time = line.substr(beginIdx+1,endIdx-beginIdx-1);
beginIdx = line.find_first_of('"',endIdx);
if(beginIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
endIdx = line.find_first_of('"',beginIdx+1);
if(endIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
string url = line.substr(beginIdx+1,endIdx-beginIdx-1);
//解析url
int tmpBeginIdx = url.find_first_of(' ');
int tmpEndIdx = url.find_first_of(' ',tmpBeginIdx+1);
if(tmpBeginIdx!=string::npos && tmpEndIdx!=string::npos){
url = url.substr(tmpBeginIdx+1, tmpEndIdx-tmpBeginIdx-1);
}
beginIdx = line.find_first_of(' ',endIdx);
if(beginIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
endIdx = line.find_first_of(' ',beginIdx+1);
if(endIdx==string::npos){
m_errorLines.push_back(nLine);
return FALSE;
}
int errorCode = atoi(line.substr(beginIdx+1,endIdx-beginIdx-1).c_str());
int pageSize = atoi(line.substr(endIdx+1).c_str());
typedef map<int,int>::value_type error_code_value_type;
if(m_errorCode.count(errorCode))
m_errorCode[errorCode]+=1;
else{
m_errorCode.insert(error_code_value_type(errorCode,1));
}
typedef map<string,__int64>::value_type host_visit;
if(!m_hostVisitCount.count(host)){
m_hostVisitCount.insert(host_visit(host,1));
m_hostPageSize.insert(host_visit(host,pageSize));
}
else{
m_hostVisitCount[host] ++;
m_hostPageSize[host] += pageSize;
}
if(pageSize>=0)
m_totalPageSize += pageSize;
m_visitCount ++;
return TRUE;
}
BOOL CLogAnalyzerDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if(m_hThread){
DWORD dwExitCode;
GetExitCodeThread(m_hThread->m_hThread,&dwExitCode);
if(dwExitCode==STILL_ACTIVE)
return FALSE;
}
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
SetPathName(lpszPathName);
//Start the thread
m_pThis = this;
m_bStop = FALSE;
m_hThread = AfxBeginThread(StatApacheLogProc, this, THREAD_PRIORITY_NORMAL);
return TRUE;
}
void CLogAnalyzerDoc::ResetVariant()
{
m_errorCode.clear();
m_hostPageSize.clear();
m_hostVisitCount.clear();
m_errorLines.clear();
m_totalPageSize = 0;
m_visitCount = 0;
}
BOOL CLogAnalyzerDoc::CanCloseFrame(CFrameWnd* pFrame)
{
if(m_hThread){
DWORD dwExitCode;
GetExitCodeThread(m_hThread->m_hThread,&dwExitCode);
if(dwExitCode==STILL_ACTIVE){
int mr = AfxMessageBox(IDS_EXIT_CONFIRM,MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2);
if(mr==IDNO)
return FALSE;
else{
m_bStop = TRUE;
dwExitCode = WaitForSingleObject(m_hThread->m_hThread, 1000);
}
}
}
return CDocument::CanCloseFrame(pFrame);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。