Skip to content

Commit

Permalink
Add logging!
Browse files Browse the repository at this point in the history
Relies on kitling/flashcart_core#64
  • Loading branch information
kitlith committed Sep 28, 2017
1 parent 9611ea5 commit 46ac156
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ CFLAGS += $(INCLUDE) -DARM9 -D_GNU_SOURCE\
-DNTRBOOT_FLASHER_VERSION=\"$(NTRBOOT_FLASHER_VERSION)\"\
-DFLASHCART_CORE_VERSION=\"$(FLASHCART_CORE_VERSION)\"

ifdef DEBUG
CFLAGS += -D LOG_LEVEL=LOG_DEBUG
endif

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions

ASFLAGS := -g $(ARCH)
Expand Down
2 changes: 1 addition & 1 deletion flashcart_core
2 changes: 2 additions & 0 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ void ntrboot_flasher()
goto reselect_cart; // keeps code complexity down ¯\_(ツ)_/¯
break;
case MENU_EXIT:
close_logfile();
ELM_Unmount();
return;
case MENU_REBOOT:
close_logfile();
ELM_Unmount();
i2cReboot();
return;
Expand Down
1 change: 1 addition & 0 deletions source/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bool menu_show_intro_warning()
}

void menu_unmount() {
close_logfile();
ELM_Unmount();
ClearScreen(TOP_SCREEN, STD_COLOR_BG);
DrawStringF(TOP_SCREEN, 10, 10, STD_COLOR_FONT, STD_COLOR_BG, "It is now safe to remove your SD card.");
Expand Down
3 changes: 3 additions & 0 deletions source/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ bool menu_show_intro_warning();
void menu_wait_cart_insert();
int8_t menu_select_flashcart();
uint8_t menu_flashcart_menu(const char* flashcart_name);

// From platform.cpp
void close_logfile(void);
52 changes: 51 additions & 1 deletion source/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,60 @@
#include "protocol_ntr.h"
#include "ui.h"

#include <cstdarg>

void Flashcart::sendCommand(const uint8_t *cmdbuf, uint16_t response_len, uint8_t *resp, uint32_t flags) {
NTR_SendCommand(cmdbuf, response_len, flags, resp);
}

void Flashcart::showProgress(uint32_t current, uint32_t total, const char* status_string) {
ShowProgress(BOTTOM_SCREEN, current, total, status_string);
}
}

static FILE *logfile = nullptr;

static void open_logfile(void) {
static bool first_open = true;
// We want to overwrite if this is our first time opening the file this run.
logfile = fopen("fat1:/ntrboot/ntrboot.log", first_open ? "w" : "a");
first_open = false;
}

void close_logfile(void) {
fclose(logfile);
logfile = nullptr;
}

#ifndef LOG_LEVEL
#define LOG_LEVEL LOG_INFO
#endif

int Flashcart::logMessage(log_priority priority, const char *fmt, ...) {
if (priority < LOG_LEVEL) return 0;
if (!logfile) {
open_logfile(); // automagicly open.
if (!logfile) return -1;
}
va_list args;
va_start(args, fmt);

static char const *const priority_strings[] = {
[LOG_DEBUG] = "DEBUG",
[LOG_INFO] = "INFO",
[LOG_NOTICE] = "NOTICE",
[LOG_WARN] = "WARN",
[LOG_ERR] = "ERROR"
};
const char *priority_str = (priority >= LOG_PRIORITY_MAX) ? "?!#$" : priority_strings[priority];

char *log_fmt;
if (asprintf(&log_fmt, "[%s]: %s\n", priority_str, fmt) < 0) {
return -1; // would pass the actual return value back, but I don't think we care.
}

int result = vfprintf(logfile, log_fmt, args);
va_end(args);

free(log_fmt);
return result;
}

0 comments on commit 46ac156

Please sign in to comment.