Recently the project has a small bug, need to debug the tracking code, so write a copy of the code to print the log.
CLogFile.h file
1 #if!defined (Afx_logfile_h__288388ca_9a3e_4f3d_a2b8_f1078e1f6a6b__included_)2 #defineAfx_logfile_h__288388ca_9a3e_4f3d_a2b8_f1078e1f6a6b__included_3 4 #if_msc_ver > 10005 #pragmaOnce6 #endif //_msc_ver >7 8 classClogfile9 { Ten Public: One clogfile (); A~clogfile (); - Private: - StaticCString GetFileName (); the StaticCString GetFilePath (); - Public: - Static BOOLWritelog (CString logtext); - }; + - #endif //!defined (afx_logfile_h__288388ca_9a3e_4f3d_a2b8_f1078e1f6a6b__included_)
CLogFile.cpp file
1#include"stdafx.h" 2#include"CLogFile.h"3 4 Clogfile::clogfile ()5 {6 7 }8 9clogfile::~Clogfile ()Ten { One A } - //Get file name - CString clogfile::getfilename () the { - CString sFileName; - -sFileName = Ctime::getcurrenttime (). Format ("%y-%m-%d") + TEXT (". Log"); + - returnsFileName; + } A at //get the path to your application - CString Clogfile::getfilepath () - { - CString M_filepath; - -GetModuleFileName (Null,m_filepath.getbuffersetlength (max_path+1), MAX_PATH); in - M_filepath.releasebuffer (); to + intM_iposindex; - theM_iposindex = M_filepath.reversefind ('\\'); * $ //M_filepath = M_filepath.left (m_iposindex) + TEXT ("\\Log");Panax NotoginsengM_filepath ="D:\\vista\\logsa"; - the returnM_filepath; + } A the BOOLClogfile::writelog (CString logtext) + { - Try $ { $ //CFile M_file; - CFile M_sfile; - CFileFind M_filefind; the CString m_serrormessage; -CString M_sfilename =GetFileName ();WuyiCString M_sfilepath =GetFilePath (); theCString m_scurrenttime = Ctime::getcurrenttime (). Format ("%y-%m-%d%x"); - Wu - if(!M_filefind.findfile (m_sfilepath)) About { $ CreateDirectory (m_sfilepath,null); - } - -M_sfile.open (M_sfilepath + TEXT ("\\") +m_sfilename,cfile::modecreate | cfile::modenotruncate| Cfile::modereadwrite |cfile::sharedenywrite); A /*{ + M_sfile.open (M_sfilepath + TEXT ("\ \") + m_sfilename,cfile::modecreate | Cfile::modereadwrite | cfile::sharedenywrite| Cfile::typetext); the }*/ - $ m_sfile.seektoend (); the the if(sizeof(TCHAR) = =sizeof(WCHAR)) the { theWORD wsignature =0xFEFF; -M_sfile.write (&wsignature,2); in } the the //tchar* m_szmessage; About the //M_szmessage=m_serrormessage.getbuffer (); the theM_serrormessage = TEXT ("*******************") + M_scurrenttime + TEXT ("*******************") +text ("\ r \ n") ; +M_sfile.write (M_serrormessage,m_serrormessage.getlength () *sizeof(TCHAR)); - the //m_serrormessage = LogText;BayiLogtext+=text ("\ r \ n"); theM_sfile.write (Logtext,logtext.getlength () *sizeof(TCHAR)); the //m_serrormessage = text ("*******************") + M_scurrenttime + text ("*******************"); -M_sfile.write (M_serrormessage,m_serrormessage.getlength () *sizeof(TCHAR)); - the //M_sfile.write (M_serrormessage.getbuffer (), m_serrormessage.getlength ()); the the m_sfile.close (); the } - Catch(cfileexception fileexception) the { the return false; the }94 the return true; the}
The above code after debugging through!
Printing of debug logs under MFC