Minecraft Community Edition 0.1.0
Loading...
Searching...
No Matches
Logger.hpp
1#pragma once
2
3#include <sstream>
4#include <string>
5#include <format>
6#include <source_location>
7
8#include "EASTL/shared_ptr.h"
9#include "EASTL/vector.h"
10
11#include "Core/QEventBus.hpp"
12#include "IEvent.hpp"
13
14#define MCE_LOGGER_SET_QEVENTBUS 0
15
16#ifndef NDEBUG
17#define MCE_INFO(...) mce::io::Logger::getGlobalLogger(qBus).info(__VA_ARGS__)
18#define MCE_WARN(...) mce::io::Logger::getGlobalLogger(qBus).warn(__VA_ARGS__)
19#define MCE_ERROR(...) mce::io::Logger::getGlobalLogger(qBus).error(__VA_ARGS__)
20#define MCE_DEBUG(...) mce::io::Logger::getGlobalLogger(qBus).debug(__VA_ARGS__)
21#define MCE_INFO_TRACE(...) mce::io::Logger::getGlobalLogger(qBus).info_trace(std::source_location::current(), __VA_ARGS__)
22#define MCE_WARN_TRACE(...) mce::io::Logger::getGlobalLogger(qBus).warn_trace(std::source_location::current(), __VA_ARGS__)
23#define MCE_ERROR_TRACE(...) mce::io::Logger::getGlobalLogger(qBus).error_trace(std::source_location::current(), __VA_ARGS__)
24#define MCE_DEBUG_TRACE(...) mce::io::Logger::getGlobalLogger(qBus).debug_trace(std::source_location::current(), __VA_ARGS__)
25#else
26#define MCE_INFO(...)
27#define MCE_WARN(...)
28#define MCE_ERROR(...)
29#define MCE_DEBUG(...)
30#define MCE_INFO_TRACE(...)
31#define MCE_WARN_TRACE(...)
32#define MCE_ERROR_TRACE(...)
33#define MCE_DEBUG_TRACE(...)
34#endif
35
36namespace mce::io {
37 class LoggerSink;
38 using core::QEventBus;
39 using core::SubscriptionToken;
40
41 enum class LogLevel {
42 INFO,
43 WARN,
44 ERROR,
45 DEBUG,
46 };
47
48 class Logger {
49 public:
50 explicit Logger(QEventBus& qBus, std::string_view name, bool createStdoutSink = true);
51
52 void addSink(const eastl::shared_ptr<LoggerSink>& sink);
53 void log(LogLevel level, std::string_view message, eastl::optional<std::source_location> location = eastl::nullopt);
54
55 template <typename... Args>
56 void info(std::format_string<Args...> format, Args&&... args) {
57 log(LogLevel::INFO, std::format(format, std::forward<Args>(args)...));
58 }
59
60 template <typename... Args>
61 void warn(std::format_string<Args...> format, Args&&... args) {
62 log(LogLevel::WARN, std::format(format, std::forward<Args>(args)...));
63 }
64
65 template <typename... Args>
66 void error(std::format_string<Args...> format, Args&&... args) {
67 log(LogLevel::ERROR, std::format(format, std::forward<Args>(args)...));
68 }
69
70 template <typename... Args>
71 void debug(std::format_string<Args...> format, Args&&... args) {
72 log(LogLevel::DEBUG, std::format(format, std::forward<Args>(args)...));
73 }
74
75 template <typename... Args>
76 void info_trace(const std::source_location& location, std::format_string<Args...> format, Args&&... args) {
77 log(LogLevel::INFO, std::format(format, std::forward<Args>(args)...), location);
78 }
79
80 template <typename... Args>
81 void warn_trace(const std::source_location& location, std::format_string<Args...> format, Args&&... args) {
82 log(LogLevel::WARN, std::format(format, std::forward<Args>(args)...), location);
83 }
84
85 template <typename... Args>
86 void error_trace(const std::source_location& location, std::format_string<Args...> format, Args&&... args) {
87 log(LogLevel::ERROR, std::format(format, std::forward<Args>(args)...), location);
88 }
89
90 template <typename... Args>
91 void debug_trace(const std::source_location& location, std::format_string<Args...> format, Args&&... args) {
92 log(LogLevel::DEBUG, std::format(format, std::forward<Args>(args)...), location);
93 }
94
95 //static QEventBus& getGlobalEventBus();
96 static Logger& getGlobalLogger(QEventBus& qBus);
97 private:
98 /*class LoggerEventBus : public QEventBus {
99 public:
100 LoggerEventBus();
101 };*/
102
103 void logCallback(const event::LoggerOutput& e);
104 static std::string getFormattedSource(const std::source_location& location);
105
106 static std::string GetFormattedTime();
107 const size_t MAX_LOG_EVENTS = 10000;
108 std::string name;
109
110 eastl::vector<eastl::shared_ptr<LoggerSink>> sinks;
111 QEventBus& qBus;
112
113 SubscriptionToken busSubscription;
114 };
115}
Thread-safe event bus.
Thread-safe event bus.
Definition QEventBus.hpp:58
Definition LoggerSinks.hpp:16
Token object that controls the lifetime of an event subscription.
Definition QEventBus.hpp:27
Definition IEvent.hpp:23