-
Notifications
You must be signed in to change notification settings - Fork 3
/
printk.c
45 lines (37 loc) · 1.14 KB
/
printk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* printk.c
*
* Created on: Jun 27, 2021
* Author: Paulo Almeida
*/
#include "kernel/lib/printk.h"
#include "kernel/video/vga_console.h"
#include "kernel/compiler/freestanding.h"
#include "kernel/compiler/macro.h"
#include "kernel/lib/string.h"
#include "kernel/lib/vsnprintf.h"
#include "kernel/device/serial.h"
static uint8_t logging_level = PRINTK_INFO_LEVEL;
static char buffer[1024];
void printk_init(const uint8_t level) {
/* sanity checks */
if (level > PRINTK_DEBUG_LEVEL) {
printk_error("Debug level doesn't exist, setting PRINTK_INFO_LEVEL");
logging_level = PRINTK_INFO_LEVEL;
} else {
logging_level = level;
}
}
void printk(const uint8_t level, const char *fmt, ...) {
/* check the configured logging level */
if (level > logging_level)
return;
size_t buffer_size = ARR_SIZE(buffer);
memset(buffer, '\0', buffer_size);
va_list args;
va_start(args, fmt);
size_t buf_pointer = vsnprintf(buffer, buffer_size - 1, fmt, args);
va_end(args);
write_console(buffer, buf_pointer + 1); // copy nul-terminator too
write_string_serial(buffer, buf_pointer);
}