From aa73023bee1aebc745188e54039d3bf567be97e3 Mon Sep 17 00:00:00 2001
From: Steven Le Rouzic <steven.lerouzic@gmail.com>
Date: Thu, 20 Feb 2025 23:47:02 +0100
Subject: Use intrusive list in logging, and add defer

---
 asl/logging/logging.cpp | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

(limited to 'asl/logging/logging.cpp')

diff --git a/asl/logging/logging.cpp b/asl/logging/logging.cpp
index df7758d..a825dd3 100644
--- a/asl/logging/logging.cpp
+++ b/asl/logging/logging.cpp
@@ -5,7 +5,27 @@
 // @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;
+
+// @Todo Protect the loggers list being a mutex
+static asl::IntrusiveList<asl::log::Logger> g_loggers(&g_default_logger);
+
+void asl::log::register_logger(Logger* logger)
+{
+    g_loggers.push_front(logger);
+}
+
+void asl::log::unregister_logger(Logger* logger)
+{
+    g_loggers.detach(logger);
+}
+
+void asl::log::remove_default_logger()
+{
+    if (g_default_logger.m_next != nullptr)
+    {
+        g_loggers.detach(&g_default_logger);
+    }
+}
 
 static constexpr asl::string_view kLevelName[] = {
     "  DEBUG  ",
@@ -14,12 +34,6 @@ static constexpr asl::string_view kLevelName[] = {
     "  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",
@@ -44,9 +58,9 @@ void asl::log::log_inner(
         .location = sl,
     };
 
-    for (auto* it = g_head; it != nullptr; it = it->next_logger())
+    for (auto& logger: g_loggers)
     {
-        it->log(m);
+        logger.log(m);
     }
 }
 
-- 
cgit