diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-01-26 22:35:47 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-01-26 22:45:42 +0100 |
commit | b3f2336e1b8f4410515344feb73d992d854c8282 (patch) | |
tree | 5b4de235b46a6081206b17761acc1bab1ea5c720 /asl/log/log.cpp | |
parent | cf7db48c261ee9c896c813a38ff8c59da5b8fe07 (diff) |
Implement logging
Diffstat (limited to 'asl/log/log.cpp')
-rw-r--r-- | asl/log/log.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
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> 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<const format_internals::type_erased_arg> 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); + } +} + |