mtl/src/gba/log.cpp
Myles Busig b53bedbcb7 Add ability to disable logging
If MTL_LOGGING_DISABLED is defined, logging is disabled. Otherwise, it
is enabled.
2024-06-19 19:36:26 -06:00

63 lines
1.5 KiB
C++

#include "mtl/log.hpp"
#include "mtl/string.hpp"
#include "mtl/string_view.hpp"
#include "mtl/string_stream.hpp"
namespace mtl {
namespace log {
/**
* \brief MGBA debug registers and constants
*/
namespace reg {
/**
* \brief MGBA debug enable register.
*
* When 0xC0DE is written to the register, debug output is enabled.
* When 0 is written to the register, debug output is disabled.
* Will be set to 0x1DEA when successfully enabled.
*/
static volatile uint16_t& enable = *reinterpret_cast<uint16_t*>(0x4FFF780);
/**
* \brief MGBA debug flags register.
*
* When 0x100 | LOG_LEVEL, the message buffer is flushed. MGBA levels are:
*
* Fatal: 0 (stops the emulator, unused)
* Error: 1
* Warn: 2
* Info: 3
* Debug: 4
*/
static volatile uint16_t& flags = *reinterpret_cast<uint16_t*>(0x4FFF700);
/**
* \brief MGBA debug string register.
*
* Max length of 0x100 (256) including null terminator.
*/
static string_ext string(reinterpret_cast<char*>(0x4FFF600), 256);
}; // namespace reg
stream::stream(istring& buf, level log_level)
: basic_string_stream(buf), m_log_level(log_level) {
reg::enable = 0xC0DE;
}
stream::~stream() {
reg::enable = 0;
}
void stream::flush() {
reg::flags = 0x100 | static_cast<uint32_t>(m_log_level);
clear();
}
typeof(debug) debug(reg::string, level::debug);
typeof(info) info(reg::string, level::info);
typeof(warn) warn(reg::string, level::warn);
typeof(error) error(reg::string, level::error);
} // namespace log
} // namespace mtl