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::logging::LogLevel::_INFO_
23#define GALAXY_DEBUG galaxy::logging::LogLevel::_DEBUG_
24#define GALAXY_WARNING galaxy::logging::LogLevel::_WARNING_
25#define GALAXY_ERROR galaxy::logging::LogLevel::_ERROR_
26#define GALAXY_FATAL galaxy::logging::LogLevel::_FATAL_
27#define GALAXY_LOG_SET_MIN_LEVEL(level) entt::locator<galaxy::logging::Log>::value().set_min_level<level>()
28#define GALAXY_ADD_SINK(sink, ...) entt::locator<galaxy::logging::Log>::value().add_sink<sink>(__VA_ARGS__)
29#define GALAXY_LOG(level, msg, ...) entt::locator<galaxy::logging::Log>::value().log<level>(std::source_location::current(), msg __VA_OPT__(, ) __VA_ARGS__)
63 template<std::derived_from<Sink> SinkTo,
typename... Args>
74 template<LogLevel level>
87 template<
LogLevel level,
typename... MsgInputs>
88 void log(
const std::source_location& loc, std::string_view message,
const MsgInputs&... args);
123 template<std::derived_from<Sink> SinkTo,
typename... Args>
126 m_sinks.push_back(std::make_unique<SinkTo>(std::forward<Args>(args)...));
127 SinkTo* ptr =
dynamic_cast<SinkTo*
>(
m_sinks.back().get());
131 template<LogLevel level>
137 template<
LogLevel level,
typename... MsgInputs>
138 inline void Log::log(
const std::source_location& loc, std::string_view message,
const MsgInputs&... args)
142 auto trace = std::stacktrace::current();
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 = std::to_string(loc.line()),
150 .message = std::vformat(message, std::make_format_args(args...)),
151 .trace = std::to_string(trace)
155 lm.
level = magic_enum::enum_name<LogLevel>(level);
160 lm.colour =
"\x1b[1m\x1b[33m";
164 lm.colour =
"\x1b[1m\x1b[31m";
168 lm.colour =
"\x1b[41m\x1b[37m";
171 for (
const auto& sink :
m_sinks)
173 sink->sink_message(lm);
178 throw std::runtime_error(std::to_string(trace));
Sink based logging system.
Log & operator=(const Log &)=delete
Copy assignment operator.
Log(Log &&)=delete
Move constructor.
void set_min_level() noexcept
Set a minimum log level.
std::vector< std::unique_ptr< Sink > > m_sinks
List of sinks.
SinkTo & add_sink(Args &&... args)
Add a sink to log to.
void log(const std::source_location &loc, std::string_view message, const MsgInputs &... args)
Log a message.
LogLevel m_min_level
Minimum level for a message to be logged.
Log & operator=(Log &&)=delete
Move assignment operator.
Log(const Log &)=delete
Copy constructor.
~Log() noexcept
Destructor.
Log() noexcept
Constructor.
LogLevel
Used to determine filtering and colouring of log messages.
@ _DEBUG_
Debug Log Level.
@ _ERROR_
Error Log Level.
@ _FATAL_
Fatal Log Level.
@ _WARNING_
Warning Log Level.
void replace_all(std::string &input, std::string_view to_replace, std::string_view replace_with) noexcept
Replaces all occurrences of a string.
Parts of a log message to be passed to sinks.
std::string level
Level of message.