From e936a143d3350999c4a452867c82ce6641d52bc4 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 7 Sep 2022 15:02:23 +0200 Subject: [PATCH] MAKE LOG FUNCTIONS VARIADIC!! --- kernel/include/log/Log.h | 24 +++++++++++++++--------- kernel/src/assert.cpp | 2 +- kernel/src/log/Log.cpp | 25 +++++++++++++++++++++---- kernel/src/main.cpp | 4 +++- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/kernel/include/log/Log.h b/kernel/include/log/Log.h index 7ad2d646..2a3eeccf 100644 --- a/kernel/include/log/Log.h +++ b/kernel/include/log/Log.h @@ -7,17 +7,23 @@ enum class LogLevel ERROR }; +#define PRINTF_LIKE(n, m) __attribute__((format(printf, n, m))) + namespace KernelLog { - void log(const char* function, const char* message, LogLevel level); - void logln(const char* function, const char* message, LogLevel level); + void log(const char* function, LogLevel level, const char* message, ...) PRINTF_LIKE(3, 4); + void logln(const char* function, LogLevel level, const char* message, ...) PRINTF_LIKE(3, 4); } -#define kinternalcall(func, function, message, level) KernelLog::func(function, message, level) +#ifndef MODULE +#define kcommonlog(function, level, ...) KernelLog::function(__FUNCTION__, level, __VA_ARGS__) +#else +#define kcommonlog(function, level, ...) KernelLog::function(MODULE, level, __VA_ARGS__) +#endif -#define kinfo(message) kinternalcall(log, __FUNCTION__, message, LogLevel::INFO) -#define kinfoln(message) kinternalcall(logln, __FUNCTION__, message, LogLevel::INFO) -#define kwarn(message) kinternalcall(log, __FUNCTION__, message, LogLevel::WARN) -#define kwarnln(message) kinternalcall(logln, __FUNCTION__, message, LogLevel::WARN) -#define kerror(message) kinternalcall(log, __FUNCTION__, message, LogLevel::ERROR) -#define kerrorln(message) kinternalcall(logln, __FUNCTION__, message, LogLevel::ERROR) \ No newline at end of file +#define kinfo(...) kcommonlog(log, LogLevel::INFO, __VA_ARGS__) +#define kinfoln(...) kcommonlog(logln, LogLevel::INFO, __VA_ARGS__) +#define kwarn(...) kcommonlog(log, LogLevel::WARN, __VA_ARGS__) +#define kwarnln(...) kcommonlog(logln, LogLevel::WARN, __VA_ARGS__) +#define kerror(...) kcommonlog(log, LogLevel::ERROR, __VA_ARGS__) +#define kerrorln(...) kcommonlog(logln, LogLevel::ERROR, __VA_ARGS__) \ No newline at end of file diff --git a/kernel/src/assert.cpp b/kernel/src/assert.cpp index 9c94d7a0..066f268b 100644 --- a/kernel/src/assert.cpp +++ b/kernel/src/assert.cpp @@ -4,7 +4,7 @@ bool __call_assert_fail(const char* function, const char* message) { - KernelLog::logln(function, message, LogLevel::ERROR); + KernelLog::logln(function, LogLevel::ERROR, "%s", message); hang(); return true; } \ No newline at end of file diff --git a/kernel/src/log/Log.cpp b/kernel/src/log/Log.cpp index ac41b2a4..86772169 100644 --- a/kernel/src/log/Log.cpp +++ b/kernel/src/log/Log.cpp @@ -1,9 +1,12 @@ #include "log/Log.h" #include "io/Serial.h" #include "std/stdio.h" +#include -void KernelLog::log(const char* function, const char* message, LogLevel level) +void KernelLog::log(const char* function, LogLevel level, const char* message, ...) { + va_list ap; + va_start(ap, message); Serial::reset_color(); Serial::print(function); Serial::print(": "); @@ -13,12 +16,26 @@ void KernelLog::log(const char* function, const char* message, LogLevel level) case LogLevel::ERROR: Serial::set_color(Color::Red); break; default: break; } - printf("%s", message); + vprintf(message, ap); Serial::reset_color(); + va_end(ap); } -void KernelLog::logln(const char* function, const char* message, LogLevel level) +void KernelLog::logln(const char* function, LogLevel level, const char* message, ...) { - log(function, message, level); + va_list ap; + va_start(ap, message); + Serial::reset_color(); + Serial::print(function); + Serial::print(": "); + switch (level) + { + case LogLevel::WARN: Serial::set_color(Color::Yellow); break; + case LogLevel::ERROR: Serial::set_color(Color::Red); break; + default: break; + } + vprintf(message, ap); + Serial::reset_color(); Serial::print("\n"); + va_end(ap); } \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 2cddb6c3..99a0d576 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -1,3 +1,5 @@ +#define MODULE "main" + #include "acpi/RSDT.h" #include "assert.h" #include "bootboot.h" @@ -92,7 +94,7 @@ extern "C" void _start() isXSDT = true; if (ACPI::ValidateSDTHeader(rootSDT)) { - kinfoln(isXSDT ? "XSDT is valid" : "RSDT is valid"); + kinfoln("%s: 0x%lx", isXSDT ? "XSDT is valid" : "RSDT is valid", (uint64_t)rootSDT); Debug::DebugStatus::the()->PassBootStage(Color{0x33, 0x33, 0x00, 0xFF}); } else