Files
uniLoader/lib/debug/debug.c
Ivaylo Ivanov 280972aec2 lib: debug: add printf functionality with nanoprintf
Add the respectively required definitions in unic.
2025-07-08 19:02:39 +03:00

94 lines
2.1 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2022, Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
*/
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <lib/debug.h>
#include <lib/simplefb.h>
#include <lib/video/font.h>
#define NANOPRINTF_IMPLEMENTATION
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_SMALL_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_ALT_FORM_FLAG 1
#include <lib/nanoprintf.h>
#define PRINTK_BUFFER_SIZE 256
long int debug_linecount = 0;
void uart_puts(const char *s);
// Global log level that controls the verbosity
static int global_loglevel = CONFIG_LOGLEVEL;
void printk(int log_level, const char *fmt, ...)
{
static char print_buffer[PRINTK_BUFFER_SIZE];
va_list args;
if (log_level > global_loglevel)
return;
char *prefix;
switch (log_level) {
case KERN_EMERG:
prefix = "[EMERG] ";
break;
case KERN_ALERT:
prefix = "[ALERT] ";
break;
case KERN_CRIT:
prefix = "[CRIT] ";
break;
case KERN_ERR:
prefix = "[ERROR] ";
break;
case KERN_WARNING:
prefix = "[WARN] ";
break;
case KERN_NOTICE:
prefix = "[NOTICE] ";
break;
case KERN_INFO:
prefix = "[INFO] ";
break;
case KERN_DEBUG:
prefix = "[DEBUG] ";
break;
default:
prefix = "[LOG] ";
break;
}
va_start(args, fmt);
npf_vsnprintf(print_buffer, sizeof(print_buffer), fmt, args);
va_end(args);
#ifdef CONFIG_UART_DEBUG
uart_puts(prefix);
uart_puts(print_buffer);
uart_puts("\r");
#endif
#ifdef CONFIG_SIMPLE_FB
// Actually the first line's Y pos. Scales in __simplefb_raw_print
int y_pos = 5;
int prefix_width = strlen(prefix) * SCALED_FONTW;
__simplefb_raw_print((char*)CONFIG_FRAMEBUFFER_BASE, prefix, 0, y_pos,
CONFIG_FRAMEBUFFER_WIDTH, CONFIG_FRAMEBUFFER_STRIDE);
__simplefb_raw_print((char*)CONFIG_FRAMEBUFFER_BASE, print_buffer, prefix_width, y_pos,
CONFIG_FRAMEBUFFER_WIDTH, CONFIG_FRAMEBUFFER_STRIDE);
#endif
}