Kernel: Add a C interface to the logging system

This commit is contained in:
apio 2022-11-12 18:12:18 +01:00
parent 511bb7a8c1
commit 37bb3273ce
4 changed files with 111 additions and 41 deletions

41
kernel/include/log/CLog.h Normal file
View File

@ -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

56
kernel/src/log/CLog.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "log/CLog.h"
#include "std/stdio.h"
#include <stdarg.h>
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);
}

View File

@ -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);

View File

@ -1,4 +1,7 @@
#define MODULE "alloc"
#include "memory/liballoc/liballoc.h"
#include "log/CLog.h"
#include <stdint.h>
@ -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