galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Log.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_LOGGING_LOG_HPP_
9#define GALAXY_LOGGING_LOG_HPP_
10
11#include <filesystem>
12#include <source_location>
13#include <stacktrace>
14
15#include <entt/locator/locator.hpp>
16#include <magic_enum/magic_enum.hpp>
17
21
22#define GALAXY_INFO galaxy::LogLevel::INFO
23#define GALAXY_WARNING galaxy::LogLevel::WARNING
24#define GALAXY_ERROR galaxy::LogLevel::ERROR
25#define GALAXY_FATAL galaxy::LogLevel::FATAL
26#define GALAXY_LOG_SET_MIN_LEVEL(level) entt::locator<galaxy::Log>::value().set_min_level<level>()
27#define GALAXY_ADD_SINK(sink, ...) entt::locator<galaxy::Log>::value().add_sink<sink>(__VA_ARGS__)
28#define GALAXY_LOG(level, msg, ...) entt::locator<galaxy::Log>::value().log<level>(std::stacktrace::current(), std::source_location::current(), msg __VA_OPT__(, ) __VA_ARGS__)
29
30namespace galaxy
31{
37 class Log final
38 {
39 public:
43 Log() noexcept;
44
48 ~Log() noexcept;
49
60 template<std::derived_from<Sink> SinkTo, typename... Args>
61 [[maybe_unused]]
62 SinkTo& add_sink(Args&&... args);
63
71 template<LogLevel level>
72 void set_min_level() noexcept;
73
85 template<LogLevel level, typename... MsgInputs>
86 void log(const std::stacktrace& trace, const std::source_location& loc, std::string_view message, const MsgInputs&... args);
87
88 private:
92 Log(const Log&) = delete;
93
97 Log(Log&&) = delete;
98
102 Log& operator=(const Log&) = delete;
103
107 Log& operator=(Log&&) = delete;
108
109 private:
114
118 std::vector<std::unique_ptr<Sink>> m_sinks;
119 };
120
121 template<std::derived_from<Sink> SinkTo, typename... Args>
122 inline SinkTo& Log::add_sink(Args&&... args)
123 {
124 m_sinks.push_back(std::make_unique<SinkTo>(std::forward<Args>(args)...));
125 SinkTo* ptr = static_cast<SinkTo*>(m_sinks.back().get());
126 return *ptr;
127 }
128
129 template<LogLevel level>
130 inline void Log::set_min_level() noexcept
131 {
132 m_min_level = level;
133 }
134
135 template<LogLevel level, typename... MsgInputs>
136 inline void Log::log(const std::stacktrace& trace, const std::source_location& loc, std::string_view message, const MsgInputs&... args)
137 {
138 if (level >= m_min_level)
139 {
140 // First create message obj.
141
142 // clang-format off
143 LogMessage lm
144 {
146 .level = level,
147 .time = std::format("{0:%r}", std::chrono::zoned_time {std::chrono::current_zone(), std::chrono::system_clock::now()}.get_local_time()),
148 .file = std::filesystem::path(loc.file_name()).filename().string(),
149 .line = loc.line(),
150 .message = std::vformat(message, std::make_format_args(args...)),
151 .trace = std::to_string(trace)
152 };
153 // clang-format on
154
155 // Send message to all sinks.
156 for (const auto& sink : m_sinks)
157 {
158 sink->sink(lm);
159 }
160
161 // Then throw error if its FATAL.
162 if constexpr (level == LogLevel::FATAL)
163 {
164 throw std::runtime_error(lm.trace);
165 }
166 }
167 }
168} // namespace galaxy
169
170#endif
Sink based logging system.
Definition Log.hpp:38
Log & operator=(Log &&)=delete
Move assignment operator.
Log() noexcept
Constructor.
Definition Log.cpp:14
Log & operator=(const Log &)=delete
Copy assignment operator.
Log(Log &&)=delete
Move constructor.
~Log() noexcept
Destructor.
Definition Log.cpp:20
std::vector< std::unique_ptr< Sink > > m_sinks
List of sinks.
Definition Log.hpp:118
LogLevel m_min_level
Minimum level for a message to be logged.
Definition Log.hpp:113
void log(const std::stacktrace &trace, const std::source_location &loc, std::string_view message, const MsgInputs &... args)
Log a message.
Definition Log.hpp:136
Log(const Log &)=delete
Copy constructor.
SinkTo & add_sink(Args &&... args)
Add a sink to log to.
Definition Log.hpp:122
void set_min_level() noexcept
Set a minimum log level.
Definition Log.hpp:130
Timer.hpp galaxy.
Definition Timer.cpp:18
LogLevel
Used to determine filtering and colouring of log messages.
Definition LogLevel.hpp:22
@ FATAL
Fatal Log Level.
constexpr const char *const get_loglevel_colour()
Definition LogLevel.hpp:45
Parts of a log message to be passed to sinks.