




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C++日志记录类logging//Copyright(c)2012Ohyonetwork.Allrightsreserved.//2012/01/04//logging.h////#ifndef__INCLUDE_LOGGING_H__#define__INCLUDE_LOGGING_H__#include"str_conv.inl"#include<iostream>#include<sstream>#include<string>#include<cstring>#include<fstream>#ifdef_WIN32# include<windows.h># include<shlobj.h># include<shlwapi.h>#pragmacomment(lib,"shlwapi.lib")#else# include<locale.h># include<string.h>#endif#ifndefBEGIN_NAMESPACE# defineBEGIN_NAMESPACE(x)namespacex{#endif#ifndefEND_NAMESPACE# defineEND_NAMESPACE(x)}#endif#ifndefDISALLOW_COPY_AND_ASSIGN# defineDISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(constTypeName&); \ voidoperator=(constTypeName&)#endifBEGIN_NAMESPACE(logging)enumLOG_LEVEL{ LOG_NONE =0, LOG_DEBUG =1 <<0, LOG_INFO =1 <<1, LOG_WARNING =1 <<2, LOG_ERROR =1 <<3, LOG_EXCEPTION =1 <<4, _LOG_ALL_ =0xFFFFFFFF};#ifdefined(_DEBUG)__declspec(selectany)volatileunsignedlongg_dwLogLevel=static_cast<unsignedlong>(_LOG_ALL_);#else// release版本不记录任何信息.__declspec(selectany)volatileunsignedlongg_dwLogLevel=static_cast<unsignedlong>(LOG_NONE);#endifconstchar*constG_pwszLogLevel[]={ "NONE", "DEBUG", "INFO", "WARNING", "ERROR", "EXCEPTION"};#ifndef__FUNCTION_W__# defineWIDEN2(x)L##x# defineWIDEN(x)WIDEN2(x)# define__FUNCTION_W__WIDEN(__FUNCTION__)#endifclasslog{private: DISALLOW_COPY_AND_ASSIGN(log);protected: LOG_LEVEL m_LogLevel; constchar *pfile; std::ostringstreamm_stream;public: explicitlog(LOG_LEVELleval,constchar*pszFile,intiLine) { m_LogLevel=leval; m_stream<<"["; switch(m_LogLevel) { caseLOG_DEBUG: m_stream<<G_pwszLogLevel[1]; break; caseLOG_INFO: m_stream<<G_pwszLogLevel[2]; break; caseLOG_WARNING: m_stream<<G_pwszLogLevel[3]; break; caseLOG_ERROR: m_stream<<G_pwszLogLevel[4]; break; caseLOG_EXCEPTION: m_stream<<G_pwszLogLevel[5]; break; default: m_stream<<G_pwszLogLevel[0]; break; } SYSTEMTIMEst; ::GetLocalTime(&st); charszTime[128]={0}; ::wnsprintfA(szTime,_countof(szTime),"[%04u-%02u-%02u_%02u:%02u:%02u_%03u_%u]", st.wYear,st.wMonth,st.wDay, st.wHour,st.wMinute,st.wSecond,st.wMilliseconds,st.wDayOfWeek); m_stream<<"]"<<szTime<<pszFile<<":"<<iLine<<""; } ~log() { if(m_LogLevel&GetLogLevel()) { m_stream<<std::endl; std::stringstr_newline(m_stream.str()); CheckLogDir(); std::ofstream outfile(GetLogFileName(),std::ios::app); outfile<<str_newline; //std::cout<<wstr_newline; } m_LogLevel=LOG_NONE; } std::ostream&stream(){returnm_stream;}private: staticBOOLCheckLogDir(void) { wchar_t wszLogFile[MAX_PATH]; ::StrCpyNW(wszLogFile,GetLogFileName(),_countof(wszLogFile)); ::PathAppendW(wszLogFile,L"..\\"); if(!::PathFileExistsW(wszLogFile))//判断文件夹是否存在 { ::SHCreateDirectoryExW(NULL,wszLogFile,NULL); //创建多级文件夹 } returnTRUE; }public: staticwchar_t*GetLogFileName(void)#ifdefined(_MSC_VER) { staticwchar_t wszLogFile[MAX_PATH]={0}; if(!wszLogFile[0]) { LPITEMIDLIST lpIdl=NULL; if(S_OK==::SHGetSpecialFolderLocation(NULL,CSIDL_PERSONAL,&lpIdl)&&lpIdl) { ::SHGetPathFromIDListW(lpIdl,wszLogFile); ::PathAppendW(wszLogFile,L"DouXie\\DxGame.log"); } ::CoTaskMemFree(static_cast<void*>(lpIdl)); } returnwszLogFile; }#else // TODO#endif#ifdefined(_MSC_VER) // // 这个类用于保留Lasterror值 // //StoresthecurrentvalueofGetLastErrorintheconstructorandrestores //itinthedestructorbycallingSetLastError. //ThisisusefulsincetheLogMessageclassusesalotofWin32calls //thatwilllosethevalueofGLEandthecodethatcalledthelogfunction //willhavelostthethreaderrorvaluewhenthelogcallreturns. classCSaveLastError{ public: CSaveLastError(){m_dwLastErrorCode=::GetLastError();} ~CSaveLastError(){::SetLastError(m_dwLastErrorCode);} unsignedlongGLE()const{returnm_dwLastErrorCode;} protected: unsignedlongm_dwLastErrorCode; }; CSaveLastErrorm_last_error;#endifpublic: staticunsignedlongSetLogLevel(unsignedlongdwNewLevel) { unsignedlongold=g_dwLogLevel; g_dwLogLevel=dwNewLevel; return(old); } staticunsignedlongGetLogLevel(void) { return(g_dwLogLevel); }};//Thisclassisusedtoexplicitlyignorevaluesintheconditional//loggingmacros.Thisavoidscompilerwarningslike"valuecomputed//isnotused"and"statementhasnoeffect".classVoidifyLog{public: VoidifyLog(){} //Thishastobeanoperatorwithaprecedencelowerthan<<but //higherthan?: voidoperator&(std::ostream&){}};#defineLOG_IS_ON(severity)\ (severity&::logging::g_dwLogLevel)#defineLAZY_STREAM(stream,condition)\ !(condition)?(void)0:::logging::VoidifyLog()&(stream)#defineLOG_STREAM(severity)::logging::log(severity,__FILE__,__LINE__).stream()#ifdef_NO_LOG_# defineLOG(level)LAZY_STREAM(LOG_STREAM(::logging::LOG_##level),(0))# defineLOG_IF(level,condition)LAZY_STREAM(LOG_STREAM(::logging::LOG_##level),(0))#else# defineLOG_IF(level,condition)LAZY_STREAM(LOG_STREAM(::logging::LOG_##level),LOG_IS_ON(::logging::LOG_##level)&&(condition))# defineLOG(level)LAZY_STREAM(LOG_STREAM(::logging::LOG_##level),LOG_IS_ON(::logging::LOG_##level))#endif_inlineBOOLResetLogFile(void){ ::DeleteFileW(::logging::log::GetLogFileName()); SYSTEMTIMEst; ::GetLocalTime(&st); charszTime[128]={0}; ::wnsprintfA(sz
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行政积分制管理暂行办法
- 西安市门头牌匾管理暂行办法
- 衡阳市重点水域管理办法
- 西丰县农村环境管理办法
- 观山湖区停车场管理办法
- 设备检修后清理管理办法
- 课件库管理办法心得体会
- 财政性资金指标管理办法
- 贵州人口生育与管理办法
- 贵州省露天煤矿管理办法
- OptiSystem-设计光纤放大器和光纤激光器-讯稷
- 初中心理健康教育活动方案(7篇)
- 《中华人民共和国监察法实施条例》测试题
- 繁峙县茶坊矿业开发有限公司3万t-a金矿开采项目 环评报告
- 2022年汽车维修工高级工(三级)理论题库-单选题库
- 摄像头图像测试(以Imatest等为主要工具)项目及简介课件
- 新教材北师大版高中英语必修第二册全册重点单词短语句型归纳总结
- POCT血糖测定授权表
- 深蓝科技风智能医疗卫生系统模板课件整理
- 消防设施操作员报名承诺书
- 复件1235接线员辅导草稿
评论
0/150
提交评论