mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
Move kernel over to new logger
This commit is contained in:
@@ -1,107 +1,26 @@
|
|||||||
#include <type_traits>
|
#include "kutil/constexpr_hash.h"
|
||||||
#include "kutil/assert.h"
|
|
||||||
#include "kutil/memory.h"
|
#include "kutil/memory.h"
|
||||||
#include "console.h"
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
namespace logs {
|
||||||
|
|
||||||
static const uint64_t default_enabled[] = {0x00, 0xff, 0xff, 0xff};
|
static uint8_t log_buffer[0x10000];
|
||||||
static const uint8_t level_colors[] = {0x07, 0x0f, 0x0b, 0x09};
|
static log::logger g_logger(log_buffer, sizeof(log_buffer));
|
||||||
|
|
||||||
static const char *levels[] = {"debug", " info", " warn", "error", "fatal"};
|
#define LOG(name, lvl) \
|
||||||
static const char *areas[] = {
|
log::area_t name = #name ## _h; \
|
||||||
"boot ",
|
const char * name ## _name = #name;
|
||||||
"memory",
|
#include "log_areas.inc"
|
||||||
"apic ",
|
#undef LOG
|
||||||
"device",
|
|
||||||
"driver",
|
|
||||||
"file ",
|
|
||||||
"task ",
|
|
||||||
"paging",
|
|
||||||
|
|
||||||
nullptr
|
void init()
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
log log::s_log;
|
|
||||||
|
|
||||||
log::log() : m_cons(nullptr)
|
|
||||||
{
|
{
|
||||||
kassert(0, "Invalid log constructor");
|
new (&g_logger) log::logger(log_buffer, sizeof(log_buffer));
|
||||||
|
|
||||||
|
#define LOG(name, lvl) \
|
||||||
|
g_logger.register_area(name, name ## _name, log::level ::lvl);
|
||||||
|
#include "log_areas.inc"
|
||||||
|
#undef LOG
|
||||||
}
|
}
|
||||||
|
|
||||||
log::log(console *cons) :
|
} // namespace logs
|
||||||
m_cons(cons)
|
|
||||||
{
|
|
||||||
const int num_levels = static_cast<int>(level::max) - 1;
|
|
||||||
for (int i = 0; i < num_levels; ++i)
|
|
||||||
m_enabled[i] = default_enabled[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
log::init(console *cons)
|
|
||||||
{
|
|
||||||
new (&s_log) log(cons);
|
|
||||||
log::info(logs::boot, "Logging system initialized.");
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint64_t
|
|
||||||
bit_mask(logs area) { return 1 << static_cast<uint64_t>(area); }
|
|
||||||
|
|
||||||
void
|
|
||||||
log::enable(logs type, level at_level)
|
|
||||||
{
|
|
||||||
using under_t = std::underlying_type<level>::type;
|
|
||||||
under_t at = static_cast<under_t>(at_level);
|
|
||||||
under_t max = sizeof(m_enabled) / sizeof(m_enabled[0]);
|
|
||||||
|
|
||||||
for (under_t i = 0; i < max; ++i) {
|
|
||||||
if (i >= at)
|
|
||||||
s_log.m_enabled[i] |= bit_mask(type);
|
|
||||||
else
|
|
||||||
s_log.m_enabled[i] &= ~bit_mask(type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void
|
|
||||||
log::trylog<log::level::fatal>(logs area, const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
s_log.output(level::fatal, area, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
while(1) __asm__ ("hlt");
|
|
||||||
}
|
|
||||||
|
|
||||||
template <log::level L>
|
|
||||||
void
|
|
||||||
log::trylog(logs area, const char *fmt, ...)
|
|
||||||
{
|
|
||||||
auto i = static_cast<std::underlying_type<level>::type>(L);
|
|
||||||
if ((s_log.m_enabled[i] & bit_mask(area)) == 0) return;
|
|
||||||
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
s_log.output(L, area, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
log::output(level severity, logs area, const char *fmt, va_list args)
|
|
||||||
{
|
|
||||||
m_cons->set_color(level_colors[static_cast<int>(severity)]);
|
|
||||||
|
|
||||||
m_cons->printf("%s %s: ",
|
|
||||||
areas[static_cast<int>(area)],
|
|
||||||
levels[static_cast<int>(severity)]);
|
|
||||||
m_cons->vprintf(fmt, args);
|
|
||||||
m_cons->set_color();
|
|
||||||
m_cons->puts("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
const log::trylog_p log::debug = &trylog<level::debug>;
|
|
||||||
const log::trylog_p log::info = &trylog<level::info>;
|
|
||||||
const log::trylog_p log::warn = &trylog<level::warn>;
|
|
||||||
const log::trylog_p log::error = &trylog<level::error>;
|
|
||||||
const log::trylog_p log::fatal = &trylog<level::fatal>;
|
|
||||||
|
|||||||
@@ -1,52 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
class console;
|
#include "kutil/logger.h"
|
||||||
|
|
||||||
|
namespace log = kutil::log;
|
||||||
|
|
||||||
enum class logs
|
namespace logs {
|
||||||
{
|
|
||||||
boot,
|
|
||||||
memory,
|
|
||||||
apic,
|
|
||||||
device,
|
|
||||||
driver,
|
|
||||||
fs,
|
|
||||||
task,
|
|
||||||
paging,
|
|
||||||
|
|
||||||
max
|
#define LOG(name, lvl) extern log::area_t name;
|
||||||
};
|
#include "log_areas.inc"
|
||||||
|
#undef LOG
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
class log
|
} // namespace logs
|
||||||
{
|
|
||||||
public:
|
|
||||||
enum class level {debug, info, warn, error, fatal, max};
|
|
||||||
|
|
||||||
static void init(console *cons);
|
|
||||||
static void enable(logs type, level at_level);
|
|
||||||
|
|
||||||
template <level L>
|
|
||||||
static void trylog(logs area, const char *fmt, ...);
|
|
||||||
using trylog_p = void (*)(logs area, const char *fmt, ...);
|
|
||||||
|
|
||||||
static const trylog_p debug;
|
|
||||||
static const trylog_p info;
|
|
||||||
static const trylog_p warn;
|
|
||||||
static const trylog_p error;
|
|
||||||
static const trylog_p fatal;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void output(level severity, logs area, const char *fmt, va_list args);
|
|
||||||
|
|
||||||
/// Bitmasks for what categories are enabled. fatal is
|
|
||||||
/// always enabled, so leave it out.
|
|
||||||
uint64_t m_enabled[static_cast<uint64_t>(level::max) - 1];
|
|
||||||
console *m_cons;
|
|
||||||
|
|
||||||
log();
|
|
||||||
log(console *cons);
|
|
||||||
static log s_log;
|
|
||||||
};
|
|
||||||
|
|||||||
8
src/kernel/log_areas.inc
Normal file
8
src/kernel/log_areas.inc
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
LOG(apic, info);
|
||||||
|
LOG(device, debug);
|
||||||
|
LOG(paging, info);
|
||||||
|
LOG(driver, debug);
|
||||||
|
LOG(memory, debug);
|
||||||
|
LOG(fs, debug);
|
||||||
|
LOG(task, debug);
|
||||||
|
LOG(boot, debug);
|
||||||
@@ -37,16 +37,7 @@ init_console()
|
|||||||
cons->set_color(0x08, 0x00);
|
cons->set_color(0x08, 0x00);
|
||||||
cons->puts(GIT_VERSION " booting...\n");
|
cons->puts(GIT_VERSION " booting...\n");
|
||||||
|
|
||||||
log::init(cons);
|
logs::init();
|
||||||
log::enable(logs::apic, log::level::info);
|
|
||||||
log::enable(logs::device, log::level::debug);
|
|
||||||
log::enable(logs::paging, log::level::info);
|
|
||||||
|
|
||||||
log::enable(logs::driver, log::level::debug);
|
|
||||||
log::enable(logs::memory, log::level::debug);
|
|
||||||
log::enable(logs::fs, log::level::debug);
|
|
||||||
log::enable(logs::task, log::level::debug);
|
|
||||||
log::enable(logs::boot, log::level::debug);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
Reference in New Issue
Block a user