Remove some redundant error propagation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Why can printing to the serial port or format onto a string fail? Even if cstyle_format returns Result<usize>, we shouldn't always follow suit.
This commit is contained in:
parent
41b3c8adb2
commit
814672c771
@ -32,7 +32,8 @@ static void log_serial(LogLevel level, const char* format, va_list origin)
|
|||||||
Serial::putchar((u8)c);
|
Serial::putchar((u8)c);
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
nullptr, ap);
|
nullptr, ap)
|
||||||
|
.expect_value("Sanity check failed in log_serial: Should never fail");
|
||||||
|
|
||||||
Serial::putchar('\n');
|
Serial::putchar('\n');
|
||||||
|
|
||||||
@ -61,7 +62,8 @@ static void log_text_console(LogLevel level, const char* format, va_list origin)
|
|||||||
TextConsole::putchar(c);
|
TextConsole::putchar(c);
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
nullptr, ap);
|
nullptr, ap)
|
||||||
|
.expect_value("Sanity check failed in log_text_console: Should never fail");
|
||||||
|
|
||||||
TextConsole::putchar('\n');
|
TextConsole::putchar('\n');
|
||||||
|
|
||||||
@ -72,26 +74,22 @@ static void log_text_console(LogLevel level, const char* format, va_list origin)
|
|||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> 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_serial_enabled) log_serial(level, format, ap);
|
||||||
if (g_text_console_enabled) log_text_console(level, format, ap);
|
if (g_text_console_enabled) log_text_console(level, format, ap);
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> log(LogLevel level, const char* format, ...)
|
void log(LogLevel level, const char* format, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
|
|
||||||
TRY(vlog(level, format, ap));
|
vlog(level, format, ap);
|
||||||
|
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console)
|
void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console)
|
||||||
|
@ -11,8 +11,8 @@ enum class LogLevel
|
|||||||
Error,
|
Error,
|
||||||
};
|
};
|
||||||
|
|
||||||
Result<void> vlog(LogLevel level, const char* format, va_list ap);
|
void vlog(LogLevel level, const char* format, va_list ap);
|
||||||
Result<void> log(LogLevel level, const char* format, ...) _format(2, 3);
|
void log(LogLevel level, const char* format, ...) _format(2, 3);
|
||||||
|
|
||||||
void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console);
|
void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console);
|
||||||
bool log_debug_enabled();
|
bool log_debug_enabled();
|
||||||
@ -23,8 +23,3 @@ bool log_text_console_enabled();
|
|||||||
#define kinfoln(...) log(LogLevel::Info, __VA_ARGS__)
|
#define kinfoln(...) log(LogLevel::Info, __VA_ARGS__)
|
||||||
#define kwarnln(...) log(LogLevel::Warn, __VA_ARGS__)
|
#define kwarnln(...) log(LogLevel::Warn, __VA_ARGS__)
|
||||||
#define kerrorln(...) log(LogLevel::Error, __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__))
|
|
@ -21,17 +21,18 @@ namespace Serial
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<usize> printf(const char* format, ...)
|
usize printf(const char* format, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
auto rc = cstyle_format(
|
auto rc = cstyle_format(
|
||||||
format,
|
format,
|
||||||
[](char c, void*) -> Result<void> {
|
[](char c, void*) -> Result<void> {
|
||||||
putchar((u8)c);
|
putchar((u8)c);
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
nullptr, ap);
|
nullptr, ap)
|
||||||
|
.expect_value("Sanity check failed in Serial::printf: Should never fail");
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -10,5 +10,5 @@ namespace Serial
|
|||||||
void write(const char* str, usize size);
|
void write(const char* str, usize size);
|
||||||
void print(const char* str);
|
void print(const char* str);
|
||||||
void println(const char* str);
|
void println(const char* str);
|
||||||
Result<usize> printf(const char* str, ...) _format(1, 2);
|
usize printf(const char* str, ...) _format(1, 2);
|
||||||
}
|
}
|
@ -157,17 +157,18 @@ namespace TextConsole
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<usize> printf(const char* format, ...)
|
usize printf(const char* format, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
auto rc = cstyle_format(
|
auto rc = cstyle_format(
|
||||||
format,
|
format,
|
||||||
[](char c, void*) -> Result<void> {
|
[](char c, void*) -> Result<void> {
|
||||||
putchar(c);
|
putchar(c);
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
nullptr, ap);
|
nullptr, ap)
|
||||||
|
.expect_value("Sanity check failed in TextConsole::printf: Should never fail");
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -14,5 +14,5 @@ namespace TextConsole
|
|||||||
void move_to(u32 x, u32 y);
|
void move_to(u32 x, u32 y);
|
||||||
void print(const char* str);
|
void print(const char* str);
|
||||||
void println(const char* str);
|
void println(const char* str);
|
||||||
Result<usize> printf(const char* format, ...) _format(1, 2);
|
usize printf(const char* format, ...) _format(1, 2);
|
||||||
}
|
}
|
@ -6,5 +6,5 @@
|
|||||||
typedef Result<void> (*callback_t)(char, void*);
|
typedef Result<void> (*callback_t)(char, void*);
|
||||||
|
|
||||||
Result<usize> cstyle_format(const char* format, callback_t callback, void* arg, va_list ap);
|
Result<usize> cstyle_format(const char* format, callback_t callback, void* arg, va_list ap);
|
||||||
Result<usize> 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);
|
||||||
Result<usize> string_format(char* buf, usize max, const char* format, ...);
|
usize string_format(char* buf, usize max, const char* format, ...);
|
@ -105,7 +105,7 @@ template <typename T> class Result
|
|||||||
T expect_value(const char* reason)
|
T expect_value(const char* reason)
|
||||||
{
|
{
|
||||||
expect(has_value(), reason);
|
expect(has_value(), reason);
|
||||||
return move(m_value.release_value());
|
return m_value.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
T value_or(const T& other)
|
T value_or(const T& other)
|
||||||
|
@ -2,5 +2,5 @@
|
|||||||
#include <luna/OwnedStringView.h>
|
#include <luna/OwnedStringView.h>
|
||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
|
|
||||||
Result<usize> to_dynamic_unit_cstr(usize value, char* buffer, usize max);
|
usize to_dynamic_unit_cstr(usize value, char* buffer, usize max);
|
||||||
Result<OwnedStringView> to_dynamic_unit(usize value);
|
Result<OwnedStringView> to_dynamic_unit(usize value);
|
@ -478,33 +478,34 @@ struct StringFormatInfo
|
|||||||
usize remaining;
|
usize remaining;
|
||||||
};
|
};
|
||||||
|
|
||||||
Result<usize> 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};
|
StringFormatInfo info = {.buffer = buf, .remaining = max - 1};
|
||||||
|
|
||||||
usize result = TRY(cstyle_format(
|
usize result = cstyle_format(
|
||||||
format,
|
format,
|
||||||
[](char c, void* arg) -> Result<void> {
|
[](char c, void* arg) -> Result<void> {
|
||||||
StringFormatInfo* info_arg = (StringFormatInfo*)arg;
|
StringFormatInfo* info_arg = (StringFormatInfo*)arg;
|
||||||
if (!info_arg->remaining) return {};
|
if (!info_arg->remaining) return {};
|
||||||
*(info_arg->buffer) = c;
|
*(info_arg->buffer) = c;
|
||||||
info_arg->buffer++;
|
info_arg->buffer++;
|
||||||
info_arg->remaining--;
|
info_arg->remaining--;
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
&info, ap));
|
&info, ap)
|
||||||
|
.expect_value("Sanity check failed in vstring_format: Should never fail");
|
||||||
|
|
||||||
*(info.buffer) = 0;
|
*(info.buffer) = 0;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<usize> string_format(char* buf, usize max, const char* format, ...)
|
usize string_format(char* buf, usize max, const char* format, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
|
|
||||||
usize result = TRY(vstring_format(buf, max, format, ap));
|
usize result = vstring_format(buf, max, format, ap);
|
||||||
|
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <luna/ScopeGuard.h>
|
#include <luna/ScopeGuard.h>
|
||||||
#include <luna/Units.h>
|
#include <luna/Units.h>
|
||||||
|
|
||||||
Result<usize> 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); }
|
if (value < 1024) { return string_format(buffer, max, "%u bytes", value); }
|
||||||
|
|
||||||
@ -22,11 +22,7 @@ Result<OwnedStringView> to_dynamic_unit(usize value)
|
|||||||
{
|
{
|
||||||
char* buf = TRY(make_array<char>(64));
|
char* buf = TRY(make_array<char>(64));
|
||||||
|
|
||||||
auto guard = make_scope_guard([&] { destroy_array(buf); });
|
to_dynamic_unit_cstr(value, buf, 64);
|
||||||
|
|
||||||
TRY(to_dynamic_unit_cstr(value, buf, 64));
|
|
||||||
|
|
||||||
guard.deactivate();
|
|
||||||
|
|
||||||
return OwnedStringView{buf};
|
return OwnedStringView{buf};
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user