#pragmaOnce/********************************************************************created:2014/05/12created:12:5:2014 20:2 7filename:d:\mfcui\log\log\log.hfile path:d:\mfcui\log\logfile Base:logfile ext:hauthor:bj.ypurpose:******* **************************************************************/#ifndef log_h__#defineLog_h__//#ifndef _windows_//#include <windows.h>////#endif#include <afxwin.h>#include<assert.h>#include<stdio.h>#defineContent_title 32#defineLog_info (format, ...) do \{clog::getinstance (). LOGF (""Format"File:"__file__", Function:"__function__", line:%05d", # #__VA_ARGS__, __line__); } while(0)classclog{Private: //ConstructorCLog (void); //destructor~clog (void); //copy ConstructorCLog (ConstCLog &); //overloaded ConstCLog &operator= (ConstCLog &);Private: critical_section m_lock; //File DescriptorFILE*M_file; //Create timeSYSTEMTIME M_time; //Content Title CharSzcontenttitle[content_title];Private: //Get file HandleFILE*GETFILEFP (); Public: //Get Instance StaticCLog &getinstance (); //Log a formate string voidLOGF (Const Char*Lpformat, ...);};#endif //log_h__
#include"Log.h"#include<stdarg.h>//************************************//Method:clog//Fullname:clog::clog//access:private//Returns://Qualifier://parameter:void//************************************Clog::clog (void) {M_file=NULL; :: InitializeCriticalSection (&m_lock);}//************************************//Method: ~clog//Fullname:clog::~clog//access:private//Returns://Qualifier://parameter:void//************************************CLog::~clog (void) {::D eletecriticalsection (&m_lock);}//************************************//method:getinstance//fullname:clog::getinstance//Access:public//Returns:clog &//Qualifier://************************************CLog&clog::getinstance () {StaticCLog M_log; returnM_log;}//************************************//Method:logf//Fullname:clog::logf//Access:public//returns:void//Qualifier://Parameter:const char * lpformat//Parameter: ...//************************************voidCLOG::LOGF (Const Char*Lpformat, ...) {__try {//Enter the lock:: EnterCriticalSection (&M_lock); GETFILEFP (); ASSERT (M_file!=NULL); DWORD Dwwrite=0; //Write the timeFwrite (Szcontenttitle,sizeof(Char), strlen (Szcontenttitle), m_file); //:: WriteFile (M_file, Szcontenttitle, strlen (Szcontenttitle), &dwwrite, NULL); //Write the contentva_list args; Va_start (args, Lpformat); vfprintf (M_file, Lpformat, args); Va_end (args); //write the Enter under Windowsfwrite ("\n\r",sizeof(Char),1, M_file); //:: WriteFile (M_file, "\ r \ n", 2, &dwwrite, NULL); return; } __finally {:: LeaveCriticalSection (&M_lock); }}file*CLOG::GETFILEFP () {SYSTEMTIME sys; Getlocaltime (&SYS); sprintf_s (Szcontenttitle, Content_title,"%02d:%02d:%02d%02d:%02d:%02d:%d->", Sys.wyear, Sys.wmonth, Sys.wday, Sys.whour, Sys.wminute, Sys.wsecond, sys.wmilliseconds); if(Sys.wday = =m_time.wday) { returnM_file; } if(M_file! =NULL) {fclose (m_file); } m_time=SYS; Charszfilename[ +] = {0 }; sprintf_s (szFileName,sizeof(szFileName),"%02d-%02d-%02d.txt", M_time.wyear, M_time.wmonth, M_time.wday); Fopen_s (&m_file, szFileName,"A +"); //m_file = CreateFile (szFileName, generic_write, File_share_read | File_share_write, NULL, open_always, NULL, or NULL); //SetFilePointer (m_file, 0, NULL, file_end); returnM_file;}
Use of the C + + multithreaded log class