Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #if DEBUG
- # define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
- #else
- # define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
- #endif
- #ifdef _MSC_VER
- # define SPDLOG_FUNCTION __FUNCTION__
- #else
- # define SPDLOG_FUNCTION __PRETTY_FUNCTION__
- #endif
- #include <source_location>
- #include <vkp/constants.h>
- #include <spdlog/logger.h>
- using loglevel = spdlog::level::level_enum;
- namespace logger
- {
- class Logger
- {
- public:
- static auto get() -> auto const& { return m_logger; }
- static void log(std::string_view msg,
- spdlog::level::level_enum level,
- std::source_location loc)
- {
- m_logger->log(
- spdlog::source_loc { loc.file_name(),
- static_cast<int>(loc.line()),
- loc.function_name() },
- level,
- msg);
- }
- static void log(std::string_view msg,
- spdlog::level::level_enum level)
- {
- m_logger->log(level, msg);
- }
- static void setPattern(char const* pattern)
- {
- m_logger->set_pattern(pattern);
- }
- static constexpr auto noLocationPattern {
- "%^%n: [%=8l] [%r]\n-> %v%$"
- };
- static constexpr auto locationPattern {
- "%^%n: [%=8l] [%r] (%@ %!)\n-> %v%$"
- };
- private:
- static auto create() -> std::shared_ptr<spdlog::logger>;
- inline static const std::shared_ptr<spdlog::logger> m_logger {
- create()
- };
- };
- template<loglevel lvl>
- class LogEntry
- {
- public:
- std::string message;
- LogEntry(std::string message) : message(std::move(message)) {}
- void at(std::source_location loc) const
- {
- Logger::log(message, lvl, loc);
- }
- void here(std::source_location here_location =
- std::source_location::current()) const
- {
- at(here_location);
- }
- void withoutLoc() const { Logger::log(message, lvl); }
- };
- template<>
- class LogEntry<loglevel::off>
- {
- public:
- constexpr LogEntry() {}
- constexpr void at(std::source_location /*unused*/) {}
- constexpr void here() {}
- constexpr void withoutLoc() {}
- };
- template<loglevel lvl, typename... Args>
- [[nodiscard]] inline auto
- log([[maybe_unused]] std::string_view msg,
- [[maybe_unused]] Args&&... args)
- -> std::conditional_t<(lvl < loglevel::err) && !kDebug,
- LogEntry<loglevel::off>,
- LogEntry<lvl>>
- {
- if constexpr (!kDebug && (lvl < loglevel::err))
- {
- return {};
- }
- else
- {
- return { fmt::vformat(
- msg,
- fmt::make_format_args(std::forward<Args>(args)...)) };
- }
- }
- } // namespace logger
Advertisement
Add Comment
Please, Sign In to add comment