From 814672c77107302b5ef1ce4a4ca2f2634b5b523e Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 16 Dec 2022 18:32:29 +0100 Subject: [PATCH] Remove some redundant error propagation Why can printing to the serial port or format onto a string fail? Even if cstyle_format returns Result, we shouldn't always follow suit. --- kernel/src/Log.cpp | 18 ++++++++---------- kernel/src/Log.h | 11 +++-------- kernel/src/arch/Serial.cpp | 15 ++++++++------- kernel/src/arch/Serial.h | 2 +- kernel/src/video/TextConsole.cpp | 15 ++++++++------- kernel/src/video/TextConsole.h | 2 +- luna/include/luna/Format.h | 4 ++-- luna/include/luna/Result.h | 2 +- luna/include/luna/Units.h | 2 +- luna/src/Format.cpp | 29 +++++++++++++++-------------- luna/src/Units.cpp | 8 ++------ 11 files changed, 50 insertions(+), 58 deletions(-) diff --git a/kernel/src/Log.cpp b/kernel/src/Log.cpp index e820dfd9..d26190b1 100644 --- a/kernel/src/Log.cpp +++ b/kernel/src/Log.cpp @@ -32,7 +32,8 @@ static void log_serial(LogLevel level, const char* format, va_list origin) Serial::putchar((u8)c); return {}; }, - nullptr, ap); + nullptr, ap) + .expect_value("Sanity check failed in log_serial: Should never fail"); Serial::putchar('\n'); @@ -61,7 +62,8 @@ static void log_text_console(LogLevel level, const char* format, va_list origin) TextConsole::putchar(c); return {}; }, - nullptr, ap); + nullptr, ap) + .expect_value("Sanity check failed in log_text_console: Should never fail"); TextConsole::putchar('\n'); @@ -72,26 +74,22 @@ static void log_text_console(LogLevel level, const char* format, va_list origin) va_end(ap); } -Result vlog(LogLevel level, const char* format, va_list ap) +void vlog(LogLevel level, const char* format, va_list ap) { - if (!g_debug_enabled && level == LogLevel::Debug) return {}; + if (!g_debug_enabled && level == LogLevel::Debug) return; if (g_serial_enabled) log_serial(level, format, ap); if (g_text_console_enabled) log_text_console(level, format, ap); - - return {}; } -Result log(LogLevel level, const char* format, ...) +void log(LogLevel level, const char* format, ...) { va_list ap; va_start(ap, format); - TRY(vlog(level, format, ap)); + vlog(level, format, ap); va_end(ap); - - return {}; } void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console) diff --git a/kernel/src/Log.h b/kernel/src/Log.h index 10eaed9f..31dddd5c 100644 --- a/kernel/src/Log.h +++ b/kernel/src/Log.h @@ -11,8 +11,8 @@ enum class LogLevel Error, }; -Result vlog(LogLevel level, const char* format, va_list ap); -Result log(LogLevel level, const char* format, ...) _format(2, 3); +void vlog(LogLevel level, const char* format, va_list ap); +void log(LogLevel level, const char* format, ...) _format(2, 3); void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console); bool log_debug_enabled(); @@ -22,9 +22,4 @@ bool log_text_console_enabled(); #define kdbgln(...) log(LogLevel::Debug, __VA_ARGS__) #define kinfoln(...) log(LogLevel::Info, __VA_ARGS__) #define kwarnln(...) log(LogLevel::Warn, __VA_ARGS__) -#define kerrorln(...) log(LogLevel::Error, __VA_ARGS__) - -#define try_kdbgln(...) TRY(log(LogLevel::Debug, __VA_ARGS__)) -#define try_kinfoln(...) TRY(log(LogLevel::Info, __VA_ARGS__)) -#define try_kwarnln(...) TRY(log(LogLevel::Warn, __VA_ARGS__)) -#define try_kerrorln(...) TRY(log(LogLevel::Error, __VA_ARGS__)) \ No newline at end of file +#define kerrorln(...) log(LogLevel::Error, __VA_ARGS__) \ No newline at end of file diff --git a/kernel/src/arch/Serial.cpp b/kernel/src/arch/Serial.cpp index e82d75e1..ce12ddae 100644 --- a/kernel/src/arch/Serial.cpp +++ b/kernel/src/arch/Serial.cpp @@ -21,17 +21,18 @@ namespace Serial putchar('\n'); } - Result printf(const char* format, ...) + usize printf(const char* format, ...) { va_list ap; va_start(ap, format); auto rc = cstyle_format( - format, - [](char c, void*) -> Result { - putchar((u8)c); - return {}; - }, - nullptr, ap); + format, + [](char c, void*) -> Result { + putchar((u8)c); + return {}; + }, + nullptr, ap) + .expect_value("Sanity check failed in Serial::printf: Should never fail"); va_end(ap); return rc; } diff --git a/kernel/src/arch/Serial.h b/kernel/src/arch/Serial.h index 0efc6e47..f74597c7 100644 --- a/kernel/src/arch/Serial.h +++ b/kernel/src/arch/Serial.h @@ -10,5 +10,5 @@ namespace Serial void write(const char* str, usize size); void print(const char* str); void println(const char* str); - Result printf(const char* str, ...) _format(1, 2); + usize printf(const char* str, ...) _format(1, 2); } \ No newline at end of file diff --git a/kernel/src/video/TextConsole.cpp b/kernel/src/video/TextConsole.cpp index dc5f96a0..d3679350 100644 --- a/kernel/src/video/TextConsole.cpp +++ b/kernel/src/video/TextConsole.cpp @@ -157,17 +157,18 @@ namespace TextConsole putchar('\n'); } - Result printf(const char* format, ...) + usize printf(const char* format, ...) { va_list ap; va_start(ap, format); auto rc = cstyle_format( - format, - [](char c, void*) -> Result { - putchar(c); - return {}; - }, - nullptr, ap); + format, + [](char c, void*) -> Result { + putchar(c); + return {}; + }, + nullptr, ap) + .expect_value("Sanity check failed in TextConsole::printf: Should never fail"); va_end(ap); return rc; } diff --git a/kernel/src/video/TextConsole.h b/kernel/src/video/TextConsole.h index d502e485..73ae96b5 100644 --- a/kernel/src/video/TextConsole.h +++ b/kernel/src/video/TextConsole.h @@ -14,5 +14,5 @@ namespace TextConsole void move_to(u32 x, u32 y); void print(const char* str); void println(const char* str); - Result printf(const char* format, ...) _format(1, 2); + usize printf(const char* format, ...) _format(1, 2); } \ No newline at end of file diff --git a/luna/include/luna/Format.h b/luna/include/luna/Format.h index e1cccc3c..fc3a4610 100644 --- a/luna/include/luna/Format.h +++ b/luna/include/luna/Format.h @@ -6,5 +6,5 @@ typedef Result (*callback_t)(char, void*); Result cstyle_format(const char* format, callback_t callback, void* arg, va_list ap); -Result vstring_format(char* buf, usize max, const char* format, va_list ap); -Result string_format(char* buf, usize max, const char* format, ...); \ No newline at end of file +usize vstring_format(char* buf, usize max, const char* format, va_list ap); +usize string_format(char* buf, usize max, const char* format, ...); \ No newline at end of file diff --git a/luna/include/luna/Result.h b/luna/include/luna/Result.h index 68581a28..b8ac716e 100644 --- a/luna/include/luna/Result.h +++ b/luna/include/luna/Result.h @@ -105,7 +105,7 @@ template class Result T expect_value(const char* reason) { expect(has_value(), reason); - return move(m_value.release_value()); + return m_value.value(); } T value_or(const T& other) diff --git a/luna/include/luna/Units.h b/luna/include/luna/Units.h index d20b4e0e..6b062f1a 100644 --- a/luna/include/luna/Units.h +++ b/luna/include/luna/Units.h @@ -2,5 +2,5 @@ #include #include -Result to_dynamic_unit_cstr(usize value, char* buffer, usize max); +usize to_dynamic_unit_cstr(usize value, char* buffer, usize max); Result to_dynamic_unit(usize value); \ No newline at end of file diff --git a/luna/src/Format.cpp b/luna/src/Format.cpp index a74b0a38..b10754be 100644 --- a/luna/src/Format.cpp +++ b/luna/src/Format.cpp @@ -478,33 +478,34 @@ struct StringFormatInfo usize remaining; }; -Result vstring_format(char* buf, usize max, const char* format, va_list ap) +usize vstring_format(char* buf, usize max, const char* format, va_list ap) { StringFormatInfo info = {.buffer = buf, .remaining = max - 1}; - usize result = TRY(cstyle_format( - format, - [](char c, void* arg) -> Result { - StringFormatInfo* info_arg = (StringFormatInfo*)arg; - if (!info_arg->remaining) return {}; - *(info_arg->buffer) = c; - info_arg->buffer++; - info_arg->remaining--; - return {}; - }, - &info, ap)); + usize result = cstyle_format( + format, + [](char c, void* arg) -> Result { + StringFormatInfo* info_arg = (StringFormatInfo*)arg; + if (!info_arg->remaining) return {}; + *(info_arg->buffer) = c; + info_arg->buffer++; + info_arg->remaining--; + return {}; + }, + &info, ap) + .expect_value("Sanity check failed in vstring_format: Should never fail"); *(info.buffer) = 0; return result; } -Result string_format(char* buf, usize max, const char* format, ...) +usize string_format(char* buf, usize max, const char* format, ...) { va_list ap; va_start(ap, format); - usize result = TRY(vstring_format(buf, max, format, ap)); + usize result = vstring_format(buf, max, format, ap); va_end(ap); diff --git a/luna/src/Units.cpp b/luna/src/Units.cpp index e8002d1c..85a73900 100644 --- a/luna/src/Units.cpp +++ b/luna/src/Units.cpp @@ -4,7 +4,7 @@ #include #include -Result to_dynamic_unit_cstr(usize value, char* buffer, usize max) +usize to_dynamic_unit_cstr(usize value, char* buffer, usize max) { if (value < 1024) { return string_format(buffer, max, "%u bytes", value); } @@ -22,11 +22,7 @@ Result to_dynamic_unit(usize value) { char* buf = TRY(make_array(64)); - auto guard = make_scope_guard([&] { destroy_array(buf); }); - - TRY(to_dynamic_unit_cstr(value, buf, 64)); - - guard.deactivate(); + to_dynamic_unit_cstr(value, buf, 64); return OwnedStringView{buf}; } \ No newline at end of file