Skip to content

Commit

Permalink
Merge pull request #2008 from dkavolis/v1.x
Browse files Browse the repository at this point in the history
Enable format string compile time validation
  • Loading branch information
gabime authored Jul 22, 2021
2 parents 0f39da5 + d8f13cb commit bee3e63
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 47 deletions.
11 changes: 11 additions & 0 deletions include/spdlog/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,24 @@ using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
template<typename T>
struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t>
{};
template<class T>
struct is_convertible_to_wformat_string : std::is_convertible<T, fmt::wformat_string<>>
{};
# endif // _WIN32
#else
template<typename>
struct is_convertible_to_wstring_view : std::false_type
{};
template<class>
struct is_convertible_to_wformat_string : std::false_type
{};
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT

template<class T>
struct is_convertible_to_basic_format_string
: std::integral_constant<bool, std::is_convertible<const T &, fmt::format_string<>>::value || is_convertible_to_wformat_string<T>::value>
{};

#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
#else
Expand Down
100 changes: 73 additions & 27 deletions include/spdlog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ class SPDLOG_API logger
logger &operator=(logger other) SPDLOG_NOEXCEPT;
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;

template<typename FormatString, typename... Args>
void log(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args)
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
{
log_(loc, lvl, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
void log(level::level_enum lvl, const FormatString &fmt, Args &&...args)
template<typename... Args>
void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
{
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
Expand Down Expand Up @@ -134,50 +134,98 @@ class SPDLOG_API logger
}

// T cannot be statically converted to string_view or wstring_view
template<class T, typename std::enable_if<!std::is_convertible<const T &, spdlog::string_view_t>::value &&
!is_convertible_to_wstring_view<const T &>::value,
int>::type = 0>
template<class T, typename std::enable_if<!is_convertible_to_basic_format_string<T>::value, int>::type = 0>
void log(source_loc loc, level::level_enum lvl, const T &msg)
{
log(loc, lvl, "{}", msg);
}

template<typename FormatString, typename... Args>
void trace(const FormatString &fmt, Args &&...args)
template<typename... Args>
void trace(fmt::format_string<Args...> fmt, Args &&...args)
{
log(level::trace, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
void debug(const FormatString &fmt, Args &&...args)
template<typename... Args>
void debug(fmt::format_string<Args...> fmt, Args &&...args)
{
log(level::debug, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
void info(const FormatString &fmt, Args &&...args)
template<typename... Args>
void info(fmt::format_string<Args...> fmt, Args &&...args)
{
log(level::info, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
void warn(const FormatString &fmt, Args &&...args)
template<typename... Args>
void warn(fmt::format_string<Args...> fmt, Args &&...args)
{
log(level::warn, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
void error(const FormatString &fmt, Args &&...args)
template<typename... Args>
void error(fmt::format_string<Args...> fmt, Args &&...args)
{
log(level::err, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
void critical(const FormatString &fmt, Args &&...args)
template<typename... Args>
void critical(fmt::format_string<Args...> fmt, Args &&...args)
{
log(level::critical, fmt, std::forward<Args>(args)...);
}

#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void log(source_loc loc, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
{
log_(loc, lvl, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void trace(fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(level::trace, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void debug(fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(level::debug, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void info(fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(level::info, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void warn(fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(level::warn, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void error(fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(level::err, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void critical(fmt::wformat_string<Args...> fmt, Args &&...args)
{
log(level::critical, fmt, std::forward<Args>(args)...);
}
#endif

template<typename T>
void trace(const T &msg)
{
Expand Down Expand Up @@ -269,9 +317,8 @@ class SPDLOG_API logger
details::backtracer tracer_;

// common implementation for after templated public api has been resolved
template<typename FormatString, typename... Args, typename Char = fmt::char_t<FormatString>,
typename std::enable_if<!std::is_same<Char, wchar_t>::value, Char>::type * = nullptr>
void log_(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args)
template<typename... Args>
void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args)
{
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
Expand All @@ -282,17 +329,16 @@ class SPDLOG_API logger
SPDLOG_TRY
{
memory_buf_t buf;
fmt::detail::vformat_to(buf, fmt::to_string_view(fmt), fmt::make_args_checked<Args...>(fmt, args...));
fmt::detail::vformat_to(buf, fmt, fmt::make_format_args(args...));
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg, log_enabled, traceback_enabled);
}
SPDLOG_LOGGER_CATCH()
}

#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename FormatString, typename... Args, typename Char = fmt::char_t<FormatString>,
typename std::enable_if<std::is_same<Char, wchar_t>::value, Char>::type * = nullptr>
void log_(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args)
template<typename... Args>
void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args)
{
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
Expand All @@ -304,7 +350,7 @@ class SPDLOG_API logger
{
// format to wmemory_buffer and convert to utf8
fmt::wmemory_buffer wbuf;
fmt::detail::vformat_to(wbuf, fmt::wstring_view(fmt), fmt::make_args_checked<Args...>(fmt, args...));
fmt::detail::vformat_to(wbuf, fmt, fmt::make_format_args<fmt::wformat_context>(args...));
memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
Expand Down
82 changes: 66 additions & 16 deletions include/spdlog/spdlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,50 +127,50 @@ SPDLOG_API spdlog::logger *default_logger_raw();

SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);

template<typename FormatString, typename... Args>
inline void log(source_loc source, level::level_enum lvl, const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void log(level::level_enum lvl, const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void trace(const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void trace(fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void debug(const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void debug(fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void info(const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void info(fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void warn(const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void warn(fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void error(const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void error(fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
}

template<typename FormatString, typename... Args>
inline void critical(const FormatString &fmt, Args &&...args)
template<typename... Args>
inline void critical(fmt::format_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
}
Expand All @@ -187,6 +187,56 @@ inline void log(level::level_enum lvl, const T &msg)
default_logger_raw()->log(lvl, msg);
}

#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void trace(fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void debug(fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void info(fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void warn(fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void error(fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
}

template<typename... Args>
inline void critical(fmt::wformat_string<Args...> fmt, Args &&...args)
{
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
}
#endif

template<typename T>
inline void trace(const T &msg)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/test_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST_CASE("default_error_handler", "[errors]]")

auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v");
logger->info("Test message {} {}", 1);
logger->info(fmt::runtime("Test message {} {}"), 1);
logger->info("Test message {}", 2);
logger->flush();

Expand All @@ -49,7 +49,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->info("Good message #1");

REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
REQUIRE_THROWS_AS(logger->info(fmt::runtime("Bad format msg {} {}"), "xxx"), custom_ex);
logger->info("Good message #2");
require_message_count(SIMPLE_LOG, 2);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ TEST_CASE("async_error_handler", "[errors]]")
ofs << err_msg;
});
logger->info("Good message #1");
logger->info("Bad format msg {} {}", "xxx");
logger->info(fmt::runtime("Bad format msg {} {}"), "xxx");
logger->info("Good message #2");
spdlog::drop("logger"); // force logger to drain the queue and shutdown
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_stdout_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ TEST_CASE("wchar_api", "[stdout]")
spdlog::drop_all();
}

#endif
#endif

0 comments on commit bee3e63

Please sign in to comment.