项目中需要分类日志功能,且日志由多个进程产生,所以写了这个类。
- #ifndefCAPPLOG_H_
- #defineCAPPLOG_H_
- #include"Generic.h"
- enumLOG_TYPE
- {
- LOG_DEAGNOSTIC_MSG=0x00000000,
- LOG_EVENT=0x00000001,
- LOG_ACTIVITY=0x00000002,
- LOG_ERROR=0x00000004,
- };
- classCAppLog{
- public:
- CAppLog();
- virtual~CAppLog();
- staticvoidappendDeagnosticMsg(constchar*lpszFmt,...);
- staticvoidappendEvent(constchar*lpszFmt,...);
- staticvoidappendActivity(constchar*lpszFmt,...);
- staticvoidappendError(constchar*lpszFmt,...);
- private:
- staticsem_t*s_pLogFileSem;
- staticvoidappend(LOG_TYPEemType,constchar*lpszText);
- };
- #endif/*CAPPLOG_H_*/
以下是它的实现
- /*
- *CAppLog.cpp
- *
- *Createdon:Jan1,2009
- *Author:root
- */
- #include"CAppLog.h"
- #include"CSystemHelper.h"
- #defineLOG_SEM_NAME"LogFileSem"
- sem_t*CAppLog::s_pLogFileSem=NULL;
- CAppLogg_oAppLog;
- CAppLog::CAppLog(){
- if(s_pLogFileSem==NULL)
- {
- s_pLogFileSem=sem_open(LOG_SEM_NAME,O_CREAT,0666,1);
- }
- }
- CAppLog::~CAppLog(){
- if(s_pLogFileSem!=NULL)
- {
- sem_close(s_pLogFileSem);
- s_pLogFileSem=NULL;
- }
- }
- voidCAppLog::append(LOG_TYPEemType,constchar*lpszText)
- {
- charszBuffer[10240]={0};
- intnRet=readlink("/proc/self/exe",szBuffer,sizeof(szBuffer));
- if(nRet<=0||nRet>=PATH_MAX)
- return;
- stringstrTemp(szBuffer);
- stringstrDir=strTemp.substr(0,strTemp.rfind('/')+1);
- stringstrExe=strTemp.substr(strTemp.rfind('/')+1);
- time_tulNow={0};
- time(&ulNow);
- tm*pNow=localtime(&ulNow);
- memset(szBuffer,0,sizeof(szBuffer));
- sprintf(szBuffer,"%sLog/%04d/%04d-%02d/"
- ,strDir.c_str()
- ,1900+pNow->tm_year
- ,1900+pNow->tm_year
- ,1+pNow->tm_mon
- );
- CSystemHelper::createDirectory(szBuffer);
- sprintf(szBuffer,"%sLog/%04d/%04d-%02d/%04d-%02d-%02d.log"
- ,strDir.c_str()
- ,1900+pNow->tm_year
- ,1900+pNow->tm_year
- ,1+pNow->tm_mon
- ,1900+pNow->tm_year
- ,1+pNow->tm_mon
- ,pNow->tm_mday
- );
- sem_wait(s_pLogFileSem);
- {
- ofstreamstream(szBuffer,ios::out|ios::app);
- if(stream)
- {
- charszFormated[20480]={0};
- sprintf(szFormated,"\r\n%02d[%02d:%02d:%02dPID=%05dTID=%05ldEXE=%-15s]%s"
- ,emType
- ,pNow->tm_hour
- ,pNow->tm_min
- ,pNow->tm_sec
- ,getpid()
- ,(long)syscall(SYS_gettid)
- ,strExe.c_str()
- ,lpszText
- );
- stream<<szFormated;
- stream.flush();
- stream.close();
- }
- }
- sem_post(s_pLogFileSem);
- }
- voidCAppLog::appendDeagnosticMsg(constchar*lpszFmt,...)
- {
- va_listarg;
- va_start(arg,lpszFmt);
- charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
- CAppLog::append(LOG_DEAGNOSTIC_MSG,szBuffer);
- }
- voidCAppLog::appendEvent(constchar*lpszFmt,...)
- {
- va_listarg;
- va_start(arg,lpszFmt);
- charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
- CAppLog::append(LOG_EVENT,szBuffer);
- }
- voidCAppLog::appendActivity(constchar*lpszFmt,...)
- {
- va_listarg;
- va_start(arg,lpszFmt);
- charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
- CAppLog::append(LOG_ACTIVITY,szBuffer);
- }
- voidCAppLog::appendError(constchar*lpszFmt,...)
- {
- va_listarg;
- va_start(arg,lpszFmt);
- charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
- CAppLog::append(LOG_ERROR,szBuffer);
- }
使用起来很简单,将类加入到工程,然后在需要的地方包含此类的头文件,然后直接调用静态方法。这里用到了一个辅助类的函数 CSystemHelper::createDirectory,它的实现如下:
- voidCSystemHelper::createDirectory(constchar*lpszDir)
- {
- stringstrDir(lpszDir);
- intnIndex=0;
- while(nIndex!=string::npos)
- {
- intnFind=strDir.find("/",nIndex+1);
- stringstrTemp=strDir.substr(0,nFind+1);
- DIR*dir=opendir(strTemp.c_str());
- if(dir!=NULL)
- closedir(dir);
- else
- mkdir(strTemp.c_str(),S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
- nIndex=nFind;
- }
- }