Skip to content

Commit

Permalink
Fix clang-14 fmt compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Sherman committed Feb 5, 2024
1 parent ace4a7c commit 8a34ca1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 24 additions & 1 deletion components/pango_core/include/pangolin/utils/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,33 @@
namespace pangolin
{

// Wraps fmt::format to conform to its compile-time checked APIs.
//
// With more recent versions of fmt and more recent compilers,
// more compile-time checking is enabled in fmt by default.
//
// Pangolin logging currently relies on some runtime
// format string construction, so we add a layer of indirection here.
//
// Alternatively we could eliminate the use of runtime format strings in Pangolin.
template <typename TFmt, typename... TArgs>
std::string format(TFmt&& fmt, TArgs&&... args) {
if constexpr (std::is_same_v<TFmt, fmt::format_string<TArgs...>>) {
// The type of the fmt string is known at compile-time, forward as-is
return fmt::format(fmt, std::forward<TArgs>(args)...);
} else if constexpr (std::is_convertible_v<TFmt, std::string_view>) {
// The type of the fmt string is not known at compile-time, wrap in fmt::runtime
return fmt::format(fmt::runtime(fmt), std::forward<TArgs>(args)...);
} else {
// The fmt string is some other type, like a `fmt::text_style`, forward as-is
return fmt::format(fmt, std::forward<TArgs>(args)...);
}
}

template <typename... TArgs>
void println(TArgs&&... args)
{
fmt::print(std::forward<TArgs>(args)...);
fmt::print(fmt::runtime(format(std::forward<TArgs>(args)...)));
std::cout << std::endl;
}

Expand Down
4 changes: 2 additions & 2 deletions components/pango_core/include/pangolin/utils/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct Log {
const char* assertion_statement, Args... args)
{
if constexpr (sizeof...(args) > 0) {
const std::string arg_string = fmt::format(std::forward<Args>(args)...);
const std::string arg_string = pangolin::format(std::forward<Args>(args)...);
logImpl(kind, sFile, sFunction, nLine, assertion_statement, arg_string);
} else {
logImpl(kind, sFile, sFunction, nLine, assertion_statement, "");
Expand All @@ -137,7 +137,7 @@ struct Log {
const char* assertion_statement, Args... args)
{
if constexpr (sizeof...(args) > 0) {
const std::string arg_string = fmt::format(std::forward<Args>(args)...);
const std::string arg_string = pangolin::format(std::forward<Args>(args)...);
logImpl(kind, sFile, sFunction, nLine, assertion_statement, arg_string);
throw std::runtime_error(arg_string);
} else {
Expand Down

0 comments on commit 8a34ca1

Please sign in to comment.