Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error when using spdlog with FMT_ENFORCE_COMPILE_STRING #2379

Closed
john4744 opened this issue May 18, 2022 · 5 comments
Closed

Compile error when using spdlog with FMT_ENFORCE_COMPILE_STRING #2379

john4744 opened this issue May 18, 2022 · 5 comments

Comments

@john4744
Copy link
Contributor

I'm building an exe that uses spdlog and fmt. I'm building with FMT_ENFORCE_COMPILE_STRING defined. This is causing the following compile error:

c:\apps\3rdparty\spdlog\include\spdlog\fmt\bundled\core.h(603,39): error C2338: FMT_ENFORCE_COMPILE_STRING requires all format strings to use FMT_STRING.
c:\apps\3rdparty\spdlog\include\spdlog\fmt\bundled\core.h(3057): message : see reference to function template instantiation 'void fmt::v8::detail::check_format_string<,char[21],0>(const S (&))' being compiled
        with
        [
            S=char [21]
        ]
c:\apps\3rdparty\spdlog\include\spdlog\fmt\bundled\format-inl.h(96): message : see reference to function template instantiation 'fmt::v8::basic_format_string<char>::basic_format_string<char[21],0>(const S (&))' being compiled
        with
        [
            S=char [21]
        ]
c:\apps\3rdparty\spdlog\include\spdlog\fmt\bundled\format-inl.h(96): message : see reference to function template instantiation 'fmt::v8::basic_format_string<char>::basic_format_string<char[21],0>(const S (&))' being compiled
        with
        [
            S=char [21]
        ]

Not sure what I'm doing wrong, any help would be appreciated. Thanks!

@tt4g
Copy link
Contributor

tt4g commented May 18, 2022

Please wrap the format string with the FMT_STRING macro if define FMT_ENFORCE_COMPILE_STRING macro (this is fmt library feature.).

spdlog::info(FMT_STRING("Log with {}."), "macro"); // OK.

spdlog::info("Log without {}.", "macro"); // Assertion error.

@john4744
Copy link
Contributor Author

Thanks for the feedback! In this case, I only have #include "spdlog/spdlog.h", no actual calls into the spdlog API. I think I may have tracked down the problem to a misconfiguration on my part 🤦‍♂️ . I noticed that the example project has SPDLOG_COMPILED_LIB defined and my project didn't. Adding this define to my project seems to resolve the compile error.

@john4744
Copy link
Contributor Author

Actually, there still may be a problem here building for Android... When I include android_sink.h I get this compile error:

../../3rdparty/fmt/include/fmt/core.h:603:3: error: static_assert failed due to requirement 'is_compile_string<char [6]>::value' "FMT_ENFORCE_COMPILE_STRING requires all format strings to use FMT_STRING."
  static_assert(is_compile_string<S>::value,
  ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../3rdparty/fmt/include/fmt/core.h:3071:13: note: in instantiation of function template specialization 'fmt::v8::detail::check_format_string<int &, char [6], 0>' requested here
    detail::check_format_string<Args...>(s);
            ^
../../3rdparty/spdlog/include/spdlog/details/fmt_helper.h:110:54: note: in instantiation of function template specialization 'fmt::v8::basic_format_string<char, int &>::basic_format_string<char [6], 0>' requested here
        fmt_lib::format_to(std::back_inserter(dest), "{:02}", n);

I'm building with SPDLOG_COMPILED_LIB and FMT_ENFORCE_COMPILE_STRING defined.

@john4744 john4744 reopened this May 18, 2022
@tt4g
Copy link
Contributor

tt4g commented May 18, 2022

The FMT_STRING macro is missing here:

inline void pad2(int n, memory_buf_t &dest)
{
if (n >= 0 && n < 100) // 0-99
{
dest.push_back(static_cast<char>('0' + n / 10));
dest.push_back(static_cast<char>('0' + n % 10));
}
else // unlikely, but just in case, let fmt deal with it
{
fmt_lib::format_to(std::back_inserter(dest), "{:02}", n);
}
}

The following fix should work around the problem.

-fmt_lib::format_to(std::back_inserter(dest), "{:02}", n); 
+fmt_lib::format_to(std::back_inserter(dest), FMT_STRING("{:02}"), n); 

And can you submit a PR?

@john4744
Copy link
Contributor Author

I think I may have found a few other spots that need the FMT_STRING, I'll submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants