Better sinks, better loggers, etx

This commit is contained in:
NaifBanana
2026-08-20 08:08:20 -05:00
parent 5415a01885
commit 9628a75710
14 changed files with 586 additions and 301 deletions
+65 -21
View File
@@ -1,38 +1,82 @@
#include <iostream>
#include <sstream>
#include <NBCore/Logger.hpp>
#include <NBCore/StringUtils.hpp>
namespace nb {
#ifndef _NB_NO_LOGGER
nb::DefaultDebugLogger logger(std::cout);
#endif // _NB_NO_LOGGER
static bool RUN_LOGGER(nb::DefaultDebugLogger& logger_) {
logger_.run();
return logger_.isRunning();
}
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
#ifdef _NB_AUTOLOG
DefaultDebugLogger logger({std::static_pointer_cast<LogEventHandler>(
std::make_shared<DefaultTerminalLogEventPrinter>(0x0)
)});
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
#else
const bool LOGGER_RUNNING = false;
#endif // _NB_AUTOLOG
// class LogEventHandler
LogEventHandler::LogEventHandler(uint8_t logging_level) {
_logLevel.store(logging_level, std::memory_order_release);
}
bool LogEventHandler::stop() noexcept {
_running.store(false, std::memory_order_release);
return isRunning();
}
bool LogEventHandler::run() {
_running.store(true, std::memory_order_release);
return isRunning();
}
uint8_t LogEventHandler::logLevel(uint8_t val) {
_logLevel.store(val, std::memory_order_release);
return logLevel();
}
uint8_t LogEventHandler::logLevel() const {
return _logLevel.load();
}
// class DefaultTerminalLogEventPrinter
bool DefaultTerminalLogEventPrinter::in(const LogEvent& msg) {
if (!isRunning()) {
return false;
}
if (msg.lvl < logLevel()) {
return false;
}
constexpr size_t level_field_width = 5;
constexpr size_t msg_prompt_length = level_field_width+7;
for (const auto os : this->_ostream) {
*os << "[";
os->width(level_field_width);
*os << std::to_string(msg.lvl) << "] :: ";
std::string fileloc="";
if (!msg.file.empty()) {
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
}
*os << indent_strblock(
fileloc + msg.msg,
std::string(20 - msg_prompt_length, ' '),
""
) << nb::NEWLINE;
std::stringstream formatted_stream;
formatted_stream << "[";
formatted_stream.width(level_field_width);
formatted_stream << std::to_string(msg.lvl) << "] :: ";
std::string fileloc="";
if (!msg.file.empty()) {
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
}
formatted_stream << indent_strblock(
fileloc + msg.msg,
std::string(20 - msg_prompt_length, ' '),
""
) << nb::NEWLINE;
std::string formatted_string = formatted_stream.str();
std::cout << formatted_string << std::flush;
return true;
}
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
return log.run();
// class DefaultDebugLogger
bool DefaultDebugLogger::process(const LogEvent& msg) {
return _logsink->in(msg);
}
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
} // namespace nb
+2 -2
View File
@@ -2,7 +2,7 @@
namespace nb {
using ObjectManagerCodes = ObjectManagerError::Codes;
/* using ObjectManagerCodes = ObjectManagerError::Codes;
const std::string ObjectManagerError::type = "nb::ObjectManagerError";
const ErrorCodeMap ObjectManagerError::ErrorMessages({
{ObjectManagerCodes::UNDEFINED, "Error"},
@@ -22,6 +22,6 @@ const ErrorCodeMap ObjectManagerError::ErrorMessages({
ObjectManagerCodes::BAD_THREAD,
"Attempting operation from a bad thread"
}
});
}); */
}; // namespace nb