-
Notifications
You must be signed in to change notification settings - Fork 2
/
printk.c
70 lines (55 loc) · 1.28 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "string.h"
#include "ports.h"
#include "types.h"
#include "vga.h"
#include "macro.h"
#include "printk.h"
#define PRINTK_BUFFER 1024
#define MAX_HANDLERS 5
static void (*printk_handlers[MAX_HANDLERS])(const char *);
static unsigned num_handlers = 0;
const char const * log_level_str[] = {
[INFO] = "INFO",
[WARN] = "WARN",
[ERROR] = "ERROR",
[CRITICAL] = "CRITICAL",
};
static const char *
log_level_to_string(enum log_level level)
{
if (level >= 0 && level < ELEMENTS_OF(log_level_str))
return log_level_str[level];
else
return "UNKOWN";
}
int
printk__register_handler(void (*handler)(const char *))
{
if (handler == NULL)
return -1;
if (num_handlers > ELEMENTS_OF(printk_handlers))
return -1;
printk_handlers[num_handlers++] = handler;
return 0;
}
void
printk(enum log_level level, const char *fmt, ...)
{
char buffer[PRINTK_BUFFER];
va_list args;
int l;
unsigned i;
sprintf(buffer, "[%s] ", log_level_to_string(level));
l = strlen(buffer);
va_start(args, fmt);
l = vsprintf(&buffer[l], fmt, args);
va_end(args);
vga__puts(buffer);
for (i = 0; i < num_handlers; i++)
printk_handlers[i](buffer);
}
void
printk__init(void)
{
vga__clear();
}