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