程序的中写日志文件

主要涉及要点:

1.定时器使用,此处用的比较简单。

   程序起来后,启动一个定时器。

   SetTimer(1,2000,NULL);

2.获取当前程序的路径问题;

   BOOL CWritelogDlg::GetProPath() { //================================================================== char path_buffer[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; GetModuleFileName(NULL, path_buffer, _MAX_PATH); _splitpath(path_buffer, drive, dir, fname, ext); CString strDir = _T(""); strDir += drive; strDir += dir; m_strBasePath = strDir; //================================================================== return FALSE; }

其中:m_strBasePath:是一个CString 类型的成员变量;

3.处理定时器的函数;

void CWritelogDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default switch (nIDEvent) { case 1: WriteLog(); break; } CDialog::OnTimer(nIDEvent); }

 

4.具体的处理的函数:

 

BOOL CWritelogDlg::WriteLog() { CString str_log = m_strBasePath+_T("login.log"); CTime time; time = CTime::GetCurrentTime(); CString str_time_log = _T(""); CString str_time = _T(""); // str_time.Format("login time:%d-%d-%d %d:%d:%d login state:",(int)time.GetYear(),(int)time.GetMonth(),(int)time.GetDay(),(int)time.GetHour(),(int)time.GetMinute(),(int)time.GetSecond()); str_time_log = time.Format("login time:%y-%m-%d %H:%M:%S login state:"); str_time = time.Format("%y-%m-%d %H:%M:%S"); SetDlgItemText(IDC_BTN_TIME,str_time); ::WritePrivateProfileString("Time-log", str_time_log, "0", str_log.GetBuffer(str_log.GetLength())); str_log.ReleaseBuffer(); // KillTimer(1); return FALSE; }

 

涉及时间获取,时间的转换。然后是写日志。

相关源代码:

下载地址:http://download.csdn.net/source/1698020

 

 

 

 

 

你可能感兴趣的:(ext,null,buffer,Path,login)