// EventLog.cpp : implementation file // #include "stdafx.h" #include "StoneU_HC_OCX.h" #include "EventLog.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif extern CLIENTPARAM ClientParam; static int iEventPos = 0; #define EVENTNUM 2000 CString csEventTime[EVENTNUM]; CString csEventInfo[EVENTNUM]; ///////////////////////////////////////////////////////////////////////////// // CEventLog dialog void F_AddEvent(CString csEvent) { CEventLog m_eventlog; m_eventlog.AddEvent(csEvent); } CEventLog::CEventLog(CWnd* pParent /*=NULL*/) : CDialog(CEventLog::IDD, pParent) { //{{AFX_DATA_INIT(CEventLog) //}}AFX_DATA_INIT } void CEventLog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CEventLog) DDX_Control(pDX, IDC_LIST1, m_list); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CEventLog, CDialog) //{{AFX_MSG_MAP(CEventLog) ON_BN_CLICKED(IDC_BUTTONSAVE, OnButtonsave) ON_BN_CLICKED(IDEXIT, OnExit) ON_BN_CLICKED(ID_CANCEL, OnCancel) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CEventLog message handlers void CEventLog::AddEvent(CString csEvent) { CTime time = CTime::GetCurrentTime(); csEventTime[iEventPos].Format("%04d-%02d-%02d %02d:%02d:%02d", time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); csEventInfo[iEventPos].Format("%s", csEvent); iEventPos++; if(iEventPos >= EVENTNUM) { iEventPos = 0; } } void CEventLog::OnButtonsave() { // TODO: Add your control notification handler code here CString sFileName; CTime time; if(iEventPos == 0) return; time = CTime::GetCurrentTime(); sFileName.Format("%s\\EventFile_%4d%02d%02d_%02d%02d%02d.txt", ClientParam.m_csLogSavePath, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); F_SaveEventFile(sFileName); F_InitList(); m_bSave = TRUE; } void CEventLog::F_SaveEventFile(CString csFileName) { int i; CString sTemp; CStdioFile myFile; if(myFile.Open(csFileName, CFile::modeCreate|CFile::modeWrite) == FALSE) { MessageBox("日志文件创建失败!", "温馨提示", MB_ICONINFORMATION); return; } for(i = 0; i < iEventPos; i++) { sTemp.Format("%s %s\r\n", csEventTime[i], csEventInfo[i]); myFile.WriteString(sTemp); } myFile.Close(); iEventPos = 0; } BOOL CEventLog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_bSave = FALSE; m_nTimer = 0; m_list.InsertColumn(0,"时间",LVCFMT_LEFT,120,-1); m_list.InsertColumn(1,"事件内容",LVCFMT_LEFT,600,-1); F_InitList(); m_nTimer = SetTimer(EVENTLOG_TIMER, 1000, NULL); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CEventLog::F_InitList() { int i; m_list.DeleteAllItems(); for(i = 0; i < iEventPos; i++) { m_list.InsertItem(i, csEventTime[i]); m_list.SetItemText(i, 1, csEventInfo[i]); } } void CEventLog::OnExit() { // TODO: Add your control notification handler code here if(m_nTimer) { KillTimer(EVENTLOG_TIMER); } CDialog::OnCancel(); } void CEventLog::OnCancel() { // TODO: Add your control notification handler code here } void CEventLog::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent == EVENTLOG_TIMER) { //要刷新列表,必须要对csEventTime和csEventInfo数组进行保护。 //F_InitList(); } CDialog::OnTimer(nIDEvent); }