Guest User

Untitled

a guest
Sep 6th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | Source Code | 0 0
  1. #pragma once
  2.  
  3. #if DEBUG
  4. #    define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
  5. #else
  6. #    define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
  7. #endif
  8.  
  9. #ifdef _MSC_VER
  10. #    define SPDLOG_FUNCTION __FUNCTION__
  11. #else
  12. #    define SPDLOG_FUNCTION __PRETTY_FUNCTION__
  13. #endif
  14.  
  15. #include <source_location>
  16.  
  17. #include <vkp/constants.h>
  18.  
  19. #include <spdlog/logger.h>
  20.  
  21. using loglevel = spdlog::level::level_enum;
  22.  
  23. namespace logger
  24. {
  25.     class Logger
  26.     {
  27.     public:
  28.         static auto get() -> auto const& { return m_logger; }
  29.  
  30.         static void log(std::string_view msg,
  31.                         spdlog::level::level_enum level,
  32.                         std::source_location loc)
  33.         {
  34.             m_logger->log(
  35.                 spdlog::source_loc { loc.file_name(),
  36.                                      static_cast<int>(loc.line()),
  37.                                      loc.function_name() },
  38.                 level,
  39.                 msg);
  40.         }
  41.  
  42.         static void log(std::string_view msg,
  43.                         spdlog::level::level_enum level)
  44.         {
  45.             m_logger->log(level, msg);
  46.         }
  47.  
  48.         static void setPattern(char const* pattern)
  49.         {
  50.             m_logger->set_pattern(pattern);
  51.         }
  52.  
  53.         static constexpr auto noLocationPattern {
  54.             "%^%n: [%=8l] [%r]\n-> %v%$"
  55.         };
  56.         static constexpr auto locationPattern {
  57.             "%^%n: [%=8l] [%r] (%@ %!)\n-> %v%$"
  58.         };
  59.  
  60.     private:
  61.         static auto create() -> std::shared_ptr<spdlog::logger>;
  62.  
  63.         inline static const std::shared_ptr<spdlog::logger> m_logger {
  64.             create()
  65.         };
  66.     };
  67.  
  68.     template<loglevel lvl>
  69.     class LogEntry
  70.     {
  71.     public:
  72.         std::string message;
  73.  
  74.         LogEntry(std::string message) : message(std::move(message)) {}
  75.  
  76.         void at(std::source_location loc) const
  77.         {
  78.             Logger::log(message, lvl, loc);
  79.         }
  80.  
  81.         void here(std::source_location here_location =
  82.                       std::source_location::current()) const
  83.         {
  84.             at(here_location);
  85.         }
  86.  
  87.         void withoutLoc() const { Logger::log(message, lvl); }
  88.     };
  89.  
  90.     template<>
  91.     class LogEntry<loglevel::off>
  92.     {
  93.     public:
  94.         constexpr LogEntry() {}
  95.  
  96.         constexpr void at(std::source_location /*unused*/) {}
  97.  
  98.         constexpr void here() {}
  99.  
  100.         constexpr void withoutLoc() {}
  101.     };
  102.  
  103.     template<loglevel lvl, typename... Args>
  104.     [[nodiscard]] inline auto
  105.     log([[maybe_unused]] std::string_view msg,
  106.         [[maybe_unused]] Args&&... args)
  107.         -> std::conditional_t<(lvl < loglevel::err) && !kDebug,
  108.                               LogEntry<loglevel::off>,
  109.                               LogEntry<lvl>>
  110.     {
  111.         if constexpr (!kDebug && (lvl < loglevel::err))
  112.         {
  113.             return {};
  114.         }
  115.         else
  116.         {
  117.             return { fmt::vformat(
  118.                 msg,
  119.                 fmt::make_format_args(std::forward<Args>(args)...)) };
  120.         }
  121.     }
  122. }  // namespace logger
  123.  
Advertisement
Add Comment
Please, Sign In to add comment