From 2c92e07f337e42cf58970443f9de678f85a9b2a4 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 19 Oct 2023 22:50:07 -0700 Subject: The great renaming: switch to "module style" --- src/brussel.common/Log.hpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/brussel.common/Log.hpp (limited to 'src/brussel.common/Log.hpp') diff --git a/src/brussel.common/Log.hpp b/src/brussel.common/Log.hpp new file mode 100644 index 0000000..aeba984 --- /dev/null +++ b/src/brussel.common/Log.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "RingBuffer.hpp" + +#include +#include +#include +#include + +// NOTE: we keep this on one line so std::soruce_location reports the correct information +#define GENERIC_LOG(lvl, fmtString, ...) Log::Add(Log::Message{ .level = lvl, .time = std::chrono::system_clock::now(), .srcLoc = std::source_location::current(), .text = fmt::format(fmtString __VA_OPT__(, ) __VA_ARGS__) }) + +#define LOG_DEBUG(...) GENERIC_LOG(Log::MessageLevel::Debug, __VA_ARGS__) +#define LOG_INFO(...) GENERIC_LOG(Log::MessageLevel::Info, __VA_ARGS__) +#define LOG_WARNING(...) GENERIC_LOG(Log::MessageLevel::Warning, __VA_ARGS__) +#define LOG_ERROR(...) GENERIC_LOG(Log::MessageLevel::Error, __VA_ARGS__) + +namespace Log { +enum class MessageLevel { + Debug, + Info, + Warning, + Error, +}; + +struct Message { + MessageLevel level; + std::chrono::time_point time; + std::source_location srcLoc; + std::string text; +}; + +/// A mRing buffer of log messages for programmatic inspection at runtime. +struct MessageBuffer { + RingBuffer messages; +}; + +/// Unique ID identifying a currently registered MessageBuffer. +using MessageBufferId = int; + +MessageBufferId RegisterBuffer(MessageBuffer& buffer); +void UnregisterBuffer(MessageBufferId id); +MessageBuffer* GetBuffer(MessageBufferId id); +void DumpRegisteredBuffers(); + +extern bool gPrintToStdOut; +#if BRUSSEL_DEV_ENV +// NOTE: initialized in main.cpp +extern MessageBuffer gDefaultBuffer; +extern MessageBufferId gDefaultBufferId; +#endif + +// TODO improve this interface: don't copy std::string when there is in fact no MessageBuffer registered +void Add(const Message& msg); +} // namespace Log -- cgit v1.2.3-70-g09d2