C++日志记录类logging.docx
《C++日志记录类logging.docx》由会员分享,可在线阅读,更多相关《C++日志记录类logging.docx(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C+日志记录类logging/ Copyright (c) 2012 Ohyo net work. All rights reserved./ 2012/01/04/ logging.h/ #ifndef _INCLUDE_LOGGING_H_#define _INCLUDE_LOGGING_H_ #include str_conv.inl#include #include #include #include #include #ifdef _WIN32#include #include #include #pragma comment(lib, shlwapi.lib)#else#inclu
2、de #include #endif #ifndef BEGIN_NAMESPACE#define BEGIN_NAMESPACE(x) namespace x #endif #ifndef END_NAMESPACE#define END_NAMESPACE(x) #endif #ifndef DISALLOW_COPY_AND_ASSIGN#define DISALLOW_COPY_AND_ASSIGN(TypeName)TypeName(const TypeName&);void operator=(const TypeName&)#endif BEGIN_NAMESPACE(loggi
3、ng) enum LOG_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; #if defined(_DEBUG)_declspec(selectany) volatile unsigned long g_dwLogLevel = static_cast(_LOG_ALL_);#else/release版本 不记录任何信息._declspec(selectany) volatile unsigned l
4、ong g_dwLogLevel = static_cast(LOG_NONE);#endif const char *const G_pwszLogLevel = NONE,DEBUG,INFO,WARNING,ERROR,EXCEPTION; #ifndef _FUNCTION_W_#define WIDEN2(x) L # x#define WIDEN(x) WIDEN2(x)#define _FUNCTION_W_ WIDEN(_FUNCTION_)#endif class logprivate:DISALLOW_COPY_AND_ASSIGN(log); protected:LOG_
5、LEVELm_LogLevel;const char*pfile;std:ostringstream m_stream; public:explicit log(LOG_LEVEL leval, const char *pszFile, int iLine)m_LogLevel = leval;m_stream ;switch(m_LogLevel)case LOG_DEBUG:m_stream G_pwszLogLevel1;break;case LOG_INFO:m_stream G_pwszLogLevel2;break;case LOG_WARNING:m_stream G_pwszL
6、ogLevel3;break;case LOG_ERROR:m_stream G_pwszLogLevel4;break;case LOG_EXCEPTION:m_stream G_pwszLogLevel5;break;default:m_stream G_pwszLogLevel0;break;SYSTEMTIME st;:GetLocalTime(&st);char szTime128 = 0;:wnsprintfA(szTime, _countof(szTime), %04u-%02u-%02u_%02u:%02u:%02u_%03u_%u ,st.wYear, st.wMonth,
7、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:string str_newline(m_stream.str(); CheckLogDir(); std:ofstreamoutfile(GetLogFileName(), std:ios:app);outfile str_newline;/std:cout wstr_
8、newline;m_LogLevel = LOG_NONE; std:ostream& stream() return m_stream; private:static BOOL CheckLogDir(void)wchar_twszLogFileMAX_PATH;:StrCpyNW(wszLogFile, GetLogFileName(), _countof(wszLogFile);:PathAppendW(wszLogFile, L.); if( ! :PathFileExistsW(wszLogFile)/判断文件夹是否存在:SHCreateDirectoryExW(NULL, wszL
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 日志 记录 logging
限制150内