[ox/std] Cleanup mGBA logging

This commit is contained in:
Gary Talent 2022-12-05 17:28:42 -06:00
parent cd4a9300a5
commit 030d46a999

View File

@ -16,37 +16,34 @@
#define REG_MGBA_DEBUG_FLAGS *reinterpret_cast<volatile uint16_t*>(0x4FFF700)
#define REG_MGBA_DEBUG_STRING (reinterpret_cast<char*>(0x4FFF600))
namespace ox::hw {
static void (*infoLog)(ox::CRStringView) = [](ox::CRStringView) {};
static void (*debugLog)(ox::CRStringView) = [](ox::CRStringView) {};
static void (*errorLog)(ox::CRStringView) = [](ox::CRStringView) {};
}
inline void nullLog(ox::CRStringView) {}
inline void (*infoLog)(ox::CRStringView) = nullLog;
inline void (*debugLog)(ox::CRStringView) = nullLog;
inline void (*errorLog)(ox::CRStringView) = nullLog;
namespace mgba {
enum LogChan {
LOG_FATAL = 0,
LOG_ERROR = 1,
LOG_WARN = 2,
LOG_INFO = 3,
LOG_DEBUG = 4
Fatal = 0,
Error = 1,
Warn = 2,
Info = 3,
Debug = 4,
};
template<LogChan chan>
static auto mkLogger() {
return [](ox::CRStringView str) {
const auto sz = ox::min<std::size_t>(0x100, str.bytes());
ox_strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz);
REG_MGBA_DEBUG_FLAGS = chan | 0x100;
};
static void log(ox::CRStringView str) {
const auto sz = ox::min<std::size_t>(0x100, str.bytes());
ox_strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz);
REG_MGBA_DEBUG_FLAGS = chan | 0x100;
}
void initConsole() {
REG_MGBA_DEBUG_ENABLE = 0xC0DE;
if (REG_MGBA_DEBUG_ENABLE == 0x1DEA) {
ox::hw::infoLog = mgba::mkLogger<LOG_INFO>();
ox::hw::debugLog = mgba::mkLogger<LOG_INFO>(); // use INFO because mGBA disables DEBUG on start
ox::hw::errorLog = mgba::mkLogger<LOG_ERROR>();
infoLog = log<LogChan::Info>;
debugLog = log<LogChan::Info>; // use INFO because mGBA disables DEBUG on start
errorLog = log<LogChan::Error>;
}
}
@ -84,15 +81,15 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
}
#else
if (ox_strcmp(ch, "info") == 0) {
ox::hw::infoLog(msg);
infoLog(msg);
} else if (ox_strcmp(ch, "debug") == 0) {
ox::hw::debugLog(msg);
debugLog(msg);
} else if (ox_strcmp(ch, "stdout") == 0) {
ox::hw::infoLog(msg);
infoLog(msg);
} else if (ox_strcmp(ch, "stderr") == 0) {
ox::hw::errorLog(msg);
errorLog(msg);
} else if (ox_strcmp(ch, "error") == 0) {
ox::hw::errorLog(msg);
errorLog(msg);
}
#endif
}