欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    C++日志记录类logging.docx

    • 资源ID:96178619       资源大小:15.75KB        全文页数:7页
    • 资源格式: DOCX        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    C++日志记录类logging.docx

    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 <iostream>#include <sstream>#include <string>#include <cstring>#include <fstream> #ifdef _WIN32#include <windows.h>#include <shlobj.h>#include <shlwapi.h>#pragma comment(lib, "shlwapi.lib")#else#include <locale.h>#include <string.h>#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(logging) 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<unsigned long>(_LOG_ALL_);#else/release版本 不记录任何信息._declspec(selectany) volatile unsigned long g_dwLogLevel = static_cast<unsigned long>(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_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_pwszLogLevel3;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, 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_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, wszLogFile, NULL);/创建多级文件夹return TRUE;public: static wchar_t* GetLogFileName(void)#if defined(_MSC_VER)static wchar_twszLogFileMAX_PATH = 0;if( ! wszLogFile0)LPITEMIDLISTlpIdl = NULL;if(S_OK = :SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &lpIdl) && lpIdl):SHGetPathFromIDListW(lpIdl, wszLogFile);:PathAppendW(wszLogFile, L"DouXieDxGame.log");:CoTaskMemFree(static_cast<void*>(lpIdl); return wszLogFile;#else/TODO#endif #if defined(_MSC_VER)/这个类用于保留Last error值/ Stores the current value of GetLastError in the constructor and restores/ it in the destructor by calling SetLastError./ This is useful since the LogMessage class uses a lot of Win32 calls/ that will lose the value of GLE and the code that called the log function/ will have lost the thread error value when the log call returns.class CSaveLastError public:CSaveLastError() m_dwLastErrorCode = :GetLastError(); CSaveLastError() :SetLastError(m_dwLastErrorCode); unsigned long GLE() const return m_dwLastErrorCode; protected:unsigned long m_dwLastErrorCode;CSaveLastError m_last_error;#endif public:static unsigned long SetLogLevel(unsigned long dwNewLevel)unsigned long old = g_dwLogLevel;g_dwLogLevel = dwNewLevel;return (old);static unsigned long GetLogLevel(void)return (g_dwLogLevel); / This class is used to explicitly ignore values in the conditional/ logging macros. This avoids compiler warnings like "value computed/ is not used" and "statement has no effect".class VoidifyLog public:VoidifyLog() / This has to be an operator with a precedence lower than << but/ higher than ?:void operator&(std:ostream&) ; #define LOG_IS_ON(severity) (severity & :logging:g_dwLogLevel) #define LAZY_STREAM(stream, condition) !(condition) ? (void) 0 : :logging:VoidifyLog() & (stream) #define LOG_STREAM(severity) :logging:log(severity, _FILE_, _LINE_).stream() #ifdef _NO_LOG_#define LOG(level) LAZY_STREAM(LOG_STREAM(:logging:LOG_ # level), (0)#define LOG_IF(level, condition) LAZY_STREAM(LOG_STREAM(:logging:LOG_ # level), (0)#else#define LOG_IF(level, condition) LAZY_STREAM(LOG_STREAM(:logging:LOG_ # level), LOG_IS_ON(:logging:LOG_ # level) && (condition)#define LOG(level) LAZY_STREAM(LOG_STREAM(:logging:LOG_ # level), LOG_IS_ON(:logging:LOG_ # level)#endif _inline BOOL ResetLogFile(void):DeleteFileW(:logging:log:GetLogFileName(); SYSTEMTIME st;:GetLocalTime(&st);char szTime128 = 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); LOG(INFO) << "Log file reset "<< szTime;return TRUE; END_NAMESPACE(logging) #endif/ #ifndef _INCLUDE_LOGGING_H_

    注意事项

    本文(C++日志记录类logging.docx)为本站会员(暗伤)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开