#pragma once #include "Format.h" #include "spdlog/spdlog.h" namespace clice::logging { using Level = spdlog::level::level_enum; using ColorMode = spdlog::color_mode; struct Options { Level level = Level::info; ColorMode color = ColorMode::automatic; }; extern Options options; void create_stderr_logger(std::string_view name, const Options& options); void create_file_loggger(std::string_view name, std::string_view dir, const Options& options); template struct logging_rformat { template StrLike> consteval logging_rformat(const StrLike& str, std::source_location location = std::source_location::current()) : str(str), location(location) {} std::format_string str; std::source_location location; }; template using logging_format = logging_rformat...>; template void log(spdlog::level::level_enum level, std::source_location location, std::format_string fmt, Args&&... args) { spdlog::source_loc loc{ location.file_name(), static_cast(location.line()), location.function_name(), }; using spdlog_fmt = spdlog::format_string_t; if constexpr(std::same_as) { spdlog::log(loc, level, fmt.get(), std::forward(args)...); } else { spdlog::log(loc, level, fmt, std::forward(args)...); } } template void trace(logging_format fmt, Args&&... args) { logging::log(spdlog::level::trace, fmt.location, fmt.str, std::forward(args)...); } template void debug(logging_format fmt, Args&&... args) { logging::log(spdlog::level::debug, fmt.location, fmt.str, std::forward(args)...); } template void info(logging_format fmt, Args&&... args) { logging::log(spdlog::level::info, fmt.location, fmt.str, std::forward(args)...); } template void warn(logging_format fmt, Args&&... args) { logging::log(spdlog::level::warn, fmt.location, fmt.str, std::forward(args)...); } template void fatal [[noreturn]] (logging_format fmt, Args&&... args) { logging::log(spdlog::level::err, fmt.location, fmt.str, std::forward(args)...); spdlog::shutdown(); std::exit(1); } } // namespace clice::logging