diff options
Diffstat (limited to 'asl/logging/logging.cpp')
-rw-r--r-- | asl/logging/logging.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/asl/logging/logging.cpp b/asl/logging/logging.cpp new file mode 100644 index 0000000..aa630ac --- /dev/null +++ b/asl/logging/logging.cpp @@ -0,0 +1,52 @@ +#include "asl/logging/logging.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<asl::Writer*> 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::DefaultLoggerBase::log_inner(Writer& writer, const message& msg) +{ + asl::format(&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); + } +} + |