sandbox
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Jonas Kabelitz
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 * You should have received a copy of the MIT License along with this program.
23 * If not, see <https://opensource.org/licenses/MIT/>.
24 */
25
30#ifndef LIBSBX_CORE_LOGGER_HPP_
31#define LIBSBX_CORE_LOGGER_HPP_
32
37#include <memory>
38#include <source_location>
39#include <unordered_map>
40#include <type_traits>
41
42#include <fmt/format.h>
43
44#include <spdlog/logger.h>
45#include <spdlog/sinks/stdout_color_sinks.h>
46#include <spdlog/sinks/basic_file_sink.h>
47
48#include <libsbx/utility/target.hpp>
49
50namespace sbx::core {
51
52class logger {
53
54public:
55
56 template<typename... Args>
57 using format_string_type = spdlog::format_string_t<Args...>;
58
59 logger() = delete;
60
61 ~logger() = default;
62
63 template<typename... Args>
64 static auto trace(format_string_type<Args...> format, Args&&... args) -> void {
65 // [NOTE] KAJ 2023-03-20 : This should make trace and debug messages be no-ops in release builds.
66 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
67 _instance().trace(format, std::forward<Args>(args)...);
68 }
69 }
70
71 template<typename Type>
72 static auto trace(const Type& value) -> void {
73 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
74 _instance().trace(value);
75 }
76 }
77
78 template<typename... Args>
79 static auto debug(format_string_type<Args...> format, Args&&... args) -> void {
80 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
81 _instance().debug(format, std::forward<Args>(args)...);
82 }
83 }
84
85 template<typename Type>
86 static auto debug(const Type& value) -> void {
87 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
88 _instance().debug(value);
89 }
90 }
91
92 template<typename... Args>
93 static auto info(format_string_type<Args...> format, Args&&... args) -> void {
94 _instance().info(format, std::forward<Args>(args)...);
95 }
96
97 template<typename Type>
98 static auto info(const Type& value) -> void {
99 _instance().info(value);
100 }
101
102 template<typename... Args>
103 static auto warn(format_string_type<Args...> format, Args&&... args) -> void {
104 _instance().warn(format, std::forward<Args>(args)...);
105 }
106
107 template<typename Type>
108 static auto warn(const Type& value) -> void {
109 _instance().warn(value);
110 }
111
112 template<typename... Args>
113 static auto error(format_string_type<Args...> format, Args&&... args) -> void {
114 _instance().error(format, std::forward<Args>(args)...);
115 }
116
117 template<typename Type>
118 static auto error(const Type& value) -> void {
119 _instance().error(value);
120 }
121
122 template<typename... Args>
123 static auto critical(format_string_type<Args...> format, Args&&... args) -> void {
124 _instance().critical(format, std::forward<Args>(args)...);
125 }
126
127 template<typename Type>
128 static auto critical(const Type& value) -> void {
129 _instance().critical(value);
130 }
131
132private:
133
134 static auto _instance() -> spdlog::logger& {
135 static auto instance = _create_logger();
136 return instance;
137 }
138
139 static auto _create_logger() -> spdlog::logger {
140 auto sinks = std::vector<std::shared_ptr<spdlog::sinks::sink>>{};
141
142 sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./demo/logs/sbx.log", true));
143
144 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
145 sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
146 }
147
148 auto logger = spdlog::logger{"logger", std::begin(sinks), std::end(sinks)};
149
150 logger.set_pattern("[%Y-%m-%d %H:%M:%S] [%^%l%$] [%n] : %v");
151
152 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
153 logger.set_level(spdlog::level::debug);
154 } else {
155 logger.set_level(spdlog::level::info);
156 }
157
158 return logger;
159 }
160
161}; // class logger
162
163} // namespace sbx::core
164
165// [NOTE] KAJ 2024-01-19 : Enable formatting to underlying type for all enums
166template<typename Type>
167requires (std::is_enum_v<Type>)
168struct fmt::formatter<Type> : public fmt::formatter<std::underlying_type_t<Type>> {
169
170 using base_type = fmt::formatter<std::underlying_type_t<Type>>;
171
172 template<typename FormatContext>
173 auto format(const Type& value, FormatContext& context) -> decltype(auto) {
174 return base_type::format(static_cast<std::underlying_type_t<Type>>(value), context);
175 }
176
177}; // struct fmt::formatter<Type>
178
179#endif // LIBSBX_CORE_LOGGER_HPP_
Definition: logger.hpp:52