diff --git a/CMakeLists.txt b/CMakeLists.txt index fe81fa60..62a8f4c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ endif () # Compiler config # --------------------------------------------------------------------------------------- if (NOT CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 14) + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) endif() diff --git a/example/example.cpp b/example/example.cpp index 0d08e8a9..cf35667e 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -26,6 +26,18 @@ void custom_flags_example(); int main(int, char *[]) { + spdlog::set_level(spdlog::level::debug); + + spdlog_trace(">>>TRACE"); + spdlog_logger_trace(spdlog::default_logger_raw(), ">>>TRACE"); + + spdlog_debug(">>>DEBUG"); + spdlog_logger_debug(spdlog::default_logger_raw(), ">>>DEBUG"); + + spdlog_critical(">>>CRITICAL"); + spdlog_logger_critical(spdlog::default_logger_raw(), ">>>CRITICAL"); + + // Log levels can be loaded from argv/env using "SPDLOG_LEVEL" load_levels_example(); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index b104c7cb..336843d5 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -183,8 +184,15 @@ SPDLOG_API void throw_spdlog_ex(std::string msg); struct source_loc { + using srcloc = std::experimental::fundamentals_v2::source_location; + constexpr source_loc() = default; - constexpr source_loc(const char *filename_in, int line_in, const char *funcname_in) + constexpr source_loc(const srcloc& srcloc) + : filename{srcloc.file_name()} + , line{srcloc.line()} + , funcname{srcloc.function_name()} + {} + constexpr source_loc(const char *filename_in, unsigned line_in, const char *funcname_in) : filename{filename_in} , line{line_in} , funcname{funcname_in} @@ -195,7 +203,7 @@ struct source_loc return line == 0; } const char *filename{nullptr}; - int line{0}; + unsigned line{0}; const char *funcname{nullptr}; }; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 831212f9..22c02869 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -18,6 +18,7 @@ #include #include #include +#include #include namespace spdlog { @@ -273,4 +274,173 @@ inline void critical(const T &msg) #define SPDLOG_CRITICAL(...) (void)0 #endif +#ifdef SPDLOG_HEADER_ONLY +#include "spdlog-inl.h" +#endif + +using srcloc = std::experimental::fundamentals_v2::source_location; + +template +constexpr void spdlog_logger_call(const srcloc& loc, spdlog::logger* logger, spdlog::level::level_enum level, Args... args) +{ + logger->log(spdlog::source_loc{loc}, level, args...); +} + +template +struct spdlog_trace +{ + constexpr spdlog_trace(Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE) + spdlog_logger_call(loc, spdlog::default_logger_raw(), spdlog::level::trace, args...); + } +}; + +template +struct spdlog_logger_trace +{ + constexpr spdlog_logger_trace(spdlog::logger* logger, Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE) + spdlog_logger_call(loc, logger, spdlog::level::trace, args...); + } +}; + +template +struct spdlog_debug +{ + constexpr spdlog_debug(Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG) + spdlog_logger_call(loc, spdlog::default_logger_raw(), spdlog::level::debug, args...); + } +}; + +template +struct spdlog_logger_debug +{ + constexpr spdlog_logger_debug(spdlog::logger* logger, Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG) + spdlog_logger_call(loc, logger, spdlog::level::debug, args...); + } +}; + +template +struct spdlog_info +{ + constexpr spdlog_info(Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO) + spdlog_logger_call(loc, spdlog::default_logger_raw(), spdlog::level::info, args...); + } +}; + +template +struct spdlog_logger_info +{ + constexpr spdlog_logger_info(spdlog::logger* logger, Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO) + spdlog_logger_call(loc, logger, spdlog::level::info, args...); + } +}; + +template +struct spdlog_warn +{ + constexpr spdlog_warn(Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN) + spdlog_logger_call(loc, spdlog::default_logger_raw(), spdlog::level::warn, args...); + } +}; + +template +struct spdlog_logger_warn +{ + constexpr spdlog_logger_warn(spdlog::logger* logger, Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN) + spdlog_logger_call(loc, logger, spdlog::level::warn, args...); + } +}; + +template +struct spdlog_error +{ + constexpr spdlog_error(Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR) + spdlog_logger_call(loc, spdlog::default_logger_raw(), spdlog::level::err, args...); + } +}; + +template +struct spdlog_logger_error +{ + constexpr spdlog_logger_error(spdlog::logger* logger, Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR) + spdlog_logger_call(loc, logger, spdlog::level::err, args...); + } +}; + +template +struct spdlog_critical +{ + constexpr spdlog_critical(Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL) + spdlog_logger_call(loc, spdlog::default_logger_raw(), spdlog::level::critical, args...); + } +}; + +template +struct spdlog_logger_critical +{ + constexpr spdlog_logger_critical(spdlog::logger* logger, Args&&... args, const srcloc& loc = srcloc::current()) + { + if constexpr (SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL) + spdlog_logger_call(loc, logger, spdlog::level::critical, args...); + } +}; + +template +spdlog_trace(Args&&...) -> spdlog_trace; + +template +spdlog_debug(Args&&...) -> spdlog_debug; + +template +spdlog_info(Args&&...) -> spdlog_info; + +template +spdlog_warn(Args&&...) -> spdlog_warn; + +template +spdlog_error(Args&&...) -> spdlog_error; + +template +spdlog_critical(Args&&...) -> spdlog_critical; + +template +spdlog_logger_trace(spdlog::logger*, Args&&...) -> spdlog_logger_trace; + +template +spdlog_logger_debug(spdlog::logger*, Args&&...) -> spdlog_logger_debug; + +template +spdlog_logger_info(spdlog::logger*, Args&&...) -> spdlog_logger_info; + +template +spdlog_logger_warn(spdlog::logger*, Args&&...) -> spdlog_logger_warn; + +template +spdlog_logger_error(spdlog::logger*, Args&&...) -> spdlog_logger_error; + +template +spdlog_logger_critical(spdlog::logger*, Args&&...) -> spdlog_logger_critical; + + #endif // SPDLOG_H