1
0
Fork 0
mirror of https://github.com/tailix/kernel.git synced 2024-10-30 12:03:52 -04:00
kernel/kernelmq/logger.c

96 lines
2.1 KiB
C
Raw Normal View History

2017-11-01 01:01:29 -04:00
#include "logger.h"
#include "console.h"
2017-11-09 11:00:36 -05:00
#include "stdlib.h"
2017-11-01 01:01:29 -04:00
2017-11-05 23:30:25 -05:00
#define LEVELS_COUNT 4
2017-11-01 01:01:29 -04:00
2017-11-05 22:48:16 -05:00
static const char *const level_text[LEVELS_COUNT] = {
2017-11-05 23:30:25 -05:00
"DBUG",
2017-11-05 22:48:16 -05:00
"INFO",
"WARN",
"FAIL",
};
2017-11-01 01:01:29 -04:00
2017-11-05 23:24:35 -05:00
static void print_prefix(unsigned char level, const char *source);
2017-11-05 23:01:49 -05:00
2017-11-05 23:21:18 -05:00
void logger_log(unsigned char level, const char *const source, const char *format, ...)
2017-11-01 01:01:29 -04:00
{
2017-11-05 22:48:16 -05:00
if (level >= LEVELS_COUNT) {
level = LEVELS_COUNT - 1;
}
2017-11-01 01:01:29 -04:00
2017-11-05 23:24:35 -05:00
print_prefix(level, source);
2017-11-05 22:48:16 -05:00
char **arg = (char **) &format;
int c;
char buf[20];
arg++;
while ((c = *format++) != 0)
{
2017-11-05 23:01:49 -05:00
if (c == '\n') {
console_putc('\n');
2017-11-05 23:24:35 -05:00
print_prefix(level, source);
2017-11-05 23:01:49 -05:00
}
else if (c != '%') {
2017-11-05 22:48:16 -05:00
console_putc(c);
}
else {
char *p, *p2;
int pad0 = 0, pad = 0;
c = *format++;
if (c == '0')
{
pad0 = 1;
c = *format++;
}
if (c >= '0' && c <= '9')
{
pad = c - '0';
c = *format++;
}
switch (c)
{
case 'd':
case 'u':
case 'x':
kitoa (buf, c, *((int *) arg++));
p = buf;
goto string;
break;
case 's':
p = *arg++;
if (! p)
p = "(null)";
string:
for (p2 = p; *p2; p2++);
for (; p2 < p + pad; p2++)
console_putc(pad0 ? '0' : ' ');
while (*p)
console_putc(*p++);
break;
default:
console_putc(*((int *) arg++));
break;
}
}
}
console_putc('\n');
2017-11-01 01:01:29 -04:00
}
2017-11-05 23:01:49 -05:00
2017-11-05 23:24:35 -05:00
void print_prefix(const unsigned char level, const char *const source)
2017-11-05 23:01:49 -05:00
{
console_putc('[');
console_print(level_text[level]);
console_putc(']');
console_putc(' ');
2017-11-05 23:24:35 -05:00
if (source) {
console_print(source);
console_print(": ");
}
2017-11-05 23:01:49 -05:00
}