Minor cleanups

This commit is contained in:
gabime 2023-09-15 21:07:09 +03:00
parent 501e3480ca
commit 1faeb5b6fa
6 changed files with 110 additions and 44 deletions

View File

@ -25,7 +25,6 @@ endif()
# ---------------------------------------------------------------------------------------
# Compiler config
# ---------------------------------------------------------------------------------------
# c++ standard >=17 is required
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)

View File

@ -27,6 +27,7 @@ void custom_flags_example();
void file_events_example();
void replace_default_logger_example();
#define SPDLOG_EMIT_SOURCE_LOCATION // define if you want to log the source file/line/function name.
#include "spdlog/spdlog.h"
#include "spdlog/cfg/env.h" // support for loading levels from the environment variable

View File

@ -135,9 +135,6 @@ using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
template<typename... Args>
using format_string_t = fmt::format_string<Args...>;
template<typename Char>
using fmt_runtime_string = fmt::runtime_format_string<Char>;
# if defined(SPDLOG_WCHAR_FILENAMES)
using wstring_view_t = fmt::basic_string_view<wchar_t>;
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
@ -287,12 +284,14 @@ struct source_loc
const char *funcname{nullptr};
};
// to capture caller's current source location and format string at compile time.
struct loc_with_fmt
{
source_loc loc;
string_view_t fmt_string;
template<typename S, typename = is_convertible_to_sv<S>>
constexpr loc_with_fmt(S fmt_str, source_loc loc = source_loc::current()) noexcept: loc(loc), fmt_string(fmt_str) {}
constexpr loc_with_fmt(S fmt_str, source_loc loc = source_loc::current()) noexcept:
loc(loc), fmt_string(fmt_str) {}
};
struct file_event_handlers

View File

@ -15,7 +15,7 @@
namespace spdlog {
// public methods
SPDLOG_INLINE logger::logger(const logger &other)
SPDLOG_INLINE logger::logger(const logger &other) noexcept
: name_(other.name_)
, sinks_(other.sinks_)
, level_(other.level_.load(std::memory_order_relaxed))

View File

@ -72,7 +72,7 @@ public:
virtual ~logger() = default;
logger(const logger &other);
logger(const logger &other) noexcept;
logger(logger &&other) noexcept;
logger &operator=(logger other) noexcept;
void swap(spdlog::logger &other) noexcept;
@ -134,6 +134,7 @@ public:
}
#ifdef SPDLOG_EMIT_SOURCE_LOCATION
template<typename... Args>
void trace(loc_with_fmt fmt, Args &&...args)
{

View File

@ -122,6 +122,8 @@ SPDLOG_API spdlog::logger *default_logger_raw();
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
// Initialize logger level based on environment configs.
//
// Useful for applying SPDLOG_LEVEL to manually created loggers.
@ -140,93 +142,157 @@ inline void log(source_loc source, level::level_enum lvl, format_string_t<Args..
template<typename... Args>
inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
default_logger_raw()->log(lvl, fmt, std::forward<Args>(args)...);
}
#ifdef SPDLOG_EMIT_SOURCE_LOCATION
template<typename... Args>
void trace(loc_with_fmt fmt, Args &&...args)
{
log(fmt.loc, level::trace, fmt.fmt_string, std::forward<Args>(args)...);
}
template<typename... Args>
inline void trace(format_string_t<Args...> fmt, Args &&...args)
void debug(loc_with_fmt fmt, Args &&...args)
{
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
log(fmt.loc, level::debug, fmt.fmt_string, std::forward<Args>(args)...);
}
template<typename... Args>
inline void debug(format_string_t<Args...> fmt, Args &&...args)
void info(loc_with_fmt fmt, Args &&...args)
{
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
log(fmt.loc, level::info, fmt.fmt_string, std::forward<Args>(args)...);
}
template<typename... Args>
inline void info(format_string_t<Args...> fmt, Args &&...args)
void warn(loc_with_fmt fmt, Args &&...args)
{
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
log(fmt.loc, level::warn, fmt.fmt_string, std::forward<Args>(args)...);
}
template<typename... Args>
inline void warn(format_string_t<Args...> fmt, Args &&...args)
void error(loc_with_fmt fmt, Args &&...args)
{
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
log(fmt.loc, level::err, fmt.fmt_string, std::forward<Args>(args)...);
}
template<typename... Args>
inline void error(format_string_t<Args...> fmt, Args &&...args)
void critical(loc_with_fmt fmt, Args &&...args)
{
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
log(fmt.loc, level::critical, fmt.fmt_string, std::forward<Args>(args)...);
}
// log functions with no format string, just string
template<typename S, typename = is_convertible_to_sv<S>>
void trace(const S &msg, source_loc loc = source_loc::current())
{
log(loc, level::trace, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void debug(const S &msg, source_loc loc = source_loc::current())
{
log(loc, level::debug, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void info(const S &msg, source_loc loc = source_loc::current())
{
log(loc, level::info, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void warn(const S &msg, source_loc loc = source_loc::current())
{
log(loc, level::warn, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void error(const S &msg, source_loc loc = source_loc::current())
{
log(loc, level::err, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void critical(const S &msg, source_loc loc = source_loc::current())
{
log(loc, level::critical, msg);
}
#else
template<typename... Args>
void trace(format_string_t<Args...> fmt, Args &&...args)
{
log(level::trace, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void critical(format_string_t<Args...> fmt, Args &&...args)
void debug(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
log(level::debug, fmt, std::forward<Args>(args)...);
}
template<typename T>
inline void log(source_loc source, level::level_enum lvl, const T &msg)
template<typename... Args>
void info(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source, lvl, msg);
log(level::info, fmt, std::forward<Args>(args)...);
}
template<typename T>
inline void log(level::level_enum lvl, const T &msg)
template<typename... Args>
void warn(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(lvl, msg);
log(level::warn, fmt, std::forward<Args>(args)...);
}
template<typename T>
inline void trace(const T &msg)
template<typename... Args>
void error(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->trace(msg);
log(level::err, fmt, std::forward<Args>(args)...);
}
template<typename T>
inline void debug(const T &msg)
template<typename... Args>
void critical(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->debug(msg);
log(level::critical, fmt, std::forward<Args>(args)...);
}
template<typename T>
inline void info(const T &msg)
// log functions with no format string, just string
template<typename S, typename = is_convertible_to_sv<S>>
void trace(const S &msg)
{
default_logger_raw()->info(msg);
log(level::trace, msg);
}
template<typename T>
inline void warn(const T &msg)
template<typename S, typename = is_convertible_to_sv<S>>
void debug(const S &msg)
{
default_logger_raw()->warn(msg);
log(level::debug, msg);
}
template<typename T>
inline void error(const T &msg)
template<typename S, typename = is_convertible_to_sv<S>>
void info(const S &msg)
{
default_logger_raw()->error(msg);
log(level::info, msg);
}
template<typename T>
inline void critical(const T &msg)
template<typename S, typename = is_convertible_to_sv<S>>
void warn(const S &msg)
{
default_logger_raw()->critical(msg);
log(level::warn, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void error(const S &msg)
{
log(level::err, msg);
}
template<typename S, typename = is_convertible_to_sv<S>>
void critical(const S &msg)
{
log(level::critical, msg);
}
#endif
} // namespace spdlog
//