2018-09-23 22:18:41 +01:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2018-09-23 22:18:41 +01:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "log.h"
|
2018-09-26 01:13:20 +01:00
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
#ifdef FUSEE_STAGE2_SRC
|
|
|
|
#include "../../../fusee/fusee-secondary/src/console.h"
|
2018-09-26 01:13:20 +01:00
|
|
|
#include <stdio.h>
|
2020-11-10 18:44:50 +00:00
|
|
|
#else
|
|
|
|
#include "display/video_fb.h"
|
|
|
|
#include "vsprintf.h"
|
|
|
|
#endif
|
2018-09-23 22:18:41 +01:00
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Default log level for screen output. */
|
2018-09-28 21:12:59 +01:00
|
|
|
ScreenLogLevel g_screen_log_level = SCREEN_LOG_LEVEL_NONE;
|
2018-09-23 22:18:41 +01:00
|
|
|
|
|
|
|
void log_set_log_level(ScreenLogLevel log_level) {
|
|
|
|
g_screen_log_level = log_level;
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:12:59 +01:00
|
|
|
ScreenLogLevel log_get_log_level() {
|
|
|
|
return g_screen_log_level;
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:18:41 +01:00
|
|
|
void log_to_uart(const char *message) {
|
2020-11-10 18:44:50 +00:00
|
|
|
/* TODO: Add UART logging. */
|
2018-09-23 22:18:41 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:12:59 +01:00
|
|
|
static void print_to_screen(ScreenLogLevel screen_log_level, char *message) {
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Don't print to screen if below log level */
|
2018-09-23 23:02:58 +01:00
|
|
|
if(screen_log_level > g_screen_log_level) return;
|
2020-11-10 18:44:50 +00:00
|
|
|
|
|
|
|
#ifdef FUSEE_STAGE2_SRC
|
2018-09-23 22:18:41 +01:00
|
|
|
printf(message);
|
2020-11-10 18:44:50 +00:00
|
|
|
#else
|
|
|
|
video_puts(message);
|
|
|
|
#endif
|
2018-09-23 22:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-23 23:02:58 +01:00
|
|
|
* vprintk - logs a message and prints it to screen based on its screen_log_level
|
2018-09-23 22:18:41 +01:00
|
|
|
*
|
2018-09-23 23:02:58 +01:00
|
|
|
* If the level is below g_screen_log_level it will not be shown but logged to UART
|
|
|
|
* This text will not be colored or prefixed
|
2018-09-23 22:18:41 +01:00
|
|
|
* UART is TODO
|
|
|
|
*/
|
2018-09-23 23:02:58 +01:00
|
|
|
void vprint(ScreenLogLevel screen_log_level, const char *fmt, va_list args)
|
2018-09-23 22:18:41 +01:00
|
|
|
{
|
|
|
|
char buf[PRINT_MESSAGE_MAX_LENGTH];
|
|
|
|
vsnprintf(buf, PRINT_MESSAGE_MAX_LENGTH, fmt, args);
|
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* We don't need that flag here, but if it gets used, strip it so we print correctly. */
|
2018-09-26 01:47:24 +01:00
|
|
|
screen_log_level &= ~SCREEN_LOG_LEVEL_NO_PREFIX;
|
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Log to UART. */
|
2018-09-23 22:18:41 +01:00
|
|
|
log_to_uart(buf);
|
|
|
|
|
2018-09-23 23:02:58 +01:00
|
|
|
print_to_screen(screen_log_level, buf);
|
2018-09-23 22:18:41 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:12:59 +01:00
|
|
|
static void add_prefix(ScreenLogLevel screen_log_level, const char *fmt, char *buf) {
|
2018-09-23 22:18:41 +01:00
|
|
|
char typebuf[] = "[%s] %s";
|
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Apply prefix and append message format. */
|
|
|
|
/* TODO: Add coloring to the output. */
|
2018-09-23 23:02:58 +01:00
|
|
|
switch(screen_log_level)
|
2018-09-23 22:18:41 +01:00
|
|
|
{
|
|
|
|
case SCREEN_LOG_LEVEL_ERROR:
|
|
|
|
snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "ERROR", fmt);
|
|
|
|
break;
|
|
|
|
case SCREEN_LOG_LEVEL_WARNING:
|
|
|
|
snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "WARNING", fmt);
|
|
|
|
break;
|
|
|
|
case SCREEN_LOG_LEVEL_MANDATORY:
|
|
|
|
snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, "%s", fmt);
|
|
|
|
break;
|
|
|
|
case SCREEN_LOG_LEVEL_INFO:
|
|
|
|
snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "INFO", fmt);
|
|
|
|
break;
|
|
|
|
case SCREEN_LOG_LEVEL_DEBUG:
|
|
|
|
snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, typebuf, "DEBUG", fmt);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-09-25 22:43:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* print - logs a message and prints it to screen based on its screen_log_level
|
2020-11-10 18:44:50 +00:00
|
|
|
*
|
2018-09-25 22:43:52 +01:00
|
|
|
* If the level is below g_screen_log_level it will not be shown but logged to UART
|
|
|
|
* Use SCREEN_LOG_LEVEL_NO_PREFIX if you don't want a prefix to be added
|
|
|
|
* UART is TODO
|
|
|
|
*/
|
|
|
|
void print(ScreenLogLevel screen_log_level, const char * fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[PRINT_MESSAGE_MAX_LENGTH] = {};
|
|
|
|
char message[PRINT_MESSAGE_MAX_LENGTH] = {};
|
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Make splash disappear if level is ERROR or WARNING. */
|
|
|
|
#ifdef FUSEE_STAGE2_SRC
|
2019-01-26 08:50:38 +00:00
|
|
|
if (screen_log_level < SCREEN_LOG_LEVEL_MANDATORY) {
|
|
|
|
console_resume();
|
|
|
|
}
|
2020-11-10 18:44:50 +00:00
|
|
|
#endif
|
2018-09-25 22:43:52 +01:00
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Make prefix free messages with log_level possible. */
|
2018-09-25 22:43:52 +01:00
|
|
|
if(screen_log_level & SCREEN_LOG_LEVEL_NO_PREFIX) {
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Remove the NO_PREFIX flag so the enum can be recognized later on. */
|
2018-09-25 22:43:52 +01:00
|
|
|
screen_log_level &= ~SCREEN_LOG_LEVEL_NO_PREFIX;
|
|
|
|
|
|
|
|
snprintf(buf, PRINT_MESSAGE_MAX_LENGTH, "%s", fmt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
add_prefix(screen_log_level, fmt, buf);
|
|
|
|
}
|
2018-09-23 22:18:41 +01:00
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Input arguments. */
|
2018-09-23 22:18:41 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(message, PRINT_MESSAGE_MAX_LENGTH, buf, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2020-11-10 18:44:50 +00:00
|
|
|
/* Log to UART. */
|
2018-09-23 22:18:41 +01:00
|
|
|
log_to_uart(message);
|
|
|
|
|
2018-09-23 23:02:58 +01:00
|
|
|
print_to_screen(screen_log_level, message);
|
2018-09-23 22:18:41 +01:00
|
|
|
}
|