Skip to content

Commit

Permalink
Fix formatting function for VS 17.10. (#145)
Browse files Browse the repository at this point in the history
* Fix formatting function for VS 17.10.
* Fix github action for formatting.
  • Loading branch information
Holt59 authored May 26, 2024
1 parent 4ed2daa commit 9da371b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run clang-format
uses: jidicula/clang-format-action@v4.11.0
- name: Check format
uses: ModOrganizer2/check-formatting-action@master
with:
clang-format-version: "15"
check-path: "."
exclude-regex: "third-party"
39 changes: 25 additions & 14 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ struct BlacklistEntry

namespace MOBase::log::details
{

// TODO: remove for C++23
template <typename T>
concept formattable = requires(T& v, std::format_context ctx) {
std::formatter<std::remove_cvref_t<T>>().format(v, ctx);
};

template <typename F, typename... Args>
concept RuntimeFormatString = requires(F&& f, Args&&... args) {
(formattable<Args> && ...);
!std::is_convertible_v<std::decay_t<F>, std::string_view>;
};

void QDLLEXPORT doLogImpl(spdlog::logger& lg, Levels lv, const std::string& s) noexcept;

void QDLLEXPORT ireplace_all(std::string& input, std::string const& search,
Expand Down Expand Up @@ -94,11 +107,9 @@ void doLog(spdlog::logger& logger, Levels lv,
if constexpr (sizeof...(Args) == 0) {
s = std::format("{}", std::forward<F>(format));
} else if constexpr (std::is_same_v<std::decay_t<F>, QString>) {
s = std::vformat(format.toStdString(),
std::make_format_args(std::forward<Args>(args)...));
s = std::vformat(format.toStdString(), std::make_format_args(args...));
} else {
s = std::vformat(std::forward<F>(format),
std::make_format_args(std::forward<Args>(args)...));
s = std::vformat(std::forward<F>(format), std::make_format_args(args...));
}

// check the blacklist
Expand Down Expand Up @@ -187,7 +198,7 @@ class QDLLEXPORT Logger
void resetBlacklist();

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void debug(F&& format, Args&&... args) noexcept
{
log(Debug, std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -200,7 +211,7 @@ class QDLLEXPORT Logger
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void info(F&& format, Args&&... args) noexcept
{
log(Info, std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -213,7 +224,7 @@ class QDLLEXPORT Logger
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void warn(F&& format, Args&&... args) noexcept
{
log(Warning, std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -226,7 +237,7 @@ class QDLLEXPORT Logger
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void error(F&& format, Args&&... args) noexcept
{
log(Error, std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -239,7 +250,7 @@ class QDLLEXPORT Logger
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void log(Levels lv, F&& format, Args&&... args) noexcept
{
details::doLog(*m_logger, lv, m_conf.blacklist, std::forward<F>(format),
Expand Down Expand Up @@ -267,7 +278,7 @@ QDLLEXPORT void createDefault(LoggerConfiguration conf);
QDLLEXPORT Logger& getDefault();

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void debug(F&& format, Args&&... args) noexcept
{
getDefault().debug(std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -280,7 +291,7 @@ void debug(std::format_string<Args...> format, Args&&... args) noexcept
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void info(F&& format, Args&&... args) noexcept
{
getDefault().info(std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -293,7 +304,7 @@ void info(std::format_string<Args...> format, Args&&... args) noexcept
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void warn(F&& format, Args&&... args) noexcept
{
getDefault().warn(std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -306,7 +317,7 @@ void warn(std::format_string<Args...> format, Args&&... args) noexcept
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void error(F&& format, Args&&... args) noexcept
{
getDefault().error(std::forward<F>(format), std::forward<Args>(args)...);
Expand All @@ -319,7 +330,7 @@ void error(std::format_string<Args...> format, Args&&... args) noexcept
}

template <class F, class... Args>
requires(!std::is_constructible_v<std::string_view, std::decay_t<F>>)
requires(details::RuntimeFormatString<F, Args...>)
void log(Levels lv, F&& format, Args&&... args) noexcept
{
getDefault().log(lv, std::forward<F>(format), std::forward<Args>(args)...);
Expand Down

0 comments on commit 9da371b

Please sign in to comment.