MFC下调试日志的打印

最近项目出现点小Bug,需要调试跟踪代码,于是乎写了份打印日志的代码。

CLogFile.h文件

 #if !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_)
#define AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 class CLogFile
{
public:
CLogFile();
~CLogFile();
private:
static CString GetFileName();
static CString GetFilePath();
public:
static bool WriteLog(CString LogText);
}; #endif // !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_)

CLogFile.cpp文件

 #include "stdafx.h"
#include "CLogFile.h" CLogFile::CLogFile()
{ } CLogFile::~CLogFile()
{ }
//获取文件名称
CString CLogFile::GetFileName()
{
CString sFileName; sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log"); return sFileName;
} //获取应用程序所在路径
CString CLogFile::GetFilePath()
{
CString m_FilePath; GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+),MAX_PATH); m_FilePath.ReleaseBuffer(); int m_iPosIndex; m_iPosIndex = m_FilePath.ReverseFind('\\'); //m_FilePath = m_FilePath.Left(m_iPosIndex) + TEXT("\\Log");
m_FilePath = "D:\\VISTA\\LOGSA"; return m_FilePath;
} bool CLogFile::WriteLog(CString LogText)
{
try
{
//CFile m_File;
CFile m_SFile;
CFileFind m_FileFind;
CString m_sErrorMessage;
CString m_sFileName = GetFileName();
CString m_sFilePath = GetFilePath();
CString m_sCurrentTime = CTime::GetCurrentTime().Format("%Y-%m-%d %X"); if(!m_FileFind.FindFile(m_sFilePath))
{
CreateDirectory(m_sFilePath,NULL);
} m_SFile.Open(m_sFilePath + TEXT("\\") +m_sFileName,CFile::modeCreate |CFile::modeNoTruncate| CFile::modeReadWrite |CFile::shareDenyWrite);
/*{
m_SFile.Open(m_sFilePath + TEXT("\\") + m_sFileName,CFile::modeCreate | CFile::modeReadWrite |CFile::shareDenyWrite| CFile::typeText);
}*/ m_SFile.SeekToEnd(); if(sizeof(TCHAR)==sizeof(WCHAR))
{
WORD wSignature = 0xFEFF;
m_SFile.Write(&wSignature, );
} //TCHAR* m_szMessage; //m_szMessage=m_sErrorMessage.GetBuffer(); m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************")+TEXT("\r\n") ;
m_SFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR)); //m_sErrorMessage = LogText ;
LogText+=TEXT("\r\n");
m_SFile.Write(LogText,LogText.GetLength()*sizeof(TCHAR));
//m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************") ;
m_SFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR)); //m_SFile.Write(m_sErrorMessage.GetBuffer(),m_sErrorMessage.GetLength()); m_SFile.Close();
}
catch(CFileException fileException)
{
return false;
} return true;
}

以上代码经过调试通过!

上一篇:maven插件maven-install-plugin


下一篇:mongodb3.6 副本集(三)mongodb 如何做数据备灾