From 3f86932ad847915035a94ccb21095028b8f41cdb Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 3 Dec 2022 16:48:07 -0600 Subject: [PATCH] [ox/std] Add mGBA logging --- deps/ox/src/ox/std/def.hpp | 9 ++++- deps/ox/src/ox/std/string.hpp | 2 +- deps/ox/src/ox/std/trace.hpp | 2 +- deps/ox/src/ox/std/tracehook.cpp | 63 ++++++++++++++++++++++++++++++-- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/deps/ox/src/ox/std/def.hpp b/deps/ox/src/ox/std/def.hpp index 2ecd87c9..60cb34c5 100644 --- a/deps/ox/src/ox/std/def.hpp +++ b/deps/ox/src/ox/std/def.hpp @@ -34,8 +34,13 @@ #define oxErrorf(...) oxTracef("error", __VA_ARGS__) #ifndef OX_NODEBUG -#define oxDebug(...) oxTrace("debug", __VA_ARGS__) -#define oxDebugf(...) oxTracef("debug", __VA_ARGS__) +#define oxDebug(...) ox::trace::OutStream(__FILE__, __LINE__, "debug", __VA_ARGS__) +#ifdef OX_USE_STDLIB +// Non-GCC compilers don't like ##__VA_ARGS__, so use initializer list, which relies on std lib +#define oxDebugf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "debug", ox::detail::fmtSegments(fmt), {__VA_ARGS__}) +#else +#define oxDebugf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "debug", ox::detail::fmtSegments(fmt), ##__VA_ARGS__) +#endif #else #define oxDebug(...) static_assert(false, "Debug prints were checked in."); #define oxDebugf(...) static_assert(false, "Debug prints were checked in."); diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index ee48b7cc..f8863c3a 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -547,7 +547,7 @@ constexpr bool BasicString::endsWith(const BasicString &endin template constexpr std::size_t BasicString::bytes() const noexcept { std::size_t i; - for (i = 0; i < m_buff.size() && m_buff[i]; i++); + for (i = 0; i < m_buff.size() && m_buff[i]; ++i); return i + 1; // add one for null terminator } diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index 8472ccf0..21d5621b 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -209,7 +209,7 @@ class NullStream { }; -#if defined(DEBUG) && !defined(OX_BARE_METAL) +#if defined(DEBUG) using TraceStream = OutStream; #else using TraceStream = NullStream; diff --git a/deps/ox/src/ox/std/tracehook.cpp b/deps/ox/src/ox/std/tracehook.cpp index 444214e4..96c9b52e 100644 --- a/deps/ox/src/ox/std/tracehook.cpp +++ b/deps/ox/src/ox/std/tracehook.cpp @@ -6,13 +6,58 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#if defined(OX_USE_STDLIB) +#if !defined(OX_USE_STDLIB) + +#include "math.hpp" +#include "stringview.hpp" +#include "types.hpp" + +#define REG_DEBUG_ENABLE *reinterpret_cast(0x4FFF780) +#define REG_DEBUG_FLAGS *reinterpret_cast(0x4FFF700) +#define REG_DEBUG_STRING (reinterpret_cast(0x4FFF600)) + +namespace ox::hw { +static std::size_t (*infoLog)(ox::CRStringView) = [](ox::CRStringView) -> std::size_t { return 0; }; +static std::size_t (*debugLog)(ox::CRStringView) = [](ox::CRStringView) -> std::size_t { return 0; }; +static std::size_t (*errorLog)(ox::CRStringView) = [](ox::CRStringView) -> std::size_t { return 0; }; +} + +namespace mgba { + +enum LogChan { + LOG_FATAL = 0, + LOG_ERROR = 1, + LOG_WARN = 2, + LOG_INFO = 3, + LOG_DEBUG = 4 +}; + +template +static auto mkLogger() { + return [](ox::CRStringView str) { + const auto sz = ox::min(0x100, str.bytes()); + ox_strncpy(REG_DEBUG_STRING, str.data(), sz); + REG_DEBUG_FLAGS = chan | 0x100; + return sz; + }; +} + +void initConsole() { + REG_DEBUG_ENABLE = 0xC0DE; + if (REG_DEBUG_ENABLE == 0x1DEA) { + ox::hw::infoLog = mgba::mkLogger(); + ox::hw::debugLog = mgba::mkLogger(); // use INFO because mGBA disables DEBUG on start + ox::hw::errorLog = mgba::mkLogger(); + } +} + +} + +#else #include #include static const auto OxPrintTrace = std::getenv("OXTRACE") != nullptr; -#else -constexpr auto OxPrintTrace = false; #endif #include "strops.hpp" @@ -38,6 +83,18 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line, } else if (ox_strcmp(ch, "error") == 0) { std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n'; } +#else + if (ox_strcmp(ch, "info") == 0) { + ox::hw::infoLog(msg); + } else if (ox_strcmp(ch, "debug") == 0) { + ox::hw::debugLog(msg); + } else if (ox_strcmp(ch, "stdout") == 0) { + ox::hw::infoLog(msg); + } else if (ox_strcmp(ch, "stderr") == 0) { + ox::hw::errorLog(msg); + } else if (ox_strcmp(ch, "error") == 0) { + ox::hw::errorLog(msg); + } #endif }