2022-11-30 12:29:28 +00:00
|
|
|
#include "Log.h"
|
2022-11-30 16:12:06 +00:00
|
|
|
#include "arch/CPU.h"
|
2022-11-30 12:29:28 +00:00
|
|
|
#include "arch/Serial.h"
|
|
|
|
#include "arch/Timer.h"
|
|
|
|
#include "video/TextConsole.h"
|
2022-12-04 11:42:43 +00:00
|
|
|
#include <luna/Format.h>
|
2022-11-30 12:29:28 +00:00
|
|
|
|
|
|
|
static bool g_debug_enabled = true;
|
|
|
|
static bool g_serial_enabled = true;
|
|
|
|
static bool g_text_console_enabled = false;
|
|
|
|
|
|
|
|
static constexpr u32 BLACK = 0xff000000;
|
|
|
|
static constexpr u32 WHITE = 0xffffffff;
|
|
|
|
static constexpr u32 YELLOW = 0xffffff00;
|
|
|
|
static constexpr u32 RED = 0xffff0000;
|
|
|
|
|
2022-11-30 16:26:33 +00:00
|
|
|
static char log_level_letters[] = {'D', 'I', 'W', 'E'}; // D for debug, I for info, W for warning, E for error
|
|
|
|
static const char* ansi_color_codes_per_log_level[] = {"37", "37", "33", "31"}; // 37 is white, 33 yellow, 31 red
|
|
|
|
|
2022-12-06 18:35:34 +00:00
|
|
|
static void log_serial(LogLevel level, const char* format, va_list origin)
|
2022-11-30 12:29:28 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_copy(ap, origin);
|
|
|
|
|
2022-12-19 20:42:53 +00:00
|
|
|
Serial::printf("\x1b[%sm"
|
|
|
|
"%c"
|
|
|
|
"\x1b[0m ",
|
|
|
|
ansi_color_codes_per_log_level[(int)level], log_level_letters[(int)level]);
|
2022-11-30 16:26:33 +00:00
|
|
|
|
2022-12-06 18:35:34 +00:00
|
|
|
Serial::printf("%4zu.%.3zu ", Timer::ticks(), Timer::ticks_ms() - (Timer::ticks() * 1000));
|
2022-11-30 12:29:28 +00:00
|
|
|
|
2022-12-19 20:41:27 +00:00
|
|
|
// FIXME: We do this manually because of a lack of vprintf() in both Serial and TextConsole.
|
2022-12-17 11:38:22 +00:00
|
|
|
pure_cstyle_format(
|
|
|
|
format, [](char c, void*) { Serial::putchar((u8)c); }, nullptr, ap);
|
2022-11-30 12:29:28 +00:00
|
|
|
|
|
|
|
Serial::putchar('\n');
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2022-12-06 18:35:34 +00:00
|
|
|
static void log_text_console(LogLevel level, const char* format, va_list origin)
|
2022-11-30 12:29:28 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_copy(ap, origin);
|
|
|
|
|
2022-12-05 12:00:41 +00:00
|
|
|
const u32 original_foreground = TextConsole::foreground();
|
|
|
|
const u32 original_background = TextConsole::background();
|
2022-11-30 12:29:28 +00:00
|
|
|
|
|
|
|
TextConsole::set_background(BLACK);
|
|
|
|
|
|
|
|
if (level == LogLevel::Warn) TextConsole::set_foreground(YELLOW);
|
|
|
|
else if (level == LogLevel::Error)
|
|
|
|
TextConsole::set_foreground(RED);
|
|
|
|
else
|
|
|
|
TextConsole::set_foreground(WHITE);
|
|
|
|
|
2022-12-19 20:41:27 +00:00
|
|
|
// FIXME: Same as above.
|
2022-12-21 16:38:19 +00:00
|
|
|
auto rc = cstyle_format(
|
|
|
|
format, [](char c, void*) -> Result<void> { return TextConsole::putchar(c); }, nullptr, ap);
|
|
|
|
|
2022-12-21 18:41:13 +00:00
|
|
|
if (rc.has_error()) { TextConsole::wprint(L"Invalid UTF-8 in log message"); }
|
2022-12-21 16:38:19 +00:00
|
|
|
|
|
|
|
TextConsole::putwchar(L'\n');
|
2022-11-30 12:29:28 +00:00
|
|
|
|
|
|
|
TextConsole::set_background(original_background);
|
|
|
|
|
|
|
|
TextConsole::set_foreground(original_foreground);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:32:29 +00:00
|
|
|
void vlog(LogLevel level, const char* format, va_list ap)
|
2022-11-30 12:29:28 +00:00
|
|
|
{
|
2022-12-16 17:32:29 +00:00
|
|
|
if (!g_debug_enabled && level == LogLevel::Debug) return;
|
2022-11-30 12:29:28 +00:00
|
|
|
|
2022-12-06 18:35:34 +00:00
|
|
|
if (g_serial_enabled) log_serial(level, format, ap);
|
|
|
|
if (g_text_console_enabled) log_text_console(level, format, ap);
|
2022-11-30 12:29:28 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 17:32:29 +00:00
|
|
|
void log(LogLevel level, const char* format, ...)
|
2022-11-30 12:29:28 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2022-12-16 17:32:29 +00:00
|
|
|
vlog(level, format, ap);
|
2022-11-30 12:29:28 +00:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_log(bool enable_debug, bool enable_serial, bool enable_text_console)
|
|
|
|
{
|
|
|
|
g_debug_enabled = enable_debug;
|
|
|
|
g_serial_enabled = enable_serial;
|
|
|
|
g_text_console_enabled = enable_text_console;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool log_debug_enabled()
|
|
|
|
{
|
|
|
|
return g_debug_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool log_serial_enabled()
|
|
|
|
{
|
|
|
|
return g_serial_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool log_text_console_enabled()
|
|
|
|
{
|
|
|
|
return g_text_console_enabled;
|
2022-11-30 16:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool g_check_already_failed = false;
|
|
|
|
|
2022-12-06 18:40:35 +00:00
|
|
|
[[noreturn]] bool __check_failed(const char* file, const char* line, const char* func, const char* expr)
|
2022-11-30 16:12:06 +00:00
|
|
|
{
|
2022-12-16 19:49:09 +00:00
|
|
|
CPU::disable_interrupts();
|
2022-11-30 16:12:06 +00:00
|
|
|
if (!g_check_already_failed)
|
|
|
|
{ // Avoid endlessly failing when trying to report a failed check.
|
|
|
|
g_check_already_failed = true;
|
|
|
|
kerrorln("ERROR: Check failed at %s:%s, in %s: %s", file, line, func, expr);
|
2022-12-16 19:49:09 +00:00
|
|
|
CPU::print_stack_trace();
|
2022-11-30 16:12:06 +00:00
|
|
|
}
|
|
|
|
CPU::efficient_halt();
|
2022-11-30 12:29:28 +00:00
|
|
|
}
|