forked from brettviren/wire-cell-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from WireCell/fix-spdlog-fmtlib
Fix spdlog fmtlib
- Loading branch information
Showing
18 changed files
with
201 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
Include this file instead of directly including spdlog or fmtlib (fmt) | ||
headers. | ||
It takes care of changing fmtlib API in the face of the fact that spdlog may | ||
be compiled against an external fmtlib or use its "bundled" copy. | ||
More info: https://github.com/WireCell/wire-cell-spack/issues/12 | ||
*/ | ||
|
||
#ifndef WIRECELLUTIL_SPDLOG | ||
#define WIRECELLUTIL_SPDLOG | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
// We need FMT version but it is found in core.h in different locations | ||
// depending on if fmtlib is external to or bundled with spdlog. | ||
#if defined(SPDLOG_FMT_EXTERNAL) | ||
#include <fmt/core.h> | ||
#else | ||
#include <spdlog/fmt/bundled/core.h> | ||
#endif | ||
|
||
// Newer fmtlib no longer implicitly uses the ostream insertion operator<<. So | ||
// we must have some ugliness to allow us to provide new, required specialization: | ||
// | ||
// template <> struct fmt::formatter<TYPE> : fmt::ostream_formatter {}; | ||
// | ||
// while backporting that base type to old fmtlib. | ||
#if FMT_VERSION >= 90000 | ||
#include <fmt/ostream.h> | ||
// This provides fmt::ostream_formatter. | ||
#else | ||
#include <spdlog/fmt/ostr.h> | ||
// This id copied from fmt/ostream.h from fmtlib 10.2 with const_cast tweak. | ||
namespace fmt { | ||
template <typename Char> | ||
struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> { | ||
void set_debug_format() = delete; | ||
|
||
template <typename T, typename OutputIt> | ||
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const | ||
-> OutputIt { | ||
auto buffer = basic_memory_buffer<Char>(); | ||
detail::format_value(buffer, value); | ||
// Must const_cast as apparently the format() method from fmtlib 7.1.3 is not const. | ||
return const_cast<basic_ostream_formatter<Char>*>(this)->formatter<basic_string_view<Char>, Char>::format( | ||
{buffer.data(), buffer.size()}, ctx); | ||
} | ||
}; | ||
using ostream_formatter = basic_ostream_formatter<char>; | ||
} | ||
#endif | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
Test for fmtlib's change in how it handles (or doesn't handle) implicitly | ||
using ostream insertion operator<< starting at fmtlib 9. | ||
Test like: | ||
spack install [email protected]+shared ^[email protected] | ||
spack install [email protected]+shared ^[email protected] | ||
spack install [email protected]+shared ^[email protected] | ||
spack install [email protected]+shared ^[email protected] | ||
spack install [email protected]+shared | ||
The goal is to force a version of fmtlib near to the version of the bundled | ||
fmtlib for a given spdlog version. | ||
For each, | ||
spack view add -i spdlog-<version>/local spdlog@<version> | ||
echo 'load_prefix local' > spdlog-<version>/.envrc | ||
cd spdlog-<version>/ | ||
direnv allow | ||
g++ -Wall -std=c++17 \ | ||
-o test-spdlog-fmtlib \ | ||
../test-spdlog-fmtlib.cxx \ | ||
(pkg-config spdlog --cflags --libs | string split ' ') | ||
(fish syntax) | ||
./test-spdlog-fmtlib | ||
*/ | ||
|
||
#include <iostream> | ||
namespace NS { | ||
|
||
struct S { int x; }; | ||
|
||
inline std::ostream& operator<<(std::ostream& os, const S& s) | ||
{ | ||
os << "<S.x=" << s.x << ">"; | ||
return os; | ||
} | ||
} | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
#if defined(SPDLOG_FMT_EXTERNAL) | ||
#include <fmt/core.h> | ||
#else | ||
#include <spdlog/fmt/bundled/core.h> | ||
#endif | ||
|
||
#if FMT_VERSION >= 90000 | ||
#include <fmt/ostream.h> | ||
// This provides fmt::ostream_formatter. | ||
#else | ||
#include <spdlog/fmt/ostr.h> | ||
// This id copied from fmt/ostream.h from fmtlib 10.2 with const_cast tweak. | ||
namespace fmt { | ||
template <typename Char> | ||
struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> { | ||
void set_debug_format() = delete; | ||
|
||
template <typename T, typename OutputIt> | ||
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const | ||
-> OutputIt { | ||
auto buffer = basic_memory_buffer<Char>(); | ||
detail::format_value(buffer, value); | ||
// Must const_cast as apparently the format() method from fmtlib 7.1.3 is not const. | ||
return const_cast<basic_ostream_formatter<Char>*>(this)->formatter<basic_string_view<Char>, Char>::format( | ||
{buffer.data(), buffer.size()}, ctx); | ||
} | ||
}; | ||
using ostream_formatter = basic_ostream_formatter<char>; | ||
} | ||
#endif | ||
|
||
|
||
// The goal with the mess above is to make this "new style" requirement work with old fmtlib. | ||
template <> struct fmt::formatter<NS::S> : fmt::ostream_formatter {}; | ||
|
||
|
||
|
||
int main() | ||
{ | ||
NS::S s{42}; | ||
std::cerr << s << "\n"; | ||
spdlog::info("{}", s); | ||
spdlog::info("FMT_VERSION={}", FMT_VERSION); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters