Browse Source

global log mechanism

master
Mathieu Serandour 1 year ago
parent
commit
d3aa53b881
  1. 4
      kernel/acpi/acpi.c
  2. 88
      kernel/debug/logging.c
  3. 22
      kernel/debug/logging.h
  4. 13
      kernel/entry.c
  5. 9
      kernel/memory/physical_allocator.c

4
kernel/acpi/acpi.c

@ -54,7 +54,7 @@ void read_acpi_tables(const void* rsdp_location) {
size_t n_entries = (xsdt->header.length - sizeof(xsdt->header)) / sizeof(void*);
kprintf("%u ACPI entries\n", n_entries);
klog_debug("%u ACPI entries:", n_entries);
bool madt_parsed = false,
hpet_parsed = false,
@ -65,7 +65,7 @@ void read_acpi_tables(const void* rsdp_location) {
const struct ACPISDTHeader* table = xsdt->entries[i];
assert(checksum(&table, table->length));
kprintf("%3u: %4s\n", i, table->signature.arg);
klog_debug(" %3u: %4s", i, table->signature.arg);
switch(table->signature.raw) {

88
kernel/debug/logging.c

@ -4,9 +4,29 @@
#include "logging.h"
#include "../video/terminal.h"
#include "../klib/string.h"
#define TEXT_COLOR 0xfff0a0
const char* get_level_names_and_set_terminal_color(unsigned level) {
#define BUFFER_SIZE 4096
static char buffer[BUFFER_SIZE];
static unsigned current_level;
// current position in the buffer
static int i = 0;
// appends a string to the buffer
// if there is enough space remaining.
// else, leave it as is
static inline void append_string(const char* str) {
unsigned len = strlen(str);
if(i+len >= BUFFER_SIZE)
return;
memcpy(buffer+i, str, len);
i += len;
}
static const char* get_level_names_and_set_terminal_color(unsigned level) {
switch(level) {
case LOG_LEVEL_DEBUG:
set_terminal_fgcolor(LOG_DEBUG_COLOR);
@ -22,24 +42,70 @@ const char* get_level_names_and_set_terminal_color(unsigned level) {
}
}
void klog(int level, const char* string) {
kputs(get_level_names_and_set_terminal_color(level));
void klog(unsigned level, const char* string) {
if(level < current_level)
return; // avoid overflows
const char* level_name = get_level_names_and_set_terminal_color(level);
// print on the screen
// with fancy colors
kputs(level_name);
set_terminal_fgcolor(TEXT_COLOR);
kputs(string);
kputs("\n");
// append to the buffer
append_string(level_name);
append_string(string);
append_string("\n");
}
void klogf(int level, const char* fmt, ...) {
va_list ap;
void klogf(unsigned level, const char* fmt, ...) {
if(i > (BUFFER_SIZE * 3) / 4 || level < current_level)
return; // avoid overflows
char string[1024];
// render string buffer
va_list ap;
va_start(ap, fmt);
vsprintf(string, fmt, ap);
va_end(ap);
kputs(get_level_names_and_set_terminal_color(level));
const char* level_name = get_level_names_and_set_terminal_color(level);
// print on the screen
// with fancy colors
kputs(level_name);
set_terminal_fgcolor(TEXT_COLOR);
vkprintf(fmt, ap);
kputs(string);
kputs("\n");
// append to the buffer
append_string(level_name);
append_string(string);
append_string("\n");
}
void set_logging_level(unsigned level) {
current_level = level;
}
const char* klog_get(void) {
buffer[i] = 0;
return buffer;
}
void klog_flush(void) {
i = 0;
}
va_end(ap);
}

22
kernel/debug/logging.h

@ -15,21 +15,21 @@
#ifndef NDEBUG
#define log_debug(fmt, ...) klogf(LOG_LEVEL_DEBUG, fmt, __VA_ARGS__)
#define klog_debug(fmt, ...) klogf(LOG_LEVEL_DEBUG, fmt, __VA_ARGS__)
#else
#define log_debug(fmt, ...)
#define klog_debug(fmt, ...)
#endif
#define log_info(fmt, ...) \
if(NUMARGS(__VA_ARGS__) == 0) klog(LOG_LEVEL_DEBUG, fmt) \
else klogf(LOG_LEVEL_DEBUG, fmt, __VA_ARGS__)
#define klog_info(...) klogf(LOG_LEVEL_INFO, __VA_ARGS__)
#define klog_warn(...) klogf(LOG_LEVEL_WARN, __VA_ARGS__)
#define log_warn(fmt, ...) \
if(NUMARGS(__VA_ARGS__) == 0) klog(LOG_LEVEL_DEBUG, fmt) \
else klogf(LOG_LEVEL_DEBUG, fmt, __VA_ARGS__)
void klog(unsigned level, const char* string);
// behaves like kprintf
void klogf(unsigned level, const char* fmt, ...);
void klog(int level, const char* string);
void set_logging_level(unsigned level);
// behaves like kprintf
void klogf(int level, const char* fmt, ...);
const char* klog_get(void);
void klog_flush(void);

13
kernel/entry.c

@ -4,9 +4,9 @@
#include "memory/gdt.h"
#include "video/video.h"
#include "video/terminal.h"
#include "klib/sprintf.h"
#include "klib/string.h"
#include "video/terminal.h"
#include "acpi/acpi.h"
#include "common.h"
#include "regs.h"
@ -17,6 +17,7 @@
#include "memory/physical_allocator.h"
#include "memory/paging.h"
#include "memory/vmap.h"
#include "debug/logging.h"
#define KERNEL_STACK_SIZE 8192
@ -153,7 +154,8 @@ void _start(struct stivale2_struct *stivale2_struct) {
}
// print all logging messages
set_logging_level(LOG_LEVEL_DEBUG);
setup_isrs();
read_acpi_tables((void*)rsdp_tag_ptr->rsdp);
@ -172,14 +174,15 @@ void _start(struct stivale2_struct *stivale2_struct) {
setup_terminal();
append_paging_initialization();
terminal_set_colors(0xfff0a0, 0x212121);
terminal_clear();
kputs(&_binary_bootmessage_txt);
kprintf("OUI\n");
kprintf("boot logs:\n");
kputs(klog_get());
klog_flush();
hpet_init();
apic_setup_clock();

9
kernel/memory/physical_allocator.c

@ -8,6 +8,7 @@
#include "../debug/assert.h"
#include "../debug/panic.h"
#include "physical_allocator.h"
#include "../debug/logging.h"
#include "vmap.h"
/**
@ -180,7 +181,13 @@ void init_physical_allocator(const struct stivale2_struct_tag_memmap* memmap) {
}
n_ranges = j;
total_available_pages = total_pages - n_ranges;
total_available_pages = total_pages - n_ranges;
klog_info(
"found %u MB of usable memory in the system (%u pages, %u physical ranges)",
total_available_pages * 4 / 1024,
total_available_pages,
n_ranges
);
}

Loading…
Cancel
Save