implement logger
This commit is contained in:
45
c_os/user/lib/Logger.cc
Normal file
45
c_os/user/lib/Logger.cc
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include "user/lib/Logger.h"
|
||||||
|
#include "kernel/Globals.h"
|
||||||
|
|
||||||
|
bool Logger::kout_enabled = true;
|
||||||
|
bool Logger::serial_enabled = true;
|
||||||
|
const Semaphore Logger::sem = Semaphore(1);
|
||||||
|
|
||||||
|
Logger::LogLevel Logger::level = Logger::TRACE;
|
||||||
|
|
||||||
|
void Logger::log(char* message, CGA::color col) {
|
||||||
|
if (Logger::kout_enabled) {
|
||||||
|
CGA::color old_col = kout.color_fg;
|
||||||
|
kout << fgc(col) << this->name << " :: " << message << fgc(old_col) << endl;
|
||||||
|
}
|
||||||
|
if (Logger::serial_enabled) {
|
||||||
|
serial.write(this->name);
|
||||||
|
serial.write(" :: ");
|
||||||
|
serial.write(message);
|
||||||
|
serial.write("\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::trace(char* message) {
|
||||||
|
if (Logger::level <= Logger::TRACE) {
|
||||||
|
this->log(message, CGA::GREEN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::debug(char* message) {
|
||||||
|
if (Logger::level <= Logger::DEBUG) {
|
||||||
|
this->log(message, CGA::CYAN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::error(char* message) {
|
||||||
|
if (Logger::level <= Logger::ERROR) {
|
||||||
|
this->log(message, CGA::RED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::info(char* message) {
|
||||||
|
if (Logger::level <= Logger::TRACE) {
|
||||||
|
this->log(message, CGA::LIGHT_GREY);
|
||||||
|
}
|
||||||
|
}
|
||||||
55
c_os/user/lib/Logger.h
Normal file
55
c_os/user/lib/Logger.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#ifndef __Logger_Include_H_
|
||||||
|
#define __Logger_Include_H_
|
||||||
|
|
||||||
|
#include "devices/CGA.h"
|
||||||
|
#include "lib/Semaphore.h"
|
||||||
|
|
||||||
|
class Logger {
|
||||||
|
private:
|
||||||
|
Logger(const Logger& copy) = delete;
|
||||||
|
|
||||||
|
char* name;
|
||||||
|
static bool kout_enabled;
|
||||||
|
static bool serial_enabled;
|
||||||
|
|
||||||
|
// TODO: Don't mix logs
|
||||||
|
static const Semaphore sem;
|
||||||
|
|
||||||
|
void log(char* message, CGA::color col);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Logger(char* name) : name(name) {}
|
||||||
|
|
||||||
|
enum LogLevel {
|
||||||
|
TRACE = 0,
|
||||||
|
DEBUG = 1,
|
||||||
|
ERROR = 2,
|
||||||
|
INFO = 3
|
||||||
|
};
|
||||||
|
static LogLevel level;
|
||||||
|
|
||||||
|
void trace(char* message);
|
||||||
|
void debug(char* message);
|
||||||
|
void error(char* message);
|
||||||
|
void info(char* message);
|
||||||
|
|
||||||
|
// TODO: Make level change accessible over menu
|
||||||
|
static void set_level(LogLevel level) {
|
||||||
|
Logger::level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void enable_kout() {
|
||||||
|
Logger::kout_enabled = true;
|
||||||
|
}
|
||||||
|
static void disable_kout() {
|
||||||
|
Logger::kout_enabled = false;
|
||||||
|
}
|
||||||
|
static void enable_serial() {
|
||||||
|
Logger::serial_enabled = true;
|
||||||
|
}
|
||||||
|
static void disable_serial() {
|
||||||
|
Logger::serial_enabled = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user