123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- #pragma once
- #define __USING_LOG4CPP__
- #include <memory>
- #include <list>
- #include <functional>
- #include "String.DString.hpp"
- #include "String.Format.tlh"
- #ifdef Log4CPP_EXPORTS
- #define Log4CPP_API _declspec(dllexport)
- #else
- #define Log4CPP_API _declspec(dllimport)
- #endif
- #include "Log4CPP.Define.hpp"
- #include "Log4CPP.LogLevel.hpp"
- #include "Log4CPP.Appender.hpp"
- #include "Log4CPP.TLSLog.hpp"
- #ifndef Log4CPP_EXPORTS
- #ifdef _WIN64
- #ifdef _DEBUG
- #pragma comment (lib, "Log4CPP64D.lib")
- #pragma comment (lib, "Log4CPP.TLSLog64D.lib")
- #pragma comment (lib, "ECOM.Utility.DString64D.lib")
- #else
- #pragma comment (lib, "801.Log4CPP64.lib")
- #pragma comment (lib, "801.Log4CPP.TLSLog64.lib")
- #pragma comment (lib, "801.ECOM.Utility.DString64.lib")
- #endif
- #else // X86
- #ifdef _DEBUG
- #pragma comment (lib, "Log4CPPD.lib")
- #pragma comment (lib, "Log4CPP.TLSLogD.lib")
- #else
- #pragma comment (lib, "Log4CPP.lib")
- #pragma comment (lib, "Log4CPP.TLSLog.lib")
- #endif
- #endif
- #endif // Log4CPP_EXPORTS
- //-----------------------------------------------------------------------------
- // Logger
- //-----------------------------------------------------------------------------
- namespace Log4CPP
- {
- namespace Detail
- {
- class Logger_Detail;
- }
- };
- namespace Log4CPP
- {
- class Log4CPP_API Logger
- {
- public:
- Logger ();
- Logger (const char * Name);
- Logger (const char * Name, const char * DisplayName);
- Logger (const Logger & from);
- Logger (Logger && from);
- ~Logger ();
- public:
- Logger & operator = (const Logger & from);
- Logger & operator = (Logger && from);
- public:
- void Close ();
- public:
- //< const char *
- // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息
- template <typename... Args>
- inline void Force (const char * fmt, Args && ... args)
- {
- DoLog ((int) enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Verbose (const char * fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enVerbose) return;
- DoLog ((int) enLogLevel::enVerbose, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Trace (const char * fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enTrace) return;
- DoLog ((int)enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Debug (const char * fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enDebug) return;
- DoLog ((int) enLogLevel::enDebug, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Info (const char* fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enInfo) return;
- DoLog ((int) enLogLevel::enInfo, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Notice (const char* fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enNotice) return;
- DoLog ((int) enLogLevel::enNotice, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Warn (const char * fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enWarn) return;
- DoLog ((int) enLogLevel::enWarn, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Error (const char * fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enError) return;
- DoLog ((int) enLogLevel::enError, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Fatal (const char * fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enFatal) return;
- DoLog ((int) enLogLevel::enFatal, fmt, std::forward <Args> (args)...);
- }
- //>
- //< StringView
- template <typename... Args>
- inline void Force (const eSTR::StringView & fmt, Args && ... args)
- {
- DoLog ((int) enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Trace (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enTrace) return;
- DoLog ((int) enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Debug (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enDebug) return;
- DoLog ((int) enLogLevel::enDebug, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Info (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enInfo) return;
- DoLog ((int) enLogLevel::enInfo, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Notice (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enNotice) return;
- DoLog ((int) enLogLevel::enNotice, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Warn (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enWarn) return;
- DoLog ((int) enLogLevel::enWarn, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Error (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enError) return;
- DoLog ((int) enLogLevel::enError, fmt, std::forward <Args> (args)...);
- }
- template <typename... Args>
- inline void Fatal (const eSTR::StringView & fmt, Args && ... args)
- {
- if (GetLevel () > enLogLevel::enFatal) return;
- DoLog ((int) enLogLevel::enFatal, fmt, std::forward <Args> (args)...);
- }
- //>
- template <typename... Args>
- inline void Log (int Level, const char * fmt, Args && ... args)
- {
- if (GetLevel () > Level) return;
- DoLog (Level, fmt, std::forward <Args> (args)...);
- }
- bool Decide (int Level) const
- {
- return (GetLevel () <= Level);
- }
- //< 以下 3 个函数中, if (tbuf [0]) 及最后的 tbuf [0] = 0, 是为了避免重入
- // 在 Appender.BUS 及 Appender.UDP 中, 执行到最后, MyWriter::WriteLine () 函数体的最后一行
- // 有可能再次调用 mLog::XX (), 这有可能再次到这里, tbuf 就会被破坏
- public:
- // 直接把字符串刷到 Appender 中, 不要做格式化
- inline void LogNoFormat (int Level, const char * buf, int len, bool bWithLayout = false)
- {
- if (GetLevel () > Level) return;
- auto tbuf = ThreadContext::TLSMessage::Get ();
- auto size = ThreadContext::TLSMessage::Size () - 4;
- if (tbuf [0])
- return;
- auto cch = (std::min) (size, len);
- memcpy (tbuf, buf, cch);
- tbuf [cch] = 0;
- Flush (Level, cch, tbuf, bWithLayout);
- tbuf [0] = 0;
- }
- private:
- template <typename... Args>
- inline void DoLog (int Level, const char * fmt, Args && ... args)
- {
- auto tbuf = ThreadContext::TLSMessage::Get ();
- auto size = ThreadContext::TLSMessage::Size () - 4;
- if (tbuf [0])
- return;
- auto cch = 0;
-
- if (sizeof...(args) == 0)
- {
- cch = (int) strlen (fmt);
- cch = (std::min) (cch, size);
- memcpy (tbuf, fmt, cch);
- tbuf [cch] = 0;
- }
- else
- {
- cch = ECOM::Utility::String::StringFormat (tbuf, size).Format (fmt, std::forward <Args> (args)...);
- tbuf [cch] = 0;
- }
- Flush (Level, cch, tbuf, true);
- tbuf [0] = 0;
- }
- template <typename... Args>
- inline void DoLog (int Level, const eSTR::StringView & fmt, Args && ... args)
- {
- auto tbuf = ThreadContext::TLSMessage::Get ();
- auto size = ThreadContext::TLSMessage::Size () - 4;
- if (tbuf [0])
- return;
- auto cch = 0;
-
- if (sizeof...(args) == 0)
- {
- cch = fmt.GetLength ();
- cch = (std::min) (cch, size);
- memcpy (tbuf, fmt, cch);
- tbuf [cch] = 0;
- }
- else
- {
- cch = ECOM::Utility::String::StringFormat (tbuf, size).Format (fmt, std::forward <Args> (args)...);
- tbuf [cch] = 0;
- }
- Flush (Level, cch, tbuf, true);
- tbuf [0] = 0;
- }
- //>
- public:
- void NewLine (int Level = enLogLevel::enInfo);
- public:
- TLogString GetName () const; // { return m_Name; }
- TLogString GetDisplayName () const; // { return m_DisplayName; }
- enLogLevel GetLevel () const; // { return m_Level; }
- void SetLevel (int value); // { m_Level = value; }
- void Set (const TLogString & key, const TLogString & value); // for arAppender
- public: // 对 Appender 进行迭代
- int GetNbOfAppender () const;
- int ForEachAppender (std::function <void (Appender *)> fun);
- int OnEachAppender (std::function <bool (Appender *)> fun);
- Appender * FindAppender (std::function <bool (Appender *)> Pred);
- public:
- // 添加一个没有任何特殊配置项的 Appender
- Appender * AddAppender (const char * Type, const char * Name);
- bool RemoveAppender (const char * Name);
- public:
- // 把 from 中已有的 Appender 复制给我自己. 目前主要是 ConfigManager 在使用
- void CopyAppenderFrom (const Logger & from);
- // 把 from 中已有的 Appender 添加到我自己. 目前主要是 ConfigManager 在使用
- void AddAppenderFrom (const Logger & from);
- // 把已有的 Appender 添加到 Logger 中, 目前主要是 ConfigManager 在使用
- void AddAppender (std::shared_ptr <Appender> a);
- public:
- void Flush (int Level, int cbBuf, const char * buf, bool bWithLayout);
- private:
- static tTraceID GetTraceID ();
- public:
- static TLogString ErrorCodeToString (DWORD errorCode);
- static TLogString LogLevelToString (int level);
- private:
- enLogLevel m_Level = enLogLevel::enUndefined;
- Detail::Logger_Detail * m_pDetail;
- };
- };
- //-----------------------------------------------------------------------------
- //
- //-----------------------------------------------------------------------------
- namespace Log4CPP
- {
- class Log4CPP_API LogManager
- {
- private:
- LogManager () = delete;
- public:
- static bool LoadConfigFile (const char * CfgFileName);
- static bool LoadConfigString (const char * strXML);
- static Logger * GetLogger (const char * LoggerName);
- static bool IsExist (const char * LoggerName);
- static auto NewAppender (const char * Type, const char * Name) -> std::unique_ptr <Appender>;
- static int ForEachLogger (std::function <void (Logger *)> fun);
- static void CloseAll ();
- };
- };
|