mirror of
https://github.com/tailix/kernel.git
synced 2024-11-27 11:24:34 -05:00
155 lines
3.7 KiB
C
155 lines
3.7 KiB
C
#include "logger.h"
|
|
|
|
#include <kernaux/console.h>
|
|
#include <kernaux/stdlib.h>
|
|
|
|
#define LEVELS_COUNT 4
|
|
|
|
static const char *const level_text[LEVELS_COUNT] = {
|
|
"DBUG",
|
|
"INFO",
|
|
"WARN",
|
|
"FAIL",
|
|
};
|
|
|
|
static void print_prefix(unsigned char level, const char *source);
|
|
|
|
void logger_log(unsigned char level, const char *const source, const char *format, ...)
|
|
{
|
|
if (level >= LEVELS_COUNT) {
|
|
level = LEVELS_COUNT - 1;
|
|
}
|
|
|
|
print_prefix(level, source);
|
|
|
|
char **arg = (char **) &format;
|
|
int c;
|
|
char buf[20];
|
|
arg++;
|
|
|
|
while ((c = *format++) != 0)
|
|
{
|
|
if (c == '\n') {
|
|
kernaux_console_putc('\n');
|
|
print_prefix(level, source);
|
|
}
|
|
else if (c != '%') {
|
|
kernaux_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':
|
|
kernaux_itoa(*((int*)arg++), buf, c);
|
|
p = buf;
|
|
goto string;
|
|
break;
|
|
case 's':
|
|
p = *arg++;
|
|
if (! p)
|
|
p = "(null)";
|
|
string:
|
|
for (p2 = p; *p2; p2++);
|
|
for (; p2 < p + pad; p2++)
|
|
kernaux_console_putc(pad0 ? '0' : ' ');
|
|
while (*p)
|
|
kernaux_console_putc(*p++);
|
|
break;
|
|
default:
|
|
kernaux_console_putc(*((int *) arg++));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
kernaux_console_putc('\n');
|
|
}
|
|
|
|
void print_prefix(const unsigned char level, const char *const source)
|
|
{
|
|
kernaux_console_putc('[');
|
|
kernaux_console_print(level_text[level]);
|
|
kernaux_console_putc(']');
|
|
kernaux_console_putc(' ');
|
|
|
|
if (source) {
|
|
kernaux_console_print(source);
|
|
kernaux_console_print(": ");
|
|
}
|
|
}
|
|
|
|
void print(const char *format, ...)
|
|
{
|
|
char **arg = (char **) &format;
|
|
int c;
|
|
char buf[20];
|
|
arg++;
|
|
|
|
while ((c = *format++) != 0)
|
|
{
|
|
if (c == '\n') {
|
|
if (*format != 0) {
|
|
kernaux_console_putc('\n');
|
|
}
|
|
}
|
|
else if (c != '%') {
|
|
kernaux_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':
|
|
kernaux_itoa(*((int*)arg++), buf, c);
|
|
p = buf;
|
|
goto string;
|
|
break;
|
|
case 's':
|
|
p = *arg++;
|
|
if (! p)
|
|
p = "(null)";
|
|
string:
|
|
for (p2 = p; *p2; p2++);
|
|
for (; p2 < p + pad; p2++)
|
|
kernaux_console_putc(pad0 ? '0' : ' ');
|
|
while (*p)
|
|
kernaux_console_putc(*p++);
|
|
break;
|
|
default:
|
|
kernaux_console_putc(*((int *) arg++));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
kernaux_console_putc('\n');
|
|
}
|