取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各種MFC常用的類
// LogFile.h: interface for the CLogFile class. // // Written by Bjarke Viksoe (bjarke@viksoe.dk) // Copyright (c) 2000. // // This code may be used in compiled form in any way you desire. This // file may be redistributed by any means PROVIDING it is // not sold for profit without the authors written consent, and // providing that this notice and the authors name is included. // // This file is provided "as is" with no expressed or implied warranty. // The author accepts no liability if it causes any damage to you or your // computer whatsoever. It's free, so don't hassle me about it. // // Beware of bugs. //////////////////////////////////////////////////////////////////////// #if !defined(AFX_LOGFILE_H__0EE45201_E8F9_11D1_93C1_241C08C10000__INCLUDED_) #define AFX_LOGFILE_H__0EE45201_E8F9_11D1_93C1_241C08C10000__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 // A small class implementing a debug log file. class CLogFile : public CObject { public: CLogFile(); CLogFile(LPCTSTR Filename); virtual ~CLogFile(); // Methods public: // Creates (removes previous) log file. RETCODE Create(LPCTSTR Filename, LPCTSTR Text); // Set the filename of the log fil to use. RETCODE SetFilename(LPCTSTR Filename); // Creates or appends to an exisiting log file. RETCODE AppendText(LPCTSTR Text, ...); // Writes System Information to the log RETCODE LogSystemInformation(); // Variables protected: CString m_Filename; // The log file we're currently writing to. }; #endif // !defined(AFX_LOGFILE_H__0EE45201_E8F9_11D1_93C1_241C08C10000__INCLUDED_)
LogFile.cpp
1 // LogFile.cpp: implementation of the CLogFile class. 2 // 3 // A small class which can be used for debug logs. 4 // 5 ////////////////////////////////////////////////////////////////////// 6 7 #include "stdafx.h" 8 #include "LogFile.h" 9 10 #ifdef _DEBUG 11 #undef THIS_FILE 12 static char THIS_FILE[]=__FILE__; 13 #define new DEBUG_NEW 14 #endif 15 16 ////////////////////////////////////////////////////////////////////// 17 // Construction/Destruction 18 ////////////////////////////////////////////////////////////////////// 19 20 CLogFile::CLogFile() 21 { 22 } 23 24 CLogFile::CLogFile(LPCTSTR Filename) 25 { 26 SetFilename( Filename ); 27 } 28 29 CLogFile::~CLogFile() 30 { 31 } 32 33 34 ////////////////////////////////////////////////////////////////////// 35 // Methods 36 ////////////////////////////////////////////////////////////////////// 37 38 RETCODE CLogFile::Create(LPCTSTR Filename, LPCTSTR Text) 39 { 40 ASSERT( Filename ); 41 ASSERT( Text ); 42 m_Filename = Filename; 43 ASSERT( !m_Filename.IsEmpty() ); 44 if( m_Filename.IsEmpty() ) return RET_INVALIDARGS; 45 // Write text to (text)file 46 CStdioFile f; 47 TRY 48 { 49 BOOL res = f.Open( Filename, CFile::modeCreate|CFile::modeWrite|CFile::typeText ); 50 if( res ) { 51 f.WriteString( Text ); 52 f.WriteString( _T("\n") ); 53 f.Close(); 54 }; 55 } 56 CATCH_ALL(e) 57 { 58 f.Abort(); 59 #ifdef _DEBUG 60 e->ReportError(); 61 #endif 62 return RET_ERROR; 63 } 64 END_CATCH_ALL; 65 return RET_OK; 66 }; 67 68 RETCODE CLogFile::AppendText(LPCTSTR Text, ...) 69 { 70 ASSERT(AfxIsValidString(Text)); 71 ASSERT(!m_Filename.IsEmpty()); 72 if( m_Filename.IsEmpty() ) return RET_NOTINITIALIZED; 73 // Append text to (text)file 74 CStdioFile f; 75 CString sText; 76 va_list args; 77 va_start(args, Text); 78 sText.FormatV(Text, args); 79 TRY 80 { 81 BOOL res = f.Open( m_Filename, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::typeText ); 82 if( res ) { 83 f.SeekToEnd(); 84 f.WriteString( sText ); 85 f.WriteString( _T("\n") ); 86 f.Close(); 87 }; 88 } 89 CATCH_ALL(e) 90 { 91 f.Abort(); 92 #ifdef _DEBUG 93 e->ReportError(); 94 #endif 95 return RET_FILEERROR; 96 } 97 END_CATCH_ALL; 98 return RET_OK; 99 }; 100 101 RETCODE CLogFile::SetFilename(LPCTSTR Filename) 102 // Sets the log filename. A new log file will 103 // be created if the file does not exist. 104 { 105 ASSERT(AfxIsValidString(Filename)); 106 m_Filename = Filename; 107 if( m_Filename.IsEmpty() ) return RET_INVALIDARGS; 108 return RET_OK; 109 } 110 111 RETCODE CLogFile::LogSystemInformation() 112 // Write some standard system information to 113 // the log file. 114 { 115 ASSERT(!m_Filename.IsEmpty()); 116 if( m_Filename.IsEmpty() ) return RET_NOTINITIALIZED; 117 118 SYSTEMTIME time; 119 ::GetLocalTime( &time ); 120 AppendText(_T("Date: %04d-%02d-%02d Time: %02d:%02d:%02d"), 121 time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond); 122 123 OSVERSIONINFO verinfo; 124 verinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 125 ::GetVersionEx(&verinfo); 126 AppendText(_T("Win%s Version %d.%.2d (build %d) %s\n"), 127 (verinfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? _T("NT") : _T("32")), 128 verinfo.dwMajorVersion, 129 verinfo.dwMinorVersion, 130 verinfo.dwBuildNumber, 131 verinfo.szCSDVersion); 132 133 SYSTEM_INFO sysinfo; 134 LPCTSTR pszProcessor; 135 ::GetSystemInfo(&sysinfo); 136 switch( sysinfo.dwProcessorType ) { 137 case PROCESSOR_INTEL_386: 138 case PROCESSOR_INTEL_486: 139 case PROCESSOR_INTEL_PENTIUM: 140 pszProcessor = _T("Intel "); 141 break; 142 case PROCESSOR_MIPS_R4000: 143 pszProcessor = _T("MIPS R"); 144 break; 145 case PROCESSOR_ALPHA_21064: 146 pszProcessor = _T("DEC Alpha "); 147 break; 148 default: 149 pszProcessor = _T("Chipset "); 150 break; 151 } 152 return AppendText(_T("%s%d, %d Processor(s)\n"), 153 (LPCTSTR)pszProcessor, 154 sysinfo.dwProcessorType, 155 sysinfo.dwNumberOfProcessors); 156 };