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/assert.hpp | 2 +- asl/box.hpp | 9 ++++++ asl/log/BUILD.bazel | 4 +-- asl/log/log.cpp | 52 ++++++++++++++++++++++++++++++++ asl/log/log.hpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ asl/log/log_tests.cpp | 10 +++++++ asl/utility.hpp | 12 ++++++++ 7 files changed, 169 insertions(+), 3 deletions(-) (limited to 'asl') diff --git a/asl/assert.hpp b/asl/assert.hpp index 7a3ae00..608a44f 100644 --- a/asl/assert.hpp +++ b/asl/assert.hpp @@ -6,7 +6,7 @@ namespace asl { -void report_assert_failure( const char* msg, const source_location& sl = source_location{}); +void report_assert_failure(const char* msg, const source_location& sl = source_location{}); } // namespace asl diff --git a/asl/box.hpp b/asl/box.hpp index f193853..795b6dd 100644 --- a/asl/box.hpp +++ b/asl/box.hpp @@ -90,6 +90,9 @@ public: { return H::combine(ASL_MOVE(h), *b); } + + template + friend constexpr U* leak(box&&); }; template @@ -111,5 +114,11 @@ constexpr box make_box(Args&&... args) return box(ptr, ASL_MOVE(allocator)); } +template +constexpr T* leak(box&& b) +{ + return exchange(b.m_ptr, nullptr); +} + } // namespace asl diff --git a/asl/log/BUILD.bazel b/asl/log/BUILD.bazel index 9291a8a..51f41df 100644 --- a/asl/log/BUILD.bazel +++ b/asl/log/BUILD.bazel @@ -1,10 +1,10 @@ cc_library( name = "log", srcs = [ - "log.hpp", + "log.cpp", ], hdrs = [ - "log.cpp", + "log.hpp", ], deps = [ "//asl", diff --git a/asl/log/log.cpp b/asl/log/log.cpp index e69de29..cd5d287 100644 --- a/asl/log/log.cpp +++ b/asl/log/log.cpp @@ -0,0 +1,52 @@ +#include "asl/log/log.hpp" +#include "asl/print.hpp" +#include "asl/string_builder.hpp" + +// @Todo Don't use internal get_stdout_writer, make console module + +static asl::log::DefaultLogger g_default_logger{asl::print_internals::get_stdout_writer()}; +static asl::log::Logger* g_head = &g_default_logger; + +static constexpr asl::string_view kLevelName[] = { + " DEBUG ", + " INFO ", + " WARNING ", + " ERROR ", +}; + +void asl::log::register_logger(box logger_box) +{ + auto* logger = leak(ASL_MOVE(logger_box)); + logger->m_next = exchange(g_head, logger); +} + +void asl::log::DefaultLogger::log(const message& msg) +{ + asl::format(m_writer, "[{}] {}:{}: {}\n", + kLevelName[msg.level], // NOLINT + msg.location.file, + msg.location.line, + msg.message); +} + +void asl::log::log_inner( + level l, + string_view fmt, span args, + const source_location& sl) +{ + // @Todo Use temporary allocator + StringWriter msg_writer{}; + asl::format_internals::format(&msg_writer, fmt, args); + + message m{ + .level = l, + .message = msg_writer.as_string_view(), + .location = sl, + }; + + for (auto* it = g_head; it != nullptr; it = it->next_logger()) + { + it->log(m); + } +} + 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__) diff --git a/asl/log/log_tests.cpp b/asl/log/log_tests.cpp index e69de29..01e8fd7 100644 --- a/asl/log/log_tests.cpp +++ b/asl/log/log_tests.cpp @@ -0,0 +1,10 @@ +#include +#include + +ASL_TEST(log) +{ + asl::log::log(asl::log::kInfo, asl::source_location{}, "Hello, {}!", "world"_sv); + + ASL_LOG_ERROR("Oh no! {}", 42); +} + diff --git a/asl/utility.hpp b/asl/utility.hpp index 51084e1..f3b94c3 100644 --- a/asl/utility.hpp +++ b/asl/utility.hpp @@ -81,4 +81,16 @@ constexpr bool is_pow2(isize_t v) ASL_DELETE_COPY(T) \ ASL_DELETE_MOVE(T) +#define ASL_DEFAULT_COPY(T) \ + T(const T&) = default; \ + T& operator=(const T&) = default; + +#define ASL_DEFAULT_MOVE(T) \ + T(T&&) = default; \ + T& operator=(T&&) = default; + +#define ASL_DEFAULT_COPY_MOVE(T) \ + ASL_DEFAULT_COPY(T) \ + ASL_DEFAULT_MOVE(T) + } // namespace asl -- cgit