Skip to content

Commit

Permalink
log: Fancier printing
Browse files Browse the repository at this point in the history
  • Loading branch information
marv7000 committed Dec 14, 2024
1 parent daa5f95 commit e3b9b9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
42 changes: 31 additions & 11 deletions include/menix/util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <menix/common.h>
#include <menix/fs/handle.h>
#include <menix/system/arch.h>
#include <menix/system/sch/thread.h>
#include <menix/system/time/clock.h>

#if !defined(NDEBUG) || CONFIG_force_asserts
Expand All @@ -12,11 +14,11 @@
{ \
if (!(expr)) \
{ \
print_log("Assertion failed!\n" \
"[Location]\t%s (%s:%zu)\n" \
"[Expression]\t%s\n" \
"[Message]\t" msg "\n", \
__FUNCTION__, __FILE__, __LINE__, #expr, ##__VA_ARGS__); \
print_error("Assertion failed!\n" \
"[Location]\t%s (%s:%zu)\n" \
"[Expression]\t%s\n" \
"[Message]\t" msg "\n", \
__FUNCTION__, __FILE__, __LINE__, #expr, ##__VA_ARGS__); \
ktrace(NULL); \
kabort(); \
} \
Expand All @@ -29,17 +31,35 @@
} while (0)
#endif

#define __get_print_info \
usize __time = clock_get_elapsed(); \
usize __secs = (__time / 1000000000); \
usize __millis = ((__time / 1000) % 1000000); \
Cpu* __cpu = arch_current_cpu(); \
usize __tid = 0; \
if (__cpu != NULL && __cpu->thread != NULL) \
__tid = __cpu->thread->id;

#define print_log(fmt, ...) \
kmesg_direct("[%5zu.%06zu] " fmt, (clock_get_elapsed() / 1000000000), ((clock_get_elapsed() / 1000) % 1000000), \
##__VA_ARGS__)
do \
{ \
__get_print_info; \
kmesg_direct("[%5zu.%06zu] [%7zu] " fmt, __secs, __millis, __tid, ##__VA_ARGS__); \
} while (0)

#define print_warn(fmt, ...) \
kmesg_direct("[%5zu.%06zu] warn: " fmt, (clock_get_elapsed() / 1000000000), \
((clock_get_elapsed() / 1000) % 1000000), ##__VA_ARGS__)
do \
{ \
__get_print_info; \
kmesg_direct("[%5zu.%06zu] [%7zu] warn: " fmt, __secs, __millis, __tid, ##__VA_ARGS__); \
} while (0)

#define print_error(fmt, ...) \
kmesg_direct("[%5zu.%06zu] error: " fmt, (clock_get_elapsed() / 1000000000), \
((clock_get_elapsed() / 1000) % 1000000), ##__VA_ARGS__)
do \
{ \
__get_print_info; \
kmesg_direct("[%5zu.%06zu] [%7zu] error: " fmt, __secs, __millis, __tid, ##__VA_ARGS__); \
} while (0)

// Print a message to the kernel log.
void kmesg_direct(const char* fmt, ...);
Expand Down
5 changes: 4 additions & 1 deletion kernel/arch/x86_64/system/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ ATTR(noreturn) void arch_stop()

Cpu* arch_current_cpu()
{
kassert(per_cpu_data != NULL, "CPU data was not initialized properly!");
// If it's too early, return NULL.
if (per_cpu_data == NULL)
return NULL;

#ifdef CONFIG_smp
// The Cpu struct starts at KERNEL_GSBASE:0
// Since we can't "directly" access the base address, just get the first field (Cpu.id)
Expand Down

0 comments on commit e3b9b9b

Please sign in to comment.