SDEngine
Game Engine
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <deque>
5#include <imgui.h>
6#include <string>
7#include <utility>
8#include <vector>
9
10#include <SD/export.hpp>
11#include <fmt/format.h>
12#include <quill/Backend.h>
13#include <quill/LogFunctions.h>
14#include <quill/core/LogLevel.h>
15
16#define ENGINE_LOG_LEVEL_TRACE 0
17#define ENGINE_LOG_LEVEL_DEBUG 1
18#define ENGINE_LOG_LEVEL_INFO 2
19#define ENGINE_LOG_LEVEL_WARN 3
20#define ENGINE_LOG_LEVEL_ERROR 4
21#define ENGINE_LOG_LEVEL_CRITICAL 5
22#define ENGINE_LOG_LEVEL_OFF 6
23
24#ifndef ENGINE_LOG_LEVEL
25#define ENGINE_LOG_LEVEL ENGINE_LOG_LEVEL_INFO
26#endif
27
28#ifndef ENGINE_LOG_LEVEL_ENGINE
29#define ENGINE_LOG_LEVEL_ENGINE ENGINE_LOG_LEVEL
30#endif
31
32#ifndef ENGINE_LOG_LEVEL_GAME
33#define ENGINE_LOG_LEVEL_GAME ENGINE_LOG_LEVEL
34#endif
35
36namespace sd::log {
37
48
49struct LogEntry {
50 std::string category;
52 std::string message;
53 float uptime_sec{};
54};
55
57 std::string name;
58 bool visible = true;
60};
61
62SD_EXPORT std::deque<LogEntry> get_log_history();
63SD_EXPORT void add_log_entry(LogEntry entry);
66SD_EXPORT std::vector<LogEntry> get_entries(size_t offset, size_t count);
67
68SD_EXPORT std::vector<CategoryInfo>& get_category_registry();
69SD_EXPORT void register_category(const char* name, ImVec4 color);
70
71bool is_category_under(const std::string& child, const std::string& parent);
72
73SD_EXPORT void init();
74
75quill::LogLevel to_quill_level(LogLevel level);
76
77LogLevel from_quill_level(quill::LogLevel level);
78
79quill::Logger* get_category_logger_or_report(const char* categoryPath);
80
81} // namespace sd::log
82
83#define SD_LOG_CATEGORY_IMPL(CategoryPath, Level) \
84 constexpr auto cMinLevel = static_cast<::sd::log::LogLevel>(Level); \
85 constexpr const char* cCategoryPath = CategoryPath; \
86 \
87 inline ::quill::Logger* get_logger() { \
88 static ::quill::Logger* logger = ::sd::log::get_category_logger_or_report(CategoryPath); \
89 return logger; \
90 } \
91 \
92 template<typename... Args> \
93 inline void trace(fmt::format_string<Args...> fmt, Args&&... args) { \
94 if constexpr (cMinLevel <= ::sd::log::LogLevel::TRACE) { \
95 if (auto* logger = get_logger()) { \
96 ::quill::tracel3(logger, fmt.get().data(), std::forward<Args>(args)...); \
97 } \
98 } \
99 } \
100 \
101 template<typename... Args> \
102 inline void debug(fmt::format_string<Args...> fmt, Args&&... args) { \
103 if constexpr (cMinLevel <= ::sd::log::LogLevel::DEBUG) { \
104 if (auto* logger = get_logger()) { \
105 ::quill::debug(logger, fmt.get().data(), std::forward<Args>(args)...); \
106 } \
107 } \
108 } \
109 \
110 template<typename... Args> \
111 inline void info(fmt::format_string<Args...> fmt, Args&&... args) { \
112 if constexpr (cMinLevel <= ::sd::log::LogLevel::INFO) { \
113 if (auto* logger = get_logger()) { \
114 ::quill::info(logger, fmt.get().data(), std::forward<Args>(args)...); \
115 } \
116 } \
117 } \
118 \
119 template<typename... Args> \
120 inline void warn(fmt::format_string<Args...> fmt, Args&&... args) { \
121 if constexpr (cMinLevel <= ::sd::log::LogLevel::WARN) { \
122 if (auto* logger = get_logger()) { \
123 ::quill::warning(logger, fmt.get().data(), std::forward<Args>(args)...); \
124 } \
125 } \
126 } \
127 \
128 template<typename... Args> \
129 inline void error(fmt::format_string<Args...> fmt, Args&&... args) { \
130 if (auto* logger = get_logger()) { \
131 ::quill::error(logger, fmt.get().data(), std::forward<Args>(args)...); \
132 } \
133 } \
134 \
135 template<typename... Args> \
136 [[noreturn]] inline void critical(fmt::format_string<Args...> fmt, Args&&... args) { \
137 if (auto* logger = get_logger()) { \
138 ::quill::critical(logger, fmt.get().data(), std::forward<Args>(args)...); \
139 logger->flush_log(); \
140 } \
141 ::quill::Backend::stop(); \
142 std::abort(); \
143 } \
144 \
145 template<typename... Args> \
146 inline void general(Args&&... args) { \
147 info(std::forward<Args>(args)...); \
148 } \
149 \
150 template<typename... Args> \
151 inline void tagged(std::string_view subcategory, \
152 fmt::format_string<Args...> fmt, \
153 Args&&... args) { \
154 if (auto* logger = get_logger()) { \
155 auto msg = \
156 fmt::format("[{}] {}", subcategory, fmt::format(fmt, std::forward<Args>(args)...)); \
157 ::quill::info(logger, msg.c_str()); \
158 } \
159 }
160
161
162namespace sd::log {
163namespace engine {
165
169namespace shader {
171}
172namespace renderer {
174} // namespace renderer
175
176namespace ecs {
178} // namespace ecs
179
180namespace network {
182} // namespace network
183} // namespace engine
184namespace vulkan {
186}
187namespace debug_layer {
189}
190} // namespace sd::log
#define ENGINE_LOG_LEVEL_TRACE
Definition logging.hpp:16
#define ENGINE_LOG_LEVEL_WARN
Definition logging.hpp:19
#define ENGINE_LOG_LEVEL_INFO
Definition logging.hpp:18
#define ENGINE_LOG_LEVEL_OFF
Definition logging.hpp:22
#define SD_LOG_CATEGORY_IMPL(CategoryPath, Level)
Definition logging.hpp:83
#define ENGINE_LOG_LEVEL_CRITICAL
Definition logging.hpp:21
#define ENGINE_LOG_LEVEL_DEBUG
Definition logging.hpp:17
#define ENGINE_LOG_LEVEL_ENGINE
Definition logging.hpp:29
#define ENGINE_LOG_LEVEL_ERROR
Definition logging.hpp:20
Namespace for Engine intrinsic things.
Definition logging.hpp:36
LogLevel
Definition logging.hpp:38
SD_EXPORT std::deque< LogEntry > get_log_history()
Definition logging.cpp:90
SD_EXPORT std::vector< LogEntry > get_entries(size_t offset, size_t count)
Definition logging.cpp:119
SD_EXPORT void add_log_entry(LogEntry entry)
Definition logging.cpp:95
SD_EXPORT std::vector< CategoryInfo > & get_category_registry()
Definition logging.cpp:148
bool is_category_under(const std::string &child, const std::string &parent)
Definition logging.cpp:428
SD_EXPORT void clear_history()
Definition logging.cpp:105
quill::LogLevel to_quill_level(LogLevel level)
Definition logging.cpp:152
SD_EXPORT void register_category(const char *name, ImVec4 color)
Definition logging.cpp:409
quill::Logger * get_category_logger_or_report(const char *categoryPath)
Definition logging.cpp:486
LogLevel from_quill_level(quill::LogLevel level)
Definition logging.cpp:173
SD_EXPORT size_t get_total_entry_count()
Definition logging.cpp:114
SD_EXPORT void init()
Definition logging.cpp:436
Definition logging.hpp:56
std::string name
Definition logging.hpp:57
bool visible
Definition logging.hpp:58
ImVec4 color
Definition logging.hpp:59
Definition logging.hpp:49
std::string message
Definition logging.hpp:52
LogLevel level
Definition logging.hpp:51
float uptime_sec
Definition logging.hpp:53
std::string category
Definition logging.hpp:50
consteval U64 type_id_of()
Definition type_id.hpp:6