Luna/kernel/src/arch/Serial.cpp

49 lines
1.1 KiB
C++
Raw Normal View History

2022-11-13 10:30:10 +01:00
#include "arch/Serial.h"
#include "arch/CPU.h"
2022-11-19 12:30:36 +01:00
#include <Format.h>
2022-11-13 10:30:10 +01:00
namespace Serial
{
void write(const char* str, usize size)
2022-11-13 10:30:10 +01:00
{
while (size--) putchar(*str++);
}
void print(const char* str)
{
while (*str) putchar(*str++);
}
void println(const char* str)
{
print(str);
putchar('\n');
}
2022-11-19 12:30:36 +01:00
2022-11-19 16:13:25 +01:00
Result<usize> printf(const char* format, ...)
2022-11-19 12:30:36 +01:00
{
va_list ap;
va_start(ap, format);
2022-11-19 16:13:25 +01:00
auto rc = cstyle_format(
2022-11-19 12:30:36 +01:00
format,
2022-11-19 16:13:25 +01:00
[](char c, void*) -> Result<void> {
2022-11-19 12:30:36 +01:00
putchar(c);
2022-11-19 16:13:25 +01:00
return {};
2022-11-19 12:30:36 +01:00
},
nullptr, ap);
va_end(ap);
2022-11-19 16:13:25 +01:00
return rc;
2022-11-19 12:30:36 +01:00
}
}
static bool g_check_already_failed = false;
_noreturn bool __check_failed(const char* file, const char* line, const char* func, const char* expr)
{
if (!g_check_already_failed)
{ // Avoid endlessly failing when trying to report a failed check.
g_check_already_failed = true;
Serial::printf("ERROR: Check failed at %s:%s, in %s: %s\n", file, line, func, expr);
}
CPU::efficient_halt();
2022-11-13 10:30:10 +01:00
}