#pragma once #include "AppTLSLog.hpp" namespace ECOM { namespace Log { class AppLog_API ShareLog { public: ShareLog (); ShareLog (ShareLog && from); ShareLog (const ShareLog & from); private: CSystemLog * m_FileLog; CSystemLog * m_ErrorLog; int m_LogLevel; bool m_bErrorOn; friend class AppLog; }; class AppLog_API AppLog { private: static CSystemLog * FileLog; static CSystemLog * ErrorLog; public: static void EnableErrorLog (bool bSet); static void PrepareLog (const char * logFilename); static void CloseLog (); static int LogLevel (); // 此日志模块是否启用? 默认 = On static bool IsErrorLogOn (); // 此日志模块对应的错误日志是否启用? 默认 = Off static void SetMQServerLogPort (const char * szPort); static void Flush (const char * str); static void TLSFlush (CSystemLog::Severity s); static void NewLine (); public: static void Share (ShareLog & with); public: template static void Log (const char * format, Args... args) { if (LogLevel () > 1) return; if (sizeof...(args) == 0) Flush (format); else { TLSLog::Format (format, args...); TLSFlush (CSystemLog::STY_DEBUG); } } template static void Info (const char * format, Args... args) { if (LogLevel () > 2) return; if (sizeof...(args) == 0) Flush (format); else { TLSLog::Format (format, args...); TLSFlush (CSystemLog::STY_INFO); } } template static void Warning (const char * format, Args... args) { if (LogLevel () > 3) return; TLSLog::Format (format, args...); TLSFlush (CSystemLog::STY_WARNING); } template static void Error (const char * format, Args... args) { if (LogLevel () > 4) return; TLSLog::Format (format, args...); TLSFlush (CSystemLog::STY_ERROR); } template static void Fatal (const char * format, Args... args) { if (LogLevel () > 5) return; TLSLog::Format (format, args...); TLSFlush (CSystemLog::STY_CRITICAL); } // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息 template static void Force (const char * format, Args... args) { if (sizeof...(args) == 0) Flush (format); else { TLSLog::Format (format, args...); TLSFlush (CSystemLog::STY_DEBUG); } } template static void Log (const std::string & format, Args... args) { if (LogLevel () > 1) return; if (sizeof...(args) == 0) Flush (format.c_str ()); else { TLSLog::Format (format.c_str (), args...); TLSFlush (CSystemLog::STY_DEBUG); } } template static void Info (const std::string & format, Args... args) { if (LogLevel () > 2) return; if (sizeof...(args) == 0) Flush (format); else { TLSLog::Format (format.c_str (), args...); TLSFlush (CSystemLog::STY_INFO); } } template static void Warning (const std::string & format, Args... args) { if (LogLevel () > 3) return; TLSLog::Format (format.c_str (), args...); TLSFlush (CSystemLog::STY_WARNING); } template static void Error (const std::string & format, Args... args) { if (LogLevel () > 4) return; TLSLog::Format (format.c_str (), args...); TLSFlush (CSystemLog::STY_ERROR); } template static void Fatal (const std::string & format, Args... args) { if (LogLevel () > 5) return; TLSLog::Format (format.c_str (), args...); TLSFlush (CSystemLog::STY_CRITICAL); } // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息 template static void Force (const std::string & format, Args... args) { if (sizeof...(args) == 0) Flush (format); else { TLSLog::Format (format.c_str (), args...); TLSFlush (CSystemLog::STY_DEBUG); } } static DString ErrorCodeToString (DWORD errorCode) { DString rc; const int nPreAlloc = 2048; rc.GetBuffer (nPreAlloc); if (errorCode == 0) errorCode = GetLastError (); rc._XAppendFormat (" = ", errorCode, errorCode); int nCurrentLength = rc.GetLength (); char * pszBuffer = rc.GetBuffer (1); pszBuffer += nCurrentLength; int nAppendLength = nPreAlloc - nCurrentLength; ::FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorCode, // GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) pszBuffer, nAppendLength, NULL); rc.TrimRight (); // 删除尾部的回车 rc.ReleaseBuffer (); return std::move (rc); } }; } }