From 840242b68e41ab52caa64f954ffa33c0b387bf06 Mon Sep 17 00:00:00 2001 From: Resaec Date: Sat, 22 Sep 2018 05:14:43 +0200 Subject: [PATCH] =?UTF-8?q?started=20on=20centralized=20printing/logging?= =?UTF-8?q?=20some=20character=20problems=20with=20"atmosph=C3=A8re"=20and?= =?UTF-8?q?=20such=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fusee/fusee-secondary/src/main.c | 11 +-- fusee/fusee-secondary/src/print.c | 84 ++++++++++++++++++++ fusee/fusee-secondary/src/print.h | 38 +++++++++ fusee/fusee-secondary/src/sdmmc/sdmmc_core.c | 11 +-- 4 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 fusee/fusee-secondary/src/print.c create mode 100644 fusee/fusee-secondary/src/print.h diff --git a/fusee/fusee-secondary/src/main.c b/fusee/fusee-secondary/src/main.c index 5048bd642..89acf0589 100644 --- a/fusee/fusee-secondary/src/main.c +++ b/fusee/fusee-secondary/src/main.c @@ -32,6 +32,7 @@ #include "gpt.h" #include "display/video_fb.h" #include "sdmmc/sdmmc.h" +#include "print.h" extern void (*__program_exit_callback)(int rc); @@ -91,23 +92,23 @@ int main(int argc, void **argv) { /* Initialize the display, console, FS, etc. */ setup_env(); - printf(u8"Welcome to Atmosphère Fusée Stage 2!\n"); - printf("Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]); + print(PRINT_LOG_MANDATORY, u8"Welcome to Atmosphère Fusée Stage 2!\n"); + print(PRINT_LOG_DEBUG, "Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]); /* This will load all remaining binaries off of the SD. */ load_payload(g_stage2_args->bct0); - printf("Loaded payloads!\n"); + print(PRINT_LOG_INFO, "Loaded payloads!\n"); g_do_nxboot = loader_ctx->chainload_entrypoint == 0; if (g_do_nxboot) { - printf("Now performing nxboot.\n"); + print(PRINT_LOG_INFO, "Now performing nxboot.\n"); uint32_t boot_memaddr = nxboot_main(); nxboot_finish(boot_memaddr); } else { /* TODO: What else do we want to do in terms of argc/argv? */ const char *path = get_loader_ctx()->file_paths_to_load[get_loader_ctx()->file_id_of_entrypoint]; - printf("Now chainloading.\n"); + print(PRINT_LOG_INFO, "Now chainloading.\n"); g_chainloader_argc = 1; strcpy(g_chainloader_arg_data, path); } diff --git a/fusee/fusee-secondary/src/print.c b/fusee/fusee-secondary/src/print.c new file mode 100644 index 000000000..0cf3f2a90 --- /dev/null +++ b/fusee/fusee-secondary/src/print.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "print.h" + +#include "display/video_fb.h" + +PrintLogLevel g_print_log_level = PRINT_LOG_DEBUG; + +void vprintk(const char *fmt, va_list args) +{ + char buf[PRINT_MESSAGE_MAX_LENGTH]; + vsnprintf(buf, PRINT_MESSAGE_MAX_LENGTH, fmt, args); + video_puts(buf); +} + +void printk(const char *fmt, ...) +{ + va_list list; + va_start(list, fmt); + vprintk(fmt, list); + va_end(list); +} + +/** + * print - logs a message based on its message_level + * + * If the level is below g_message_level it will not be shown + * but logged to UART (UART is TO DO) + */ +void print(PrintLogLevel printLogLevel, const char * fmt, ...) +{ + char typebuf[] = "[%s] %s"; + char buf[PRINT_MESSAGE_MAX_LENGTH] = {}; + + /* apply prefix and append message format */ + /* TODO: add coloring to the output */ + switch(printLogLevel) + { + case PRINT_LOG_DEBUG: + snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "DEBUG", fmt); + break; + case PRINT_LOG_INFO: + snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "INFO", fmt); + break; + case PRINT_LOG_MANDATORY: + snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, "%s", fmt); + break; + case PRINT_LOG_WARNING: + snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "WARNING", fmt); + break; + case PRINT_LOG_ERROR: + snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "ERROR", fmt); + break; + default: + break; + } + + /* input arguments for UART logging */ + va_list args; + va_start(args, fmt); + vsnprintf(buf, PRINT_MESSAGE_MAX_LENGTH, buf, args); + va_end(args); + + /* TODO: implement SD and/or UART logging */ + + /* don't print to screen if below log level */ + if (printLogLevel < g_print_log_level) return; + + printk(buf); +} \ No newline at end of file diff --git a/fusee/fusee-secondary/src/print.h b/fusee/fusee-secondary/src/print.h new file mode 100644 index 000000000..9e06eff0e --- /dev/null +++ b/fusee/fusee-secondary/src/print.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef FUSEE_PRINT_H +#define FUSEE_PRINT_H + +#define PRINT_MESSAGE_MAX_LENGTH 512 + +typedef enum { + PRINT_LOG_DEBUG = 0, + PRINT_LOG_INFO, + PRINT_LOG_MANDATORY, + PRINT_LOG_WARNING, + PRINT_LOG_ERROR +} PrintLogLevel; + +#include +#include "../../fusee-primary/src/lib/vsprintf.h" + +extern PrintLogLevel g_print_log_level; + +//void vprintk(const char *fmt, va_list args); +void print(PrintLogLevel printLogLevel, const char* fmt, ...); + +#endif \ No newline at end of file diff --git a/fusee/fusee-secondary/src/sdmmc/sdmmc_core.c b/fusee/fusee-secondary/src/sdmmc/sdmmc_core.c index c831493f5..cf0adb5a2 100644 --- a/fusee/fusee-secondary/src/sdmmc/sdmmc_core.c +++ b/fusee/fusee-secondary/src/sdmmc/sdmmc_core.c @@ -31,6 +31,7 @@ #include "../pmc.h" #include "../max7762x.h" #include "../lib/driver_utils.h" +#include "../print.h" static SdmmcLogLevel g_sdmmc_log_level = SDMMC_LOG_NONE; @@ -46,21 +47,21 @@ static void sdmmc_print(sdmmc_t *sdmmc, SdmmcLogLevel log_level, char *fmt, va_l switch (log_level) { case SDMMC_LOG_ERROR: - printk("%s [ERROR]: ", sdmmc->name); + print(PRINT_LOG_ERROR, "%s", sdmmc->name); break; case SDMMC_LOG_WARN: - printk("%s [WARN]: ", sdmmc->name); + print(PRINT_LOG_WARNING, "%s", sdmmc->name); break; case SDMMC_LOG_INFO: - printk("%s [INFO]: ", sdmmc->name); + print(PRINT_LOG_INFO, "%s", sdmmc->name); break; case SDMMC_LOG_DEBUG: - printk("%s [DEBUG]: ", sdmmc->name); + print(PRINT_LOG_DEBUG, "%s", sdmmc->name); break; default: break; } - + vprintk(fmt, list); printk("\n"); }