3#include <fmt/ostream.h>
12namespace PixelBullet::Log
24 inline constexpr std::array<const char*, static_cast<size_t>(Level::Count)> LEVEL_STRINGS = {
"TRACE:",
"INFO:",
28 inline constexpr const char* ToString(Level level)
30 return LEVEL_STRINGS[
static_cast<size_t>(level)];
36 virtual ~Sink() =
default;
38 virtual void Log(
const std::string& channelName,
const std::string& message, Level level) = 0;
40 void SetMinLevel(Level level)
44 Level GetMinLevel()
const
50 Level m_MinLevel = Level::Trace;
59 m_MinLevel = minLevel;
62 void Log(
const std::string& channelName,
const std::string& message, Level level)
override;
68 explicit FileSink(
const std::string& filename, Level minLevel = Level::Trace);
71 void Log(
const std::string& channelName,
const std::string& message, Level level)
override;
74 std::ofstream m_FileStream;
82 static std::shared_ptr<Channel>& Core();
83 static std::shared_ptr<Channel>& Client();
85 template <
typename... Args>
86 void Log(Level level, fmt::format_string<Args...> fmtStr, Args&&... args)
88 std::string message = fmt::format(fmtStr, std::forward<Args>(args)...);
90 for (
auto& sink : m_Sinks)
92 sink->Log(m_Name, message, level);
100 explicit Channel(std::string name);
102 void AddSink(
const std::shared_ptr<Sink>& sink);
105 std::vector<std::shared_ptr<Sink>> m_Sinks;
107 static std::shared_ptr<Channel> s_CoreChannel;
108 static std::shared_ptr<Channel> s_ClientChannel;
111 template <
typename... Args>
112 void Trace(fmt::format_string<Args...> fmtStr, Args&&... args)
114 Channel::Core()->Log(Level::Trace, fmtStr, std::forward<Args>(args)...);
117 template <
typename... Args>
118 void Info(fmt::format_string<Args...> fmtStr, Args&&... args)
120 Channel::Core()->Log(Level::Info, fmtStr, std::forward<Args>(args)...);
123 template <
typename... Args>
124 void Warn(fmt::format_string<Args...> fmtStr, Args&&... args)
126 Channel::Core()->Log(Level::Warn, fmtStr, std::forward<Args>(args)...);
129 template <
typename... Args>
130 void Error(fmt::format_string<Args...> fmtStr, Args&&... args)
132 Channel::Core()->Log(Level::Error, fmtStr, std::forward<Args>(args)...);
135 template <
typename... Args>
136 void Critical(fmt::format_string<Args...> fmtStr, Args&&... args)
138 Channel::Core()->Log(Level::Critical, fmtStr, std::forward<Args>(args)...);
145 template <
typename... Args>
146 void Trace(fmt::format_string<Args...> fmtStr, Args&&... args)
148 PixelBullet::Log::Channel::Client()->Log(PixelBullet::Log::Level::Trace, fmtStr, std::forward<Args>(args)...);
151 template <
typename... Args>
152 void Info(fmt::format_string<Args...> fmtStr, Args&&... args)
154 PixelBullet::Log::Channel::Client()->Log(PixelBullet::Log::Level::Info, fmtStr, std::forward<Args>(args)...);
157 template <
typename... Args>
158 void Warn(fmt::format_string<Args...> fmtStr, Args&&... args)
160 PixelBullet::Log::Channel::Client()->Log(PixelBullet::Log::Level::Warn, fmtStr, std::forward<Args>(args)...);
163 template <
typename... Args>
164 void Error(fmt::format_string<Args...> fmtStr, Args&&... args)
166 PixelBullet::Log::Channel::Client()->Log(PixelBullet::Log::Level::Error, fmtStr, std::forward<Args>(args)...);
169 template <
typename... Args>
170 void Critical(fmt::format_string<Args...> fmtStr, Args&&... args)
172 PixelBullet::Log::Channel::Client()->Log(PixelBullet::Log::Level::Critical, fmtStr,
173 std::forward<Args>(args)...);