aboutsummaryrefslogtreecommitdiff
path: root/source/10-common/Log.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-11-25 17:06:58 -0800
committerrtk0c <[email protected]>2022-11-25 17:06:58 -0800
commita0ddfdbcbc6336685362343518770f7bdefd10fe (patch)
treeeeee6982bca066b2286ef266c51fdc8ffaf8f9e0 /source/10-common/Log.hpp
parent01c98830affd1189ab2b227d685f10f5dc8b6ca7 (diff)
Changeset: 91 Basic logging infrastructure
Diffstat (limited to 'source/10-common/Log.hpp')
-rw-r--r--source/10-common/Log.hpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/source/10-common/Log.hpp b/source/10-common/Log.hpp
new file mode 100644
index 0000000..aeba984
--- /dev/null
+++ b/source/10-common/Log.hpp
@@ -0,0 +1,55 @@
+#pragma once
+
+#include "RingBuffer.hpp"
+
+#include <fmt/format.h>
+#include <chrono>
+#include <source_location>
+#include <string_view>
+
+// 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<std::chrono::system_clock> time;
+ std::source_location srcLoc;
+ std::string text;
+};
+
+/// A mRing buffer of log messages for programmatic inspection at runtime.
+struct MessageBuffer {
+ RingBuffer<Message> 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