30#ifndef LIBSBX_CORE_LOGGER_HPP_
31#define LIBSBX_CORE_LOGGER_HPP_
38#include <source_location>
39#include <unordered_map>
42#include <fmt/format.h>
44#include <spdlog/logger.h>
45#include <spdlog/sinks/stdout_color_sinks.h>
46#include <spdlog/sinks/basic_file_sink.h>
48#include <libsbx/utility/target.hpp>
56 template<
typename... Args>
57 using format_string_type = spdlog::format_string_t<Args...>;
63 template<
typename... Args>
64 static auto trace(format_string_type<Args...> format, Args&&... args) ->
void {
66 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
67 _instance().trace(format, std::forward<Args>(args)...);
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);
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)...);
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);
92 template<
typename... Args>
93 static auto info(format_string_type<Args...> format, Args&&... args) ->
void {
94 _instance().info(format, std::forward<Args>(args)...);
97 template<
typename Type>
98 static auto info(
const Type& value) ->
void {
99 _instance().info(value);
102 template<
typename... Args>
103 static auto warn(format_string_type<Args...> format, Args&&... args) ->
void {
104 _instance().warn(format, std::forward<Args>(args)...);
107 template<
typename Type>
108 static auto warn(
const Type& value) ->
void {
109 _instance().warn(value);
112 template<
typename... Args>
113 static auto error(format_string_type<Args...> format, Args&&... args) ->
void {
114 _instance().error(format, std::forward<Args>(args)...);
117 template<
typename Type>
118 static auto error(
const Type& value) ->
void {
119 _instance().error(value);
122 template<
typename... Args>
123 static auto critical(format_string_type<Args...> format, Args&&... args) ->
void {
124 _instance().critical(format, std::forward<Args>(args)...);
127 template<
typename Type>
128 static auto critical(
const Type& value) ->
void {
129 _instance().critical(value);
134 static auto _instance() -> spdlog::logger& {
135 static auto instance = _create_logger();
139 static auto _create_logger() -> spdlog::logger {
140 auto sinks = std::vector<std::shared_ptr<spdlog::sinks::sink>>{};
142 sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(
"./demo/logs/sbx.log",
true));
144 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
145 sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
148 auto logger = spdlog::logger{
"logger", std::begin(sinks), std::end(sinks)};
150 logger.set_pattern(
"[%Y-%m-%d %H:%M:%S] [%^%l%$] [%n] : %v");
152 if constexpr (utility::build_configuration_v == utility::build_configuration::debug) {
153 logger.set_level(spdlog::level::debug);
155 logger.set_level(spdlog::level::info);
166template<
typename Type>
167requires (std::is_enum_v<Type>)
168struct fmt::formatter<Type> :
public fmt::formatter<std::underlying_type_t<Type>> {
170 using base_type = fmt::formatter<std::underlying_type_t<Type>>;
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);
Definition: logger.hpp:52