22#include <inja/inja.hpp>
23#include <spdlog/sinks/stdout_color_sinks.h>
24#include <spdlog/spdlog.h>
31 (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
33#define FILENAME_ __FILE__
36#define LOG_ERROR(fmt__, ...) \
37 ::clanguml::logging::log_impl( \
38 spdlog::level::err, fmt__, FILENAME_, __LINE__, ##__VA_ARGS__)
40#define LOG_WARN(fmt__, ...) \
41 ::clanguml::logging::log_impl( \
42 spdlog::level::warn, fmt__, FILENAME_, __LINE__, ##__VA_ARGS__)
44#define LOG_INFO(fmt__, ...) \
45 ::clanguml::logging::log_impl( \
46 spdlog::level::info, fmt__, FILENAME_, __LINE__, ##__VA_ARGS__)
48#define LOG_DBG(fmt__, ...) \
49 ::clanguml::logging::log_impl( \
50 spdlog::level::debug, fmt__, FILENAME_, __LINE__, ##__VA_ARGS__)
52#define LOG_TRACE(fmt__, ...) \
53 ::clanguml::logging::log_impl( \
54 spdlog::level::trace, fmt__, FILENAME_, __LINE__, ##__VA_ARGS__)
57template <>
struct formatter<inja::json> : formatter<std::string> {
59 format_context &ctx)
const ->
decltype(ctx.out())
61 return fmt::format_to(ctx.out(),
"{}", json.dump());
68enum class logger_type_t {
74logger_type_t
logger_type(logger_type_t type = logger_type_t::get);
78std::string
to_string(spdlog::level::level_enum level);
82template <
typename T>
decltype(
auto) escape_json(T &&val)
84 using DecayedT = std::decay_t<T>;
85 if constexpr (std::is_same_v<DecayedT, inja::json>) {
86 std::string result{val.dump()};
89 else if constexpr (std::is_convertible_v<DecayedT, std::string>) {
90 std::string result{val};
95 return std::forward<T>(val);
98template <
typename FilenameT,
typename LineT,
typename... Args>
99void log_impl(spdlog::level::level_enum level, logger_type_t type,
100 std::string_view fmt_, FilenameT f, LineT l, Args &&...args)
102 if (type == logger_type_t::text) {
103 spdlog::get(
"clanguml-logger")
104 ->log(level, fmt::runtime(
"[{}:{}] " + std::string{fmt_}), f, l,
105 std::forward<Args>(args)...);
108 spdlog::get(
"clanguml-logger")
110 fmt::runtime(R
"("file": "{}", "line": {}, "message": ")" +
111 std::string{fmt_} + "\""),
112 f, l, escape_json(std::forward<Args>(args))...);
116template <
typename FilenameT,
typename LineT,
typename... Args>
117void log_impl(spdlog::level::level_enum level, std::string_view fmt_,
118 FilenameT f, LineT t, Args &&...args)
120 log_impl(level,
logger_type(), fmt_, f, t, std::forward<Args>(args)...);