8#ifndef GALAXY_LOGGING_LOG_HPP_
9#define GALAXY_LOGGING_LOG_HPP_
12#include <source_location>
15#include <entt/locator/locator.hpp>
16#include <magic_enum/magic_enum.hpp>
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__)
60 template<std::derived_from<Sink> SinkTo,
typename... Args>
71 template<LogLevel level>
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);
121 template<std::derived_from<Sink> SinkTo,
typename... Args>
124 m_sinks.push_back(std::make_unique<SinkTo>(std::forward<Args>(args)...));
125 SinkTo* ptr =
static_cast<SinkTo*
>(
m_sinks.back().get());
129 template<LogLevel level>
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)
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(),
150 .message = std::vformat(message, std::make_format_args(args...)),
151 .trace = std::to_string(trace)
156 for (
const auto& sink :
m_sinks)
164 throw std::runtime_error(lm.trace);
Sink based logging system.
Log & operator=(Log &&)=delete
Move assignment operator.
Log() noexcept
Constructor.
Log & operator=(const Log &)=delete
Copy assignment operator.
Log(Log &&)=delete
Move constructor.
~Log() noexcept
Destructor.
std::vector< std::unique_ptr< Sink > > m_sinks
List of sinks.
LogLevel m_min_level
Minimum level for a message to be logged.
void log(const std::stacktrace &trace, const std::source_location &loc, std::string_view message, const MsgInputs &... args)
Log a message.
Log(const Log &)=delete
Copy constructor.
SinkTo & add_sink(Args &&... args)
Add a sink to log to.
void set_min_level() noexcept
Set a minimum log level.
LogLevel
Used to determine filtering and colouring of log messages.
constexpr const char *const get_loglevel_colour()
Parts of a log message to be passed to sinks.