TextConsole: Propagate UTF-8 decoding errors
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-21 17:38:19 +01:00
parent 2e8ea724a0
commit 293b7b0f11
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 37 additions and 24 deletions

View File

@ -55,10 +55,21 @@ static void log_text_console(LogLevel level, const char* format, va_list origin)
TextConsole::set_foreground(WHITE); TextConsole::set_foreground(WHITE);
// FIXME: Same as above. // FIXME: Same as above.
pure_cstyle_format( auto rc = cstyle_format(
format, [](char c, void*) { TextConsole::putchar(c); }, nullptr, ap); format, [](char c, void*) -> Result<void> { return TextConsole::putchar(c); }, nullptr, ap);
TextConsole::putchar('\n'); if (rc.has_error())
{
// FIXME: Find a way to do this in a simpler way (TextConsole::wprint maybe?)
const wchar_t* s = L"Invalid UTF-8 in log message";
while (*s)
{
TextConsole::putwchar(*s);
s++;
}
}
TextConsole::putwchar(L'\n');
TextConsole::set_background(original_background); TextConsole::set_background(original_background);

View File

@ -4,6 +4,7 @@
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/Format.h> #include <luna/Format.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/ScopeGuard.h>
#include <luna/Utf8.h> #include <luna/Utf8.h>
#include <stdarg.h> #include <stdarg.h>
@ -120,18 +121,17 @@ namespace TextConsole
} }
} }
// FIXME: Should this function propagate errors? Result<void> putchar(char c)
void putchar(char c)
{ {
auto rc = utf8_decoder.feed(c); auto guard = make_scope_guard([] { utf8_decoder.reset(); });
if (rc.has_error())
{ auto maybe_wchar = TRY(utf8_decoder.feed(c));
utf8_decoder.reset();
return; guard.deactivate();
}
auto maybe_wchar = rc.value();
if (maybe_wchar.has_value()) putwchar(maybe_wchar.value()); if (maybe_wchar.has_value()) putwchar(maybe_wchar.value());
return {};
} }
void set_foreground(u32 color) void set_foreground(u32 color)
@ -166,23 +166,25 @@ namespace TextConsole
Framebuffer::rect(0, 0, Framebuffer::width(), Framebuffer::height(), BLACK); Framebuffer::rect(0, 0, Framebuffer::width(), Framebuffer::height(), BLACK);
} }
void print(const char* str) Result<void> print(const char* str)
{ {
while (*str) putchar(*str++); while (*str) TRY(putchar(*str++));
return {};
} }
void println(const char* str) Result<void> println(const char* str)
{ {
print(str); TRY(print(str));
putchar('\n'); putwchar(L'\n');
return {};
} }
usize printf(const char* format, ...) Result<usize> printf(const char* format, ...)
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
auto rc = pure_cstyle_format( auto rc = TRY(cstyle_format(
format, [](char c, void*) { putchar(c); }, nullptr, ap); format, [](char c, void*) -> Result<void> { return putchar(c); }, nullptr, ap));
va_end(ap); va_end(ap);
return rc; return rc;
} }

View File

@ -6,14 +6,14 @@
namespace TextConsole namespace TextConsole
{ {
void clear(); void clear();
void putchar(char c); Result<void> putchar(char c);
void putwchar(wchar_t c); void putwchar(wchar_t c);
void set_foreground(u32 color); void set_foreground(u32 color);
void set_background(u32 color); void set_background(u32 color);
u32 foreground(); u32 foreground();
u32 background(); u32 background();
void move_to(u32 x, u32 y); void move_to(u32 x, u32 y);
void print(const char* str); Result<void> print(const char* str);
void println(const char* str); Result<void> println(const char* str);
usize printf(const char* format, ...) _format(1, 2); Result<usize> printf(const char* format, ...) _format(1, 2);
} }