改编于一个老外的同名作品。
############ log.h #################
//===========================================================================//// HomeWork from Belgium Not licensed software // 1999 - 2000 No rights reserved////===========================================================================//// Project/Product : Iconizer DLL// FileName : log.h// Author(s) : Bart Gysens//// Description : Declaration of CLog class//// Classes : CLog//// Information :// Compiler(s) : Visual C++ 6.0// Target(s) : Windows 95/98 and Windows NT (x86)// Editor : Visual C++ 6.0 internal editor//// History// Vers. Date Aut. Type Description// ----- ---------- ---- ------- -----------------------------------------// 1.00 22 05 1999 BG Create Original// 1.01 20 12 2000 hewen Add// 1.03 24 10 2001 hewen Add// 1.04 10 06 2003 hewen Add//===========================================================================
#ifndef _LOG_H_INCLUDED__#define _LOG_H_INCLUDED__
//===========================================================================// Macros and typedefs//===========================================================================
//===========================================================================// // Class : CLog// Author(s) : Bart Gysens//// Description : Declaration of the CLog class//// Comments : none//// History : 1.00 : Create// 1.01 : hewen Add// 1.03 : hewen Add// 1.04 : hewen Add//===========================================================================
class CLog{ // Member functions public: CLog();
public: static void PutLog( LPCTSTR pFmt, ... ); //1.01 static void SetFile(LPCTSTR FileName);//1.03 static void Delete(void);//1.04 static void PutLogBinary(VOID *pMem,DWORD Length,BOOL bTxt); static void MessageBox( LPCTSTR pFmt, ... );
};
#endif // _LOG_H_INCLUDED__
############ log.cpp #################
//===========================================================================//// HomeWork from Belgium Not licensed software // 1999 - 2000 No rights reserved////===========================================================================//// Project/Product : Iconizer DLL// FileName : log.cpp// Author(s) : Bart Gysens//// Description : Declaration of CLog class//// Classes : CLog//// Information :// Compiler(s) : Visual C++ 6.0// Target(s) : Windows 95/98 and Windows NT (x86)// Editor : Visual C++ 6.0 internal editor//// History// Vers. Date Aut. Type Description// ----- ---------- ---- ------- -----------------------------------------// 1.00 22 05 1999 BG Create Original// 1.01 20 12 2000 hewen Add// 1.03 24 10 2001 hewen Add// 1.04 16 06 2003 hewen Add My BirthDay//===========================================================================
#include "stdafx.h"#include "stdio.h"//1.01#include "stdlib.h"#include "log.h"
//===========================================================================// Macros and typedefs//===========================================================================
//===========================================================================// // Class : CLog// Author(s) : Bart Gysens//// Description : Declaration of the CLog class//// Comments : none//// History : 1.00 : Create// 1.01 : hewen Add// 1.03 : hewen Add// 1.04 : hewen Add//===========================================================================
//1.01static char gLogFileName[_MAX_PATH] = "LOG.TXT";
CLog::CLog(){}
void CLog::PutLog( LPCTSTR pFmt, ... ){//1.02#ifdef _DEBUG_LOG
FILE * pFile;//1.01 pFile = fopen( gLogFileName, "ab" );//1.00// pFile = fopen( "C://LOG.TXT", "a" );
if ( pFile != NULL ) { va_list arg; va_start( arg, pFmt ); vfprintf( pFile, pFmt, arg ); va_end( arg ); fclose( pFile ); }#endif}
//1.01void CLog::SetFile(LPCTSTR FileName){//1.02#ifdef _DEBUG_LOG strncpy(gLogFileName,FileName,_MAX_PATH);#endif}
//1.03void CLog::Delete(void){#ifdef _DEBUG_LOG DeleteFile(gLogFileName);#endif}
//1.04void CLog::PutLogBinary(VOID *pMem,DWORD Length,BOOL bTxt){#ifdef _DEBUG_LOG FILE * pFile; int Row, r, i, j;
if(pMem == NULL || Length <= 0) return;
pFile = fopen( gLogFileName, "ab" );
unsigned char *P = (unsigned char *)pMem;
unsigned char txtBuff[16];
if ( pFile != NULL ) { if(!bTxt) { fwrite(P,Length,1,pFile); } else { Row = Length / 16; r = Length % 16;
for(i=0;i<Row;i++) { fprintf(pFile,"x:",i); for(j=0;j<16;j++) { fprintf(pFile,"%cX",(j == 8 ? '-' : ' '),*P); txtBuff[j] = isgraph(*P) ? *P : '.'; P ++; }
fprintf(pFile," | "); fwrite(txtBuff,16,1,pFile); fprintf(pFile,"/r/n"); }
fprintf(pFile,"x:",Row); for(i=0;i<16;i++) { if(i < r) { fprintf(pFile,"%cX",(j == 8 ? '-' : ' '),*P); txtBuff[j] = isgraph(*P) ? *P : '.'; } else { fprintf(pFile," "); txtBuff[j] = ' '; } P ++; }
fprintf(pFile," | "); fwrite(txtBuff,16,1,pFile); fprintf(pFile,"/r/n"); }
fprintf(pFile,"/r/n"); fclose( pFile ); }
#endif}
//1.04#include "windows.h"void CLog::MessageBox( LPCTSTR pFmt, ... ){#ifdef _DEBUG_LOG char buff[1024]; buff[0] = 0; va_list arg; va_start( arg, pFmt ); vsprintf( buff, pFmt, arg ); va_end( arg ); AfxMessageBox(buff,MB_OK,0);#endif}