From b3f2336e1b8f4410515344feb73d992d854c8282 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 26 Jan 2025 22:35:47 +0100 Subject: Implement logging --- asl/log/log.hpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'asl/log/log.hpp') diff --git a/asl/log/log.hpp b/asl/log/log.hpp index e69de29..a6b8f03 100644 --- a/asl/log/log.hpp +++ b/asl/log/log.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include +#include +#include + +namespace asl::log +{ + +enum level : uint8_t +{ + kDebug = 0, + kInfo, + kWarning, + kError, +}; + +struct message +{ + level level; + string_view message; + source_location location; +}; + +// @Todo Write and use an intrusive doubly-linked list +class Logger +{ + Logger* m_next{}; + +public: + Logger() = default; + ASL_DEFAULT_COPY_MOVE(Logger); + virtual ~Logger() = default; + + virtual void log(const message&) = 0; + + friend void register_logger(box); + + constexpr Logger* next_logger() const { return m_next; } +}; + +// @Todo Make a deref_as trait & deref utility +// @Todo Accept writer as box, pointer, reference, or value +class DefaultLogger : public Logger +{ + Writer* m_writer; + +public: + explicit constexpr DefaultLogger(Writer* writer) : m_writer{writer} {} + + void log(const message&) override; +}; + +void register_logger(box); + +// @Todo Add a way to remove loggers (including all) + +template +requires constructible_from && convertible_from +void register_logger(Args&&... args) +{ + register_logger(make_box(ASL_FWD(args)...)); +} + +void log_inner(level l, string_view fmt, span args, const source_location& sl); + +template +void log(level l, const source_location& sl, string_view fmt, const Args&... args) +{ + format_internals::type_erased_arg type_erased_args[] = { + format_internals::type_erased_arg(args)... + }; + log_inner(l, fmt, type_erased_args, sl); +} + +} // namespace asl::log + +// @Todo Compile-time configuration of logging + +#define ASL_LOG_DEBUG(...) ::asl::log::log(::asl::log::kDebug, ::asl::source_location{}, __VA_ARGS__) +#define ASL_LOG_INFO(...) ::asl::log::log(::asl::log::kInfo, ::asl::source_location{}, __VA_ARGS__) +#define ASL_LOG_WARNING(...) ::asl::log::log(::asl::log::kWarning, ::asl::source_location{}, __VA_ARGS__) +#define ASL_LOG_ERROR(...) ::asl::log::log(::asl::log::kError, ::asl::source_location{}, __VA_ARGS__) -- cgit