[ox/std] Add mGBA logging

This commit is contained in:
Gary Talent 2022-12-03 16:48:07 -06:00
parent 3040e6c90a
commit 3f86932ad8
4 changed files with 69 additions and 7 deletions

View File

@ -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<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#else
#define oxDebugf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "debug", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(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.");

View File

@ -547,7 +547,7 @@ constexpr bool BasicString<SmallStringSize_v>::endsWith(const BasicString &endin
template<std::size_t SmallStringSize_v>
constexpr std::size_t BasicString<SmallStringSize_v>::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
}

View File

@ -209,7 +209,7 @@ class NullStream {
};
#if defined(DEBUG) && !defined(OX_BARE_METAL)
#if defined(DEBUG)
using TraceStream = OutStream;
#else
using TraceStream = NullStream;

View File

@ -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<volatile uint16_t*>(0x4FFF780)
#define REG_DEBUG_FLAGS *reinterpret_cast<volatile uint16_t*>(0x4FFF700)
#define REG_DEBUG_STRING (reinterpret_cast<char*>(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<LogChan chan>
static auto mkLogger() {
return [](ox::CRStringView str) {
const auto sz = ox::min<std::size_t>(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<LOG_INFO>();
ox::hw::debugLog = mgba::mkLogger<LOG_INFO>(); // use INFO because mGBA disables DEBUG on start
ox::hw::errorLog = mgba::mkLogger<LOG_ERROR>();
}
}
}
#else
#include <iomanip>
#include <iostream>
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
}