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 <memory>
7#include <string>
8#include <utility>
9#include <vector>
10
11#include <SD/export.hpp>
12#include <spdlog/common.h>
13#include <spdlog/spdlog.h>
14
15#define ENGINE_LOG_LEVEL_TRACE 0
16#define ENGINE_LOG_LEVEL_DEBUG 1
17#define ENGINE_LOG_LEVEL_INFO 2
18#define ENGINE_LOG_LEVEL_WARN 3
19#define ENGINE_LOG_LEVEL_ERROR 4
20#define ENGINE_LOG_LEVEL_CRITICAL 5
21#define ENGINE_LOG_LEVEL_OFF 6
22
23#ifndef ENGINE_LOG_LEVEL
24#define ENGINE_LOG_LEVEL ENGINE_LOG_LEVEL_INFO
25#endif
26
27#ifndef ENGINE_LOG_LEVEL_ENGINE
28#define ENGINE_LOG_LEVEL_ENGINE ENGINE_LOG_LEVEL
29#endif
30
31#ifndef ENGINE_LOG_LEVEL_GAME
32#define ENGINE_LOG_LEVEL_GAME ENGINE_LOG_LEVEL
33#endif
34
35namespace sd::log {
36
46
47struct LogEntry {
48 std::string category;
50 std::string message;
51 float uptime_sec{};
52};
53
55 std::string name;
56 bool visible = true;
58};
59
60SD_EXPORT std::deque<LogEntry> get_log_history();
61SD_EXPORT void add_log_entry(LogEntry entry);
62
63SD_EXPORT std::vector<CategoryInfo>& get_category_registry();
64SD_EXPORT void register_category(const char* name, ImVec4 color);
65
66bool is_category_under(const std::string& child, const std::string& parent);
67
71SD_EXPORT void init();
72
73spdlog::level::level_enum to_spdlog_level(LogLevel level);
74
75LogLevel from_spdlog_level(spdlog::level::level_enum level);
76
77inline std::shared_ptr<spdlog::logger> get_category_logger_or_report(const char* categoryPath) {
78 if (auto logger = spdlog::get(categoryPath)) {
79 return logger;
80 }
81
82 spdlog::error("Log category '{}' is not registered. Message was not logged.", categoryPath);
83 return nullptr;
84}
85
86} // namespace sd::log
87
88
89#define SD_LOG_CATEGORY_IMPL(CategoryPath, Level) \
90 constexpr auto cMinLevel = static_cast<::sd::log::LogLevel>(Level); \
91 constexpr const char* cCategoryPath = CategoryPath; \
92 \
93 template<typename... Args> \
94 inline void trace(spdlog::format_string_t<Args...> fmt, Args&&... args) { \
95 if constexpr (cMinLevel <= ::sd::log::LogLevel::TRACE) { \
96 if (auto logger = ::sd::log::get_category_logger_or_report(CategoryPath)) { \
97 logger->trace(fmt, std::forward<Args>(args)...); \
98 } \
99 } \
100 } \
101 \
102 template<typename... Args> \
103 inline void debug(spdlog::format_string_t<Args...> fmt, Args&&... args) { \
104 if constexpr (cMinLevel <= ::sd::log::LogLevel::DEBUG) { \
105 if (auto logger = ::sd::log::get_category_logger_or_report(CategoryPath)) { \
106 logger->debug(fmt, std::forward<Args>(args)...); \
107 } \
108 } \
109 } \
110 \
111 template<typename... Args> \
112 inline void info(spdlog::format_string_t<Args...> fmt, Args&&... args) { \
113 if constexpr (cMinLevel <= ::sd::log::LogLevel::INFO) { \
114 if (auto logger = ::sd::log::get_category_logger_or_report(CategoryPath)) { \
115 logger->info(fmt, std::forward<Args>(args)...); \
116 } \
117 } \
118 } \
119 \
120 template<typename... Args> \
121 inline void warn(spdlog::format_string_t<Args...> fmt, Args&&... args) { \
122 if constexpr (cMinLevel <= ::sd::log::LogLevel::WARN) { \
123 if (auto logger = ::sd::log::get_category_logger_or_report(CategoryPath)) { \
124 logger->warn(fmt, std::forward<Args>(args)...); \
125 } \
126 } \
127 } \
128 \
129 template<typename... Args> \
130 inline void error(spdlog::format_string_t<Args...> fmt, Args&&... args) { \
131 if constexpr (cMinLevel <= ::sd::log::LogLevel::ERROR) { \
132 if (auto logger = ::sd::log::get_category_logger_or_report(CategoryPath)) { \
133 logger->error(fmt, std::forward<Args>(args)...); \
134 } \
135 } \
136 } \
137 \
138 template<typename... Args> \
139 inline void critical(spdlog::format_string_t<Args...> fmt, Args&&... args) { \
140 if constexpr (cMinLevel <= ::sd::log::LogLevel::CRITICAL) { \
141 if (auto logger = ::sd::log::get_category_logger_or_report(CategoryPath)) { \
142 logger->critical(fmt, std::forward<Args>(args)...); \
143 } \
144 } \
145 }
146
147
150
151namespace renderer {
153} // namespace renderer
154
155namespace ecs {
157} // namespace ecs
158
159namespace network {
161} // namespace network
162} // namespace sd::log::engine
#define ENGINE_LOG_LEVEL_TRACE
Definition logging.hpp:15
#define ENGINE_LOG_LEVEL_WARN
Definition logging.hpp:18
#define ENGINE_LOG_LEVEL_INFO
Definition logging.hpp:17
#define ENGINE_LOG_LEVEL_OFF
Definition logging.hpp:21
#define SD_LOG_CATEGORY_IMPL(CategoryPath, Level)
Definition logging.hpp:89
#define ENGINE_LOG_LEVEL_CRITICAL
Definition logging.hpp:20
#define ENGINE_LOG_LEVEL_DEBUG
Definition logging.hpp:16
#define ENGINE_LOG_LEVEL_ENGINE
Definition logging.hpp:28
#define ENGINE_LOG_LEVEL_ERROR
Definition logging.hpp:19
Definition logging.hpp:148
Definition logging.hpp:35
LogLevel
Definition logging.hpp:37
SD_EXPORT void add_log_entry(LogEntry entry)
Definition logging.cpp:23
LogLevel from_spdlog_level(spdlog::level::level_enum level)
Definition logging.cpp:52
bool is_category_under(const std::string &child, const std::string &parent)
Definition logging.cpp:131
SD_EXPORT std::deque< LogEntry > get_log_history()
Definition logging.cpp:18
spdlog::level::level_enum to_spdlog_level(LogLevel level)
Definition logging.cpp:39
SD_EXPORT void register_category(const char *name, ImVec4 color)
Definition logging.cpp:120
SD_EXPORT void init()
Initializes all category loggers, shared sinks, and the ImGui sink.
Definition logging.cpp:138
SD_EXPORT std::vector< CategoryInfo > & get_category_registry()
Definition logging.cpp:31
std::shared_ptr< spdlog::logger > get_category_logger_or_report(const char *categoryPath)
Definition logging.hpp:77
Definition logging.hpp:54
std::string name
Definition logging.hpp:55
bool visible
Definition logging.hpp:56
ImVec4 color
Definition logging.hpp:57
Definition logging.hpp:47
std::string message
Definition logging.hpp:50
LogLevel level
Definition logging.hpp:49
float uptime_sec
Definition logging.hpp:51
std::string category
Definition logging.hpp:48
constexpr T g_type_max
Definition types.hpp:21