From 37bb3273ce43350100f5a98f9262ce159572487a Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 12 Nov 2022 18:12:18 +0100 Subject: [PATCH] Kernel: Add a C interface to the logging system --- kernel/include/log/CLog.h | 41 ++++++++++++++++++++ kernel/src/log/CLog.cpp | 56 +++++++++++++++++++++++++++ kernel/src/log/Log.cpp | 9 ++--- kernel/src/memory/liballoc/liballoc.c | 46 +++++----------------- 4 files changed, 111 insertions(+), 41 deletions(-) create mode 100644 kernel/include/log/CLog.h create mode 100644 kernel/src/log/CLog.cpp diff --git a/kernel/include/log/CLog.h b/kernel/include/log/CLog.h new file mode 100644 index 00000000..0c02696a --- /dev/null +++ b/kernel/include/log/CLog.h @@ -0,0 +1,41 @@ +#ifndef _MOON_CLOG_H +#define _MOON_CLOG_H + +enum log_level +{ + LOG_DEBUG, + LOG_INFO, + LOG_WARN, + LOG_ERROR +}; + +#define PRINTF_LIKE(n, m) __attribute__((format(printf, n, m))) + +#ifdef __cplusplus +extern "C" +{ +#endif + + void clog_log(const char* function, enum log_level level, const char* message, ...) PRINTF_LIKE(3, 4); + void clog_logln(const char* function, enum log_level level, const char* message, ...) PRINTF_LIKE(3, 4); + +#ifdef __cplusplus +} +#endif + +#ifndef MODULE +#define kcommonlog(function, level, ...) clog_##function(__FUNCTION__, level, __VA_ARGS__) +#else +#define kcommonlog(function, level, ...) clog_##function(MODULE, level, __VA_ARGS__) +#endif + +#define kdbg(...) kcommonlog(log, LOG_DEBUG, __VA_ARGS__) +#define kdbgln(...) kcommonlog(logln, LOG_DEBUG, __VA_ARGS__) +#define kinfo(...) kcommonlog(log, LOG_INFO, __VA_ARGS__) +#define kinfoln(...) kcommonlog(logln, LOG_INFO, __VA_ARGS__) +#define kwarn(...) kcommonlog(log, LOG_WARN, __VA_ARGS__) +#define kwarnln(...) kcommonlog(logln, LOG_WARN, __VA_ARGS__) +#define kerror(...) kcommonlog(log, LOG_ERROR, __VA_ARGS__) +#define kerrorln(...) kcommonlog(logln, LOG_ERROR, __VA_ARGS__) + +#endif \ No newline at end of file diff --git a/kernel/src/log/CLog.cpp b/kernel/src/log/CLog.cpp new file mode 100644 index 00000000..7c62e565 --- /dev/null +++ b/kernel/src/log/CLog.cpp @@ -0,0 +1,56 @@ +#include "log/CLog.h" +#include "std/stdio.h" +#include + +enum class LogLevel +{ + DEBUG, + INFO, + WARN, + ERROR +}; + +enum class Backend +{ + Serial, + Console +}; + +namespace KernelLog +{ + 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); +} + +void log_backend_serial(const char* function, LogLevel level, const char* message, va_list origin); +void log_backend_console(const char* function, LogLevel level, const char* message, va_list origin); +bool log_level_enabled(LogLevel level); +bool log_backend_enabled(Backend backend); + +extern "C" void clog_log(const char* function, enum log_level level, const char* message, ...) +{ + if (!log_level_enabled((LogLevel)level)) return; + va_list ap; + va_start(ap, message); + if (log_backend_enabled(Backend::Serial)) log_backend_serial(function, (LogLevel)level, message, ap); + if (log_backend_enabled(Backend::Console)) log_backend_console(function, (LogLevel)level, message, ap); + va_end(ap); +} + +extern "C" void clog_logln(const char* function, enum log_level level, const char* message, ...) +{ + if (!log_level_enabled((LogLevel)level)) return; + va_list ap; + va_start(ap, message); + if (log_backend_enabled(Backend::Serial)) + { + log_backend_serial(function, (LogLevel)level, message, ap); + printf("\n"); + } + if (log_backend_enabled(Backend::Console)) + { + log_backend_console(function, (LogLevel)level, message, ap); + kprintf("\n"); + } + va_end(ap); +} \ No newline at end of file diff --git a/kernel/src/log/Log.cpp b/kernel/src/log/Log.cpp index 6faf779b..13df22ea 100644 --- a/kernel/src/log/Log.cpp +++ b/kernel/src/log/Log.cpp @@ -8,17 +8,17 @@ static int level_mask = 15; static int backend_mask = 3; -static bool log_level_enabled(LogLevel level) +bool log_level_enabled(LogLevel level) { return level_mask & (1 << (int)level); } -static bool log_backend_enabled(Backend backend) +bool log_backend_enabled(Backend backend) { return backend_mask & (1 << (int)backend); } -static void log_backend_serial(const char* function, LogLevel level, const char* message, va_list origin) +void log_backend_serial(const char* function, LogLevel level, const char* message, va_list origin) { va_list ap; va_copy(ap, origin); @@ -41,8 +41,7 @@ static void log_backend_serial(const char* function, LogLevel level, const char* va_end(ap); } -static void log_backend_console(const char* function, [[maybe_unused]] LogLevel level, const char* message, - va_list origin) +void log_backend_console(const char* function, [[maybe_unused]] LogLevel level, const char* message, va_list origin) { va_list ap; va_copy(ap, origin); diff --git a/kernel/src/memory/liballoc/liballoc.c b/kernel/src/memory/liballoc/liballoc.c index e80c16cf..6cc26933 100644 --- a/kernel/src/memory/liballoc/liballoc.c +++ b/kernel/src/memory/liballoc/liballoc.c @@ -1,4 +1,7 @@ +#define MODULE "alloc" + #include "memory/liballoc/liballoc.h" +#include "log/CLog.h" #include @@ -549,10 +552,7 @@ void PREFIX(free)(void* ptr) if (ptr == NULL) { l_warningCount += 1; -#if defined DEBUG || defined INFO - printf("liballoc: WARNING: PREFIX(free)( NULL ) called from %x\n", __builtin_return_address(0)); - FLUSH(); -#endif + kwarnln("kfree( NULL ) called from %p", __builtin_return_address(0)); return; } @@ -571,27 +571,14 @@ void PREFIX(free)(void* ptr) ((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) || ((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))) { l_possibleOverruns += 1; -#if defined DEBUG || defined INFO - printf("liballoc: ERROR: Possible 1-3 byte overrun for magic %x != %x\n", min->magic, LIBALLOC_MAGIC); - FLUSH(); -#endif + kerrorln("Possible 1-3 byte overrun for magic %x != %x", min->magic, LIBALLOC_MAGIC); } if (min->magic == LIBALLOC_DEAD) { -#if defined DEBUG || defined INFO - printf("liballoc: ERROR: multiple PREFIX(free)() attempt on %x from %x.\n", ptr, - __builtin_return_address(0)); - FLUSH(); -#endif - } - else - { -#if defined DEBUG || defined INFO - printf("liballoc: ERROR: Bad PREFIX(free)( %x ) called from %x\n", ptr, __builtin_return_address(0)); - FLUSH(); -#endif + kerrorln("double free attempt on %p from %p.", ptr, __builtin_return_address(0)); } + else { kerrorln("Bad kfree(%p) called from %p\n", ptr, __builtin_return_address(0)); } // being lied to... liballoc_unlock(); // release the lock @@ -696,27 +683,14 @@ void* PREFIX(realloc)(void* p, size_t size) ((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) || ((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))) { l_possibleOverruns += 1; -#if defined DEBUG || defined INFO - printf("liballoc: ERROR: Possible 1-3 byte overrun for magic %x != %x\n", min->magic, LIBALLOC_MAGIC); - FLUSH(); -#endif + kerrorln("Possible 1-3 byte overrun for magic %x != %x", min->magic, LIBALLOC_MAGIC); } if (min->magic == LIBALLOC_DEAD) { -#if defined DEBUG || defined INFO - printf("liballoc: ERROR: multiple PREFIX(free)() attempt on %x from %x.\n", ptr, - __builtin_return_address(0)); - FLUSH(); -#endif - } - else - { -#if defined DEBUG || defined INFO - printf("liballoc: ERROR: Bad PREFIX(free)( %x ) called from %x\n", ptr, __builtin_return_address(0)); - FLUSH(); -#endif + kerrorln("realloc after free attempt on %p from %p.", ptr, __builtin_return_address(0)); } + else { kerrorln("Bad krealloc(%p) called from %p\n", ptr, __builtin_return_address(0)); } // being lied to... liballoc_unlock(); // release the lock