From 5f8f5140eb6d7aa204d2565eec29fd7a2a934906 Mon Sep 17 00:00:00 2001 From: Oxore Date: Fri, 29 Dec 2023 00:20:57 +0300 Subject: Introduce runtime log level switching --- debug.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 debug.c (limited to 'debug.c') diff --git a/debug.c b/debug.c new file mode 100644 index 0000000..eff2654 --- /dev/null +++ b/debug.c @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: Unlicense */ + +#include "debug.h" + +#include + +int g_log_level = LOG_LEVEL_INFO; + +static char ByteToPrintableChar(uint32_t byte) +{ + return (byte >= 0x20 && byte <= 0x7f) ? byte : '.'; +} + +void FPrintRaw(FILE *s, void const *data_arg, size_t size) +{ + const size_t cols = 16; + const size_t section_width = 8; + if (size == 0) { + return; + } + uint8_t const *data = data_arg; + for (size_t line = 0; line < (size - 1) / cols + 1; line++) { + fprintf(s, "%08zx: ", line * cols); + for (size_t c = 0; c < cols; c++) { + fprintf(s, "%02x ", data[line * cols + c]); + if (c % section_width == section_width - 1 && c != cols - 1) { + fprintf(s, " "); + } + } + fprintf(s, " |"); + for (size_t c = 0; c < cols; c++) { + fprintf(s, "%c", ByteToPrintableChar(data[line * cols + c])); + if (c % section_width == section_width - 1 && c != cols - 1) { + fprintf(s, " "); + } + } + fprintf(s, "|\n"); + } +} -- cgit v1.2.3