Add optional std::format and fmtlib support to examples #51
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds optional support for modern C++ formatting (
std::format
orfmtlib
) to the http_io examples, addressing the need for better formatting capabilities in the server example.Problem
The server example's logger currently uses
std::stringstream
for formatting, which is verbose and less efficient. As noted in the issue, the server example needsfmtlib
orstd::format
to be fully useful. Additionally, the burl client example had a TODO comment to replaceostringstream
usage.Solution
This implementation uses a tiered conditional compilation approach that automatically detects and uses the best available formatting method:
std::format
- Used automatically when C++20 is available (viastd::vformat
for runtime format strings)<fmt/core.h>
is availablestd::stringstream
- Legacy fallback for maximum compatibilityThe solution requires no changes to existing code and introduces no hard dependencies - fmtlib is completely optional.
API Examples
When
std::format
or fmtlib is available, the logger supports modern format strings:When neither is available, the legacy variadic API continues to work:
Both styles work regardless of which backend is used, ensuring seamless migration.
Changes
Core Implementation:
example/server/format.hpp
- Conditional compilation utility for format detectionexample/server/logger.hpp
- Format string support when available, with inline docsexample/server/CMakeLists.txt
- Optional fmtlib detection viafind_package(fmt QUIET)
example/server/Jamfile
- Optional fmtlib linking viaac.check-library
example/client/burl/utils.cpp
- Format support forformat_size()
function (resolves TODO)example/client/burl/CMakeLists.txt
- Optional fmtlib detectionexample/client/burl/Jamfile
- Optional fmtlib linking viaac.check-library
Documentation:
example/server/README.md
- Comprehensive usage guide and build instructionsexample/server/logger_example.cpp
- Working examples of both API stylesREADME.adoc
- Optional dependencies section with installation instructionsIMPLEMENTATION_NOTES.md
- Technical details and design rationaleBuilding
The examples build successfully with or without format support:
With CMake:
With Boost.Build (b2):
# Builds with fmtlib if available, otherwise falls back to stringstream b2 example/server b2 example/client/burl
Benefits
Testing
worker.hpp
Fixes #50
Original prompt
Fixes #50
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.