版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026江阴美术面试题及答案
- 人工智能风控体系构建
- 2026年内蒙古自治区包头市网格员招聘笔试备考题库及答案详解
- 2026年浙江省杭州市住房和城乡建设局人员招聘考试备考题库及答案详解
- 人工智能在银行客户画像中的作用-第12篇
- 2026年聊城市妇幼保健院公开招聘备案制工作人员(7人)考试参考题库及答案详解
- 2026年承德市双桥区住房和城乡建设局人员招聘笔试备考试题及答案详解
- 2026中国科学院上海硅酸盐研究所高技术处主管岗位招聘1人考试参考题库及答案详解
- 2026四川成都市选调公务员90人考试模拟试题及答案详解
- 2026库尔勒陆港商贸物流(集团)有限公司管理岗位公开选聘(8人)考试模拟试题及答案详解
- DZ/T 0089-1993地质钻探用钻塔技术条件
- 雇佣兵中文合同协议
- 2025年锂电池安全生产管理和风险辨识手册
- 贵州师范学院《人工智能时代的生物基材料前沿》2023-2024学年第二学期期末试卷
- 管道振动的主要原因、危害及消除措施
- 第一 二章综合测试卷 北师大版八年级数学上册
- 溺水的预防与急救 课件 2024-2025学年人教版(2024)初中体育与健康七年级全一册
- 100以内两位数进位加法退位减法计算题-(直接打印版)
- 血透室医疗垃圾分类
- 手术预防传染病
- 小学三年级奥数题100道带答案
评论
0/150
提交评论