From 34ffa4de405d053ef4b53d58e1437f2e82d152b1 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 13 Jul 2026 12:18:08 +0200 Subject: kstd: make error_code formattable --- kernel/kapi/system.cpp | 4 ++-- libs/kstd/kstd/format.hpp | 1 + libs/kstd/kstd/system_error.hpp | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/kernel/kapi/system.cpp b/kernel/kapi/system.cpp index 23dadd3a..629e5990 100644 --- a/kernel/kapi/system.cpp +++ b/kernel/kapi/system.cpp @@ -22,8 +22,8 @@ namespace kapi::system [[gnu::weak]] auto panic(std::string_view message, kstd::error_code error, std::source_location location) -> void { - kstd::println(kstd::print_sink::stderr, "[PANIC] in {} : {} ({}:{}) @ {}:{}", location.function_name(), message, - error.category().name(), error.message(), location.file_name(), location.line()); + kstd::println(kstd::print_sink::stderr, "[PANIC] in {} : {} ({}) @ {}:{}", location.function_name(), message, error, + location.file_name(), location.line()); cpu::halt(); } diff --git a/libs/kstd/kstd/format.hpp b/libs/kstd/kstd/format.hpp index 5ed798db..5d7f4ae6 100644 --- a/libs/kstd/kstd/format.hpp +++ b/libs/kstd/kstd/format.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/libs/kstd/kstd/system_error.hpp b/libs/kstd/kstd/system_error.hpp index f971703b..08725609 100644 --- a/libs/kstd/kstd/system_error.hpp +++ b/libs/kstd/kstd/system_error.hpp @@ -1,6 +1,8 @@ #ifndef KSTD_SYSTEM_ERROR_HPP #define KSTD_SYSTEM_ERROR_HPP +#include + #include #include #include @@ -472,6 +474,29 @@ namespace kstd return {static_cast(value), generic_category()}; } + template<> + struct formatter + { + constexpr auto parse(format_parse_context & context) -> format_parse_context::iterator + { + auto it = context.begin(); + if (it != context.end() && *it != '}') + { + bits::format::error("Invalid specifier for string_view."); + } + return it; + } + + auto format(error_code const & error, format_context & context) const -> void + { + context.push(error.category().name()); + context.push(':'); + formatter{}.format(error.value(), context); + context.push(':'); + context.push(error.message()); + } + }; + } // namespace kstd #endif -- cgit v1.2.3