summaryrefslogtreecommitdiff
path: root/asl/logging
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-02-20 23:47:02 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-02-20 23:47:02 +0100
commitaa73023bee1aebc745188e54039d3bf567be97e3 (patch)
treee4d3385a7e9eedfff7b36a8237771441668ca7cc /asl/logging
parent409ef997e2ff99b2bbea89ea61d10fc8e26dac96 (diff)
Use intrusive list in logging, and add defer
Diffstat (limited to 'asl/logging')
-rw-r--r--asl/logging/BUILD.bazel2
-rw-r--r--asl/logging/logging.cpp32
-rw-r--r--asl/logging/logging.hpp23
-rw-r--r--asl/logging/logging_tests.cpp17
4 files changed, 41 insertions, 33 deletions
diff --git a/asl/logging/BUILD.bazel b/asl/logging/BUILD.bazel
index 3c60915..0c8323a 100644
--- a/asl/logging/BUILD.bazel
+++ b/asl/logging/BUILD.bazel
@@ -8,7 +8,7 @@ cc_library(
],
deps = [
"//asl/base",
- "//asl/types:box",
+ "//asl/containers:intrusive_list",
"//asl/formatting",
"//asl/io:print",
"//asl/strings:string_builder",
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);
}
}
diff --git a/asl/logging/logging.hpp b/asl/logging/logging.hpp
index c692750..6ad7cfc 100644
--- a/asl/logging/logging.hpp
+++ b/asl/logging/logging.hpp
@@ -1,8 +1,8 @@
#pragma once
#include "asl/base/utility.hpp"
-#include "asl/types/box.hpp"
#include "asl/formatting/format.hpp"
+#include "asl/containers/intrusive_list.hpp"
namespace asl::log
{
@@ -22,21 +22,14 @@ struct message
source_location location;
};
-// @Todo Write and use an intrusive doubly-linked list
-class Logger
+class Logger : public intrusive_list_node<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<Logger>);
-
- constexpr Logger* next_logger() const { return m_next; }
};
class DefaultLoggerBase : public Logger
@@ -59,16 +52,12 @@ public:
}
};
-void register_logger(box<Logger>);
+void register_logger(Logger*);
+void unregister_logger(Logger*);
-// @Todo Add a way to remove loggers (including all)
+void remove_default_logger();
-template<typename T, typename... Args>
-requires constructible_from<T, Args&&...> && convertible_from<Logger*, T*>
-void register_logger(Args&&... args)
-{
- register_logger(make_box<T>(ASL_FWD(args)...));
-}
+// @Todo Add a way to remove loggers (including all)
void log_inner(level l, string_view fmt, span<const format_internals::type_erased_arg> args, const source_location& sl);
diff --git a/asl/logging/logging_tests.cpp b/asl/logging/logging_tests.cpp
index 39a800a..d531cda 100644
--- a/asl/logging/logging_tests.cpp
+++ b/asl/logging/logging_tests.cpp
@@ -1,6 +1,7 @@
#include "asl/logging/logging.hpp"
#include "asl/testing/testing.hpp"
#include "asl/strings/string_builder.hpp"
+#include "asl/base/defer.hpp"
ASL_TEST(log)
{
@@ -9,15 +10,19 @@ ASL_TEST(log)
ASL_LOG_ERROR("Oh no! {}", 42);
}
-static asl::StringWriter g_string_writer{};
-
ASL_TEST(custom_writer)
{
- asl::log::register_logger<asl::log::DefaultLogger<asl::StringWriter<>&>>(g_string_writer);
-
+ asl::StringWriter string_writer{};
+ asl::log::DefaultLogger<asl::StringWriter<>&> logger(string_writer);
+
+ asl::log::register_logger(&logger);
+ ASL_DEFER [&logger]() {
+ asl::log::unregister_logger(&logger);
+ };
+
ASL_LOG_INFO("Hello");
- auto sv = g_string_writer.as_string_view();
+ auto sv = string_writer.as_string_view();
- ASL_TEST_EXPECT(sv == "[ INFO ] asl/logging/logging_tests.cpp:18: Hello\n");
+ ASL_TEST_EXPECT(sv == "[ INFO ] asl/logging/logging_tests.cpp:23: Hello\n");
}