Added support also for spdlog::log functions.

Added overloading for std::source_location in logger class.
pull/2667/head
Gianluca Martino 3 years ago
parent e950911a4a
commit aa6f732844

@ -27,6 +27,13 @@
#include <vector>
#ifdef __has_include
# if __has_include(<source_location>) && __cplusplus >= 202002L
# include <source_location>
# define SPDLOG_SOURCE_LOCATION
# endif
#endif
#ifndef SPDLOG_NO_EXCEPTIONS
# define SPDLOG_LOGGER_CATCH(location) \
catch (const std::exception &ex) \
@ -90,6 +97,14 @@ public:
log_(loc, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
}
#ifdef SPDLOG_SOURCE_LOCATION
template<typename... Args>
void log(std::source_location loc, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{
log_(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
}
#endif
template<typename... Args>
void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{
@ -109,6 +124,14 @@ public:
log(loc, lvl, "{}", msg);
}
#ifdef SPDLOG_SOURCE_LOCATION
template<typename T>
void log(std::source_location loc, level::level_enum lvl, const T &msg)
{
log(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, msg);
}
#endif
void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg)
{
bool log_enabled = should_log(lvl);
@ -183,6 +206,14 @@ public:
log_(loc, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
}
# ifdef SPDLOG_SOURCE_LOCATION
template<typename... Args>
void log(std::source_location loc, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
{
log_(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
}
# endif
template<typename... Args>
void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
{

@ -20,13 +20,6 @@
#include <memory>
#include <string>
#ifdef __has_include
# if __has_include(<source_location>) && __cplusplus >= 202002L
# include <source_location>
# define SPDLOG_SOURCE_LOCATION
# endif
#endif
namespace spdlog {
using default_factory = synchronous_factory;
@ -147,6 +140,7 @@ SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logge
// spdlog::apply_logger_env_levels(mylogger);
SPDLOG_API void apply_logger_env_levels(std::shared_ptr<logger> logger);
#ifndef SPDLOG_SOURCE_LOCATION
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{
@ -159,7 +153,6 @@ inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
#ifndef SPDLOG_SOURCE_LOCATION
template<typename... Args>
inline void trace(format_string_t<Args...> fmt, Args &&... args)
{
@ -196,6 +189,18 @@ inline void critical(format_string_t<Args...> fmt, Args &&... args)
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
}
template<typename T>
inline void log(source_loc source, level::level_enum lvl, const T &msg)
{
default_logger_raw()->log(source, lvl, msg);
}
template<typename T>
inline void log(level::level_enum lvl, const T &msg)
{
default_logger_raw()->log(lvl, msg);
}
template<typename T>
inline void trace(const T &msg)
{
@ -231,21 +236,7 @@ inline void critical(const T &msg)
{
default_logger_raw()->critical(msg);
}
#endif
template<typename T>
inline void log(source_loc source, level::level_enum lvl, const T &msg)
{
default_logger_raw()->log(source, lvl, msg);
}
template<typename T>
inline void log(level::level_enum lvl, const T &msg)
{
default_logger_raw()->log(lvl, msg);
}
#ifndef SPDLOG_SOURCE_LOCATION
# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
@ -298,6 +289,27 @@ inline void critical(wformat_string_t<Args...> fmt, Args &&... args)
#endif
#ifdef SPDLOG_SOURCE_LOCATION
template<typename... Args>
struct log
{
log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args, const std::source_location &loc = std::source_location::current())
{
default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, fmt, std::forward<Args>(args)...);
}
log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
}
# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args, const std::source_location &loc = std::source_location::current())
{
default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, fmt, std::forward<Args>(args)...);
}
# endif
};
template<typename... Args>
struct trace
{
@ -394,6 +406,9 @@ struct critical
# endif
};
template <typename... Args>
log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args) -> log<Args...>;
template <typename... Args>
trace(format_string_t<Args...> fmt, Args &&... args) -> trace<Args...>;
@ -413,6 +428,9 @@ template <typename... Args>
critical(format_string_t<Args...> fmt, Args &&... args) -> critical<Args...>;
# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template <typename... Args>
log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args) -> log<Args...>;
template <typename... Args>
trace(wformat_string_t<Args...> fmt, Args &&... args) -> trace<Args...>;
@ -432,6 +450,25 @@ template <typename... Args>
critical(wformat_string_t<Args...> fmt, Args &&... args) -> critical<Args...>;
# endif
template<typename T>
struct log<T>
{
log(source_loc source, level::level_enum lvl, const T &msg)
{
default_logger_raw()->log(source, lvl, msg);
}
log(level::level_enum lvl, const T &msg, const std::source_location &loc = std::source_location::current())
{
default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, msg);
}
log(level::level_enum lvl, format_string_t<T> fmt, T&& arg, const std::source_location &loc = std::source_location::current())
{
default_logger_raw()->log(spdlog::source_loc{loc.file_name(), static_cast<int>(loc.line()), loc.function_name()}, lvl, fmt, std::forward<T>(arg));
}
};
template<typename T>
struct trace<T>
{
@ -516,6 +553,9 @@ struct critical<T>
}
};
template <typename T>
log(level::level_enum lvl, const T &msg) -> log<T>;
template <typename T>
trace(const T &msg) -> trace<T>;

Loading…
Cancel
Save