Browse Source

terminal frontend

master
Mathieu Serandour 11 months ago
parent
commit
ccf2dd8160
  1. 15
      kernel/lib/logging.c
  2. 45
      kernel/lib/panic.c
  3. 21
      kernel/lib/sprintf.c
  4. 2
      kernel/lib/sprintf.h
  5. 24
      kernel/lib/string_t.h

15
kernel/lib/logging.c

@ -29,17 +29,21 @@ static inline void append_string(const char* str) {
}
static const char* get_level_names_and_set_terminal_color(unsigned level) {
driver_t* terminal = get_active_terminal();
switch(level) {
case LOG_LEVEL_DEBUG:
set_terminal_fgcolor(LOG_DEBUG_COLOR);
if(terminal)
terminal_set_fgcolor(terminal,LOG_DEBUG_COLOR);
return "[DEBUG] ";
case LOG_LEVEL_INFO:
set_terminal_fgcolor(LOG_INFO_COLOR);
if(terminal)
terminal_set_fgcolor(terminal,LOG_INFO_COLOR);
return "[INFO] ";
default:// level > warning -> warning.
case LOG_LEVEL_WARN:
set_terminal_fgcolor(LOG_WARNIN_COLOR);
if(terminal)
terminal_set_fgcolor(terminal,LOG_WARNIN_COLOR);
return "[WARNING] ";
}
}
@ -50,11 +54,14 @@ void log(unsigned level, const char* string) {
const char* level_name = get_level_names_and_set_terminal_color(level);
driver_t* terminal = get_active_terminal();
// print on the screen
// with fancy colors
puts(level_name);
set_terminal_fgcolor(TEXT_COLOR);
if(terminal)
terminal_set_fgcolor(terminal, TEXT_COLOR);
puts(string);
puts("\n");

45
kernel/lib/panic.c

@ -40,31 +40,32 @@ static inline __attribute__((always_inline)) void stack_trace(void) {
__attribute__((noreturn)) void panic(const char* panic_string) {
// checks if video is operationnal
if(get_terminal_handler() != NULL) {
terminal_set_colors(0xfff0a0, 0x400000);
driver_t* terminal = get_active_terminal();
if(panic_string == NULL)
panic_string = "(null)";
if(terminal) // well, we cannot print colors
terminal_set_colors(terminal, 0xfff0a0, 0x400000);
puts(
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
"!!!!!!!!!!!!! KERNL PANIC !!!!!!!!!!!!!\n"
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
puts(panic_string);
puts("\n\n");
stack_trace();
if(panic_string == NULL)
panic_string = "(null)";
puts(
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
"!!!!!!!!!!!!! KERNL PANIC !!!!!!!!!!!!!\n"
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
puts(panic_string);
puts("\n\n");
stack_trace();
puts(
"\n\n"
"type ESCAPE to shutdown the computer.\n"
);
// do not make any interrupt
ps2kb_poll_wait_for_key(PS2KB_ESCAPE);
shutdown();
puts(
"\n\n"
"type ESCAPE to shutdown the computer.\n"
);
// do not make any interrupt
ps2kb_poll_wait_for_key(PS2KB_ESCAPE);
shutdown();
}
asm volatile("cli");
asm volatile("hlt");

21
kernel/lib/sprintf.c

@ -193,11 +193,20 @@ int sprintf(char* str, const char* format, ...) {
//
//}
void puts(const char* s) {
terminal_handler_t print_fun = get_terminal_handler();
void default_backend(const char* string, size_t len) {
(void)(string + len);
}
static void (* backend_fun)(const char *, size_t) = default_backend;
if(print_fun)
print_fun(s, strlen(s));
void set_backend_print_fun(void (*fun)(const char *string, size_t length)) {
backend_fun = fun;
}
void puts(const char* s) {
backend_fun(s, strlen(s));
}
int printf(const char* format, ...) {
@ -215,13 +224,13 @@ int printf(const char* format, ...) {
}
int vprintf(const char* format, va_list ap) {
terminal_handler_t print_fun = get_terminal_handler();
char buf[1024];
int ret = vsprintf(buf, format, ap);
print_fun(buf, strlen(buf));
puts(buf);
return ret;
}

2
kernel/lib/sprintf.h

@ -17,4 +17,6 @@ int printf(const char* format, ...);
void puts(const char* s);
void set_backend_print_fun(void (*fun)(const char *string, size_t length));
#endif

24
kernel/lib/string_t.h

@ -0,0 +1,24 @@
#pragma once
#include <stddef.h>
#include "../memory/heap.h"
// this file defines
// the string_t structure
typedef struct {
const char* ptr;
char freeable;
} string_t;
static inline string_t string_alloc(size_t len) {
return (string_t){
.ptr = (char*)malloc(len),
.freeable = 1
};
}
static inline void string_free(string_t* s) {
if(s->freeable)
free((void*)s->ptr);
}
Loading…
Cancel
Save