123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #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 <typename... Args>
- 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 <typename... Args>
- 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 <typename... Args>
- static void Warning (const char * format, Args... args)
- {
- if (LogLevel () > 3) return;
- TLSLog::Format (format, args...);
- TLSFlush (CSystemLog::STY_WARNING);
- }
- template <typename... Args>
- static void Error (const char * format, Args... args)
- {
- if (LogLevel () > 4) return;
- TLSLog::Format (format, args...);
- TLSFlush (CSystemLog::STY_ERROR);
- }
- template <typename... Args>
- static void Fatal (const char * format, Args... args)
- {
- if (LogLevel () > 5) return;
- TLSLog::Format (format, args...);
- TLSFlush (CSystemLog::STY_CRITICAL);
- }
- // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息
- template <typename... Args>
- static void Force (const char * format, Args... args)
- {
- if (sizeof...(args) == 0)
- Flush (format);
- else
- {
- TLSLog::Format (format, args...);
- TLSFlush (CSystemLog::STY_DEBUG);
- }
- }
- template <typename... Args>
- 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 <typename... Args>
- 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 <typename... Args>
- static void Warning (const std::string & format, Args... args)
- {
- if (LogLevel () > 3) return;
- TLSLog::Format (format.c_str (), args...);
- TLSFlush (CSystemLog::STY_WARNING);
- }
- template <typename... Args>
- static void Error (const std::string & format, Args... args)
- {
- if (LogLevel () > 4) return;
- TLSLog::Format (format.c_str (), args...);
- TLSFlush (CSystemLog::STY_ERROR);
- }
- template <typename... Args>
- static void Fatal (const std::string & format, Args... args)
- {
- if (LogLevel () > 5) return;
- TLSLog::Format (format.c_str (), args...);
- TLSFlush (CSystemLog::STY_CRITICAL);
- }
- // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息
- template <typename... Args>
- 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 ("<Error Code : {$} (0x{$:x})> = ", 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);
- }
- };
- }
- }
|