1- #pragma once
2- #ifndef USE_FMT
3- #include < format>
4- #define FORMAT std::format
5- #define VFORMAT std::vformat
6- #define MAKE_FORMAT_ARGS std::make_format_args
7- #else
8- #include < fmt/core.h>
9- #define FORMAT fmt::format
10- #define VFORMAT fmt::vformat
11- #define MAKE_FORMAT_ARGS fmt::make_format_args
12- #endif
13- #include < functional>
14- #include " concurrency/shared_queue.hpp"
15- #include " LogCapture.hpp"
16- #include " LogIntermediate.hpp"
17- #include " LogLevel.hpp"
18- #include " LogMessage.hpp"
19-
20- namespace al
21- {
22- using LogMessagePtr = std::shared_ptr<LogMessage>;
23- using LogSink = std::function<void (LogMessagePtr)>;
24-
25- class Logger
26- {
27- public:
28- Logger () = default ;
29- virtual ~Logger () = default ;
30-
31- static void AddSink (LogSink sink);
32- static void Destroy ();
33- static void Init ();
34-
35- protected:
36- static void PushMessage (const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message) noexcept ;
37-
38- private:
39- void CallSinks (LogMessagePtr msgPtr);
40- void DestroyImpl ();
41- void InitImpl ();
42- void QueueMessage (LogMessagePtr msgPtr);
43-
44- static Logger& GetInstance ()
45- {
46- static Logger i{};
47- return i;
48- }
49-
50- private:
51- friend LogCapture::~LogCapture ();
52-
53- shared_queue<std::function<void ()>> m_Queue;
54- std::vector<LogSink> m_Sinks;
55- bool m_Running = false ;
56- std::thread m_LogWorker;
57-
58- };
59-
60- extern LogCapture LOG (const eLogLevel level, std::source_location location = std::source_location::current());
61-
62- template <typename ...Args>
63- inline void LOGF (const eLogLevel level, LogIntermediate formatString, Args&&... formatArgs)
64- {
65- auto capture = LogCapture (level, std::move (formatString.Location ()));
66- capture << VFORMAT (formatString.FormatString (), MAKE_FORMAT_ARGS (std::forward<Args>(formatArgs)...));
67- }
1+ #pragma once
2+ #ifndef USE_FMT
3+ #include < format>
4+ #define FORMAT std::format
5+ #define VFORMAT std::vformat
6+ #define MAKE_FORMAT_ARGS std::make_format_args
7+ #else
8+ #include < fmt/core.h>
9+ #define FORMAT fmt::format
10+ #define VFORMAT fmt::vformat
11+ #define MAKE_FORMAT_ARGS fmt::make_format_args
12+ #endif
13+ #include < functional>
14+ #include " concurrency/shared_queue.hpp"
15+ #include " LogCapture.hpp"
16+ #include " LogIntermediate.hpp"
17+ #include " LogLevel.hpp"
18+ #include " LogMessage.hpp"
19+
20+ namespace al
21+ {
22+ using LogMessagePtr = std::shared_ptr<LogMessage>;
23+ using LogSink = std::function<void (LogMessagePtr)>;
24+
25+ class Logger
26+ {
27+ public:
28+ Logger () = default ;
29+ virtual ~Logger () = default ;
30+
31+ static void AddSink (LogSink sink);
32+ static void Destroy ();
33+ static void Init ();
34+ /* *
35+ * @brief Attempts to flush the log queue immediately until it's completely empty.
36+ */
37+ static void FlushQueue ();
38+
39+ protected:
40+ static void PushMessage (const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message) noexcept ;
41+
42+ private:
43+ void CallSinks (LogMessagePtr msgPtr);
44+ void DestroyImpl ();
45+ void InitImpl ();
46+ /* *
47+ * @brief Attempts to flush the entire log queue before returning
48+ */
49+ void FlushQueueImpl ();
50+ void QueueMessage (LogMessagePtr msgPtr);
51+
52+ static Logger& GetInstance ()
53+ {
54+ static Logger i{};
55+ return i;
56+ }
57+
58+ private:
59+ friend LogCapture::~LogCapture ();
60+
61+ shared_queue<std::function<void ()>> m_Queue;
62+ std::vector<LogSink> m_Sinks;
63+ bool m_Running = false ;
64+ std::thread m_LogWorker;
65+
66+ };
67+
68+ extern LogCapture LOG (const eLogLevel level, std::source_location location = std::source_location::current());
69+
70+ template <typename ...Args>
71+ inline void LOGF (const eLogLevel level, LogIntermediate formatString, Args&&... formatArgs)
72+ {
73+ auto capture = LogCapture (level, std::move (formatString.Location ()));
74+ capture << VFORMAT (formatString.FormatString (), MAKE_FORMAT_ARGS (std::forward<Args>(formatArgs)...));
75+ }
6876}
0 commit comments