Write to log log file using Ostream stream creation
Use macros to configure file size and interval when creating a file time interval or a file size greater than the specified number creates a new file
File names are automatically named by time
/************************************************************** Technology Blog http://www.cnblogs.com/itdef/
Technology Exchange Group
Group number: 324164944
Welcome C C + + Windows Driver Enthusiast Server programmer communication **************************************************************/
#include"stdafx.h"#include"StreamLog.h"#include<sstream>namespaceDeftools {logfile::logfile ():p Ofslogfile_ (NULL) {filepath_=""; Filename_= Filepath_ +Makefilename (); } logfile::logfile (ConstSTD::string&FilePath): Pofslogfile_ (NULL) {filepath_=FilePath; Filename_= Filepath_ +Makefilename (); } ConstSTD::stringLogfile::makefilename () {structTM T =tm_. Getcurrentdatatime (); Std::stringstream s; STD::stringstr; S<< T.tm_year +1900<<"_"<< T.tm_mon +1<<"_"<< T.tm_mday <<"_"<< T.tm_hour <<"_"<<T.tm_min<<"_"<< t.tm_sec <<". Log"; S>>str; returnstr; } BOOLLogfile::createfile () {BOOLBRet =false; Try{Pofslogfile_=NewStd::ofstream (Filename_.c_str (), Std::ios_base::app); if(NULL = = Pofslogfile_ | | pofslogfile_->Bad ()) {Pofslogfile_=NULL; Std::exception E ("Open Ofstream Error"); Throwe; } tm_. Setbegintime (); BRet=true; } Catch(Conststd::exception&error) {Std::cerr<< error.what () <<Std::endl; } returnBRet; } BOOLLogfile::initfunc () {returnCreateFile (); } LogFile::~LogFile () {if(NULL! =Pofslogfile_) {Pofslogfile_-Close (); Delete Pofslogfile_; } } voidLogfile::checkfile () {tm_. Setendtime (); if(Tm_. Getdeltatime () > Default_delta_time) | |GetFileSize ()>default_file_size) { if(NULL! =Pofslogfile_) {Pofslogfile_-Close (); Delete Pofslogfile_; Pofslogfile_=NULL; } filename_= Filepath_ +Makefilename (); CreateFile (); } } ConstSTD::stringlogfile::getcurrenttimestring () {structTM T =tm_. Getcurrentdatatime (); Std::stringstream s; STD::stringstr; STD::stringStrtime; S<< T.tm_year +1900<<"-"<< T.tm_mon +1<<"-"<<T.tm_mday; S>>str; S.clear (); S<< T.tm_hour <<":"<<T.tm_min<<":"<< t.tm_sec <<"\t\t"; S>>Strtime; STR+=" "; STR+=Strtime; returnstr; } std::stringLogfile::getlevelstring (ConstWritelevel WL) {std::stringLevelstr; Switch(WL) { CaseNormal_l:levelstr="[Normal]\t"; Break; CaseWarning_l:levelstr="[Warning]\t"; Break; CaseError_l:levelstr="[Error]\t"; Break; CaseUnknown_l:levelstr="[Unknown]\t"; Break; default: Levelstr="[???????] \ t"; Break; } returnLevelstr; } BOOLLogfile::writelog (std::stringWRTIESTR,ConstWritelevel WL) { BOOLBRet =false; Checkfile (); if(!Pofslogfile_) { returnBRet; } (*pofslogfile_) << getcurrenttimestring () << getlevelstring (WL) << wrtiestr <<Std::endl; BRet=true; returnBRet; }}//namespace Deftools
View Code
#pragmaOnce#include<time.h>#include<string>#include<fstream>#include<iostream>#include<string>namespaceDeftools {#defineDefault_delta_time (60*2)//Log Switching time#defineDefault_file_size (1024*1024*1024)//Log switch File sizetypedefenumWrite_level {normal_l=0, warning_l, error_l, unknown_l} Writelevel; //time management times and timing and date classLogTime { Public: LogTime (): Timebegin_ (Time (null)), Timeend_ (Time (null)) {time_t T=Time (NULL); localtime_s (&tm_, &t); } voidSetbegintime () {timebegin_ =Time (NULL);} voidSetendtime () {timeend_ =Time (NULL);} time_t Getbegintime () {returnTimebegin_;} time_t Getendtime () {returnTimeend_;} time_t Getdeltatime () {returnTimeend_-Timebegin_;} structTM Getcurrentdatatime () {time_t T=Time (NULL); localtime_s (&tm_, &t); returntm_; }; Private: time_t timebegin_; time_t Timeend_; structTM Tm_; }; classLogFile { Public: LogFile (); LogFile (ConstSTD::string&FilePath); Virtual~LogFile (); BOOLInitfunc (); Std::streampos GetFileSize () {if(!pofslogfile_) {return 0; }returnPofslogfile_->TELLP ();}; voidcheckfile (); BOOLWritelog (ConstSTD::stringWRTIESTR,ConstWritelevel WL =normal_l); //============================ voidwritefiletest () {checkfile (); if(!Pofslogfile_) { return; } (*pofslogfile_) <<"Test 1234"<<Std::endl; } //============================ Private: BOOLCreateFile (); ConstSTD::stringgetcurrenttimestring (); ConstSTD::stringMakefilename (); STD::stringLogfile::getlevelstring (Constwritelevel WL); STD::stringfilename_; STD::stringFilepath_; Std::ofstream*Pofslogfile_; LogTime tm_; };}//namespace Deftools
View Code
C + + log exercise