mtl/include/mtl/log.hpp
Myles Busig e8804cff5b Add log::stream_type typedef
This addition is useful when an explicit template instantiation of
operator<< is needed, for example, when logging from an ARM mode
function. Example usage:

template mtl::log::stream_type& mtl::log::stream_type::operator<<
<uint16_t>(uint16_t);

ARM_MODE void foo(int16_t x) {
	mtl::log::debug << x; // Without the explicit template
	instantiation, an ODR violation would occur.
}
2024-07-28 18:08:21 -06:00

97 lines
2.0 KiB
C++

#pragma once
#include <cstdint>
#include "mtl/string_stream.hpp"
namespace mtl {
namespace log {
enum class level : uint32_t {
error = 1,
warn = 2,
info = 3,
debug = 4,
};
#ifdef __GBA__
constexpr char endl_char = 0;
#else
constexpr char endl_char = '\n';
#endif
using stream_type = basic_string_stream<false, endl_char>;
/**
* \brief Log stream
*
* String stream that overrides flush() to flush the stream to the correct
* output depending on the platform. Each flush is treated as a separate message.
*
* Different streams may share a buffer, so writing to a different stream
* without flushing the previous may cause strange behaviour.
*
* The capacity of the stream depends on the platform, but will always be at
* least 255 characters.
*
* See mtl::basic_string_stream for information on when the stream is flushed.
*
* Platform specific info:
*
* GBA: Flushing cannot be done without printing to a new line. Trying to
* flush without using mtl::endl will still flush the stream onto a new line.
* On MGBA this also starts the line with "GBA Debug: ",so the line is not
* completely blank either.
*/
class stream : public stream_type {
private:
level m_log_level;
public:
stream(istring& str, level log_level);
~stream();
using basic_string_stream::operator=;
using basic_string_stream::operator<<;
void flush() override;
};
/**
* \brief Stub log stream
*
* Implements all of the log stream functions as a NOP. Replaces all of
* the log streams when logging is disabled.
*
* TODO: Enable logging disabling.
*/
class stream_stub {
public:
stream_stub(istring&, uint32_t) { }
void flush() { }
void clear() { }
template <typename T>
stream_stub& operator<<(T) {
return *this;
}
};
#ifdef MTL_LOGGING_DISABLED
extern stream_stub debug;
extern stream_stub info;
extern stream_stub warn;
extern stream_stub error;
#else
extern stream debug;
extern stream info;
extern stream warn;
extern stream error;
#endif
} // namespace log
} // namespace mtl