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