Browse Source

Fixes for C++20 support (#215)

* fix: using soralog
* feature: formatter for outcome
* fix: muxer and streams test
* update: hunter
* update: own version

Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
pull/218/head v0.1.14
Dmitriy Khaustov 1 year ago
committed by GitHub
parent
commit
f38725b3db
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CMakeLists.txt
  2. 4
      cmake/Hunter/init.cmake
  3. 28
      include/libp2p/log/sublogger.hpp
  4. 62
      include/libp2p/outcome/outcome.hpp
  5. 36
      test/libp2p/muxer/muxers_and_streams_test.cpp
  6. 1
      test/libp2p/protocol/gossip/CMakeLists.txt

2
CMakeLists.txt

@ -18,7 +18,7 @@ cmake_policy(SET CMP0135 NEW)
include("cmake/Hunter/init.cmake") include("cmake/Hunter/init.cmake")
project(libp2p VERSION 0.0.1 LANGUAGES C CXX) project(libp2p VERSION 0.1.14 LANGUAGES C CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

4
cmake/Hunter/init.cmake

@ -31,7 +31,7 @@ set(
include(${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake) include(${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake)
HunterGate( HunterGate(
URL "https://github.com/qdrvm/hunter/archive/refs/tags/v0.23.257-qdrvm1.zip" URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.23.257-qdrvm4.tar.gz
SHA1 "e6aee2a8086d749d19d31d587f1f19c750a820ea" SHA1 da7f2ca25a1f79e809732175ab0b026f3ac89e39
LOCAL LOCAL
) )

28
include/libp2p/log/sublogger.hpp

@ -12,6 +12,10 @@
namespace libp2p::log { namespace libp2p::log {
namespace fmt {
using namespace soralog::fmt;
} // namespace fmt
/// Local logger with common prefix used to distinguish message source /// Local logger with common prefix used to distinguish message source
/// instances /// instances
class SubLogger { class SubLogger {
@ -37,7 +41,7 @@ namespace libp2p::log {
prefix_size_(prefix_.size()) {} prefix_size_(prefix_.size()) {}
template <typename... Args> template <typename... Args>
void log(soralog::Level level, std::string_view fmt, const Args &... args) { void log(soralog::Level level, std::string_view fmt, const Args &...args) {
if (log_->level() >= level) { if (log_->level() >= level) {
prefix_.append(fmt.data(), fmt.size()); prefix_.append(fmt.data(), fmt.size());
log_->log(level, prefix_, args...); log_->log(level, prefix_, args...);
@ -46,47 +50,47 @@ namespace libp2p::log {
} }
template <typename... Args> template <typename... Args>
void trace(std::string_view fmt, const Args &... args) { void trace(std::string_view fmt, const Args &...args) {
log(Level::TRACE, fmt, args...); log(Level::TRACE, fmt, args...);
} }
template <typename... Args> template <typename... Args>
void debug(std::string_view fmt, const Args &... args) { void debug(std::string_view fmt, const Args &...args) {
log(Level::DEBUG, fmt, args...); log(Level::DEBUG, fmt, args...);
} }
template <typename... Args> template <typename... Args>
void verbose(std::string_view fmt, const Args &... args) { void verbose(std::string_view fmt, const Args &...args) {
log(Level::VERBOSE, fmt, args...); log(Level::VERBOSE, fmt, args...);
} }
template <typename... Args> template <typename... Args>
void info(std::string_view fmt, const Args &... args) { void info(std::string_view fmt, const Args &...args) {
log(Level::INFO, fmt, args...); log(Level::INFO, fmt, args...);
} }
template <typename... Args> template <typename... Args>
void warn(std::string_view fmt, const Args &... args) { void warn(std::string_view fmt, const Args &...args) {
log(Level::WARN, fmt, args...); log(Level::WARN, fmt, args...);
} }
template <typename... Args> template <typename... Args>
void error(std::string_view fmt, const Args &... args) { void error(std::string_view fmt, const Args &...args) {
log(Level::ERROR, fmt, args...); log(Level::ERROR, fmt, args...);
} }
template <typename... Args> template <typename... Args>
void critical(std::string_view fmt, const Args &... args) { void critical(std::string_view fmt, const Args &...args) {
log(Level::CRITICAL, fmt, args...); log(Level::CRITICAL, fmt, args...);
} }
private: private:
template <typename T> template <typename T>
auto makePrefix(std::string_view prefix, T instance) { auto makePrefix(std::string_view prefix, T instance) {
if constexpr (std::is_pointer_v< if constexpr (std::is_pointer_v<T>
T> and not std::is_same_v<T, const char *>) { and not std::is_same_v<T, const char *>) {
return fmt::format("{}({:x}): ", prefix, auto ptr_as_int = reinterpret_cast<std::intptr_t>(instance); // NOLINT
reinterpret_cast<void *>(instance)); // NOLINT; return fmt::format("{}({:x}): ", prefix, ptr_as_int);
} else if constexpr (std::is_integral_v<T> and sizeof(T) > 1) { } else if constexpr (std::is_integral_v<T> and sizeof(T) > 1) {
return fmt::format("{}#{}: ", prefix, instance); return fmt::format("{}#{}: ", prefix, instance);
} else { } else {

62
include/libp2p/outcome/outcome.hpp

@ -10,6 +10,8 @@
#include <boost/outcome/success_failure.hpp> #include <boost/outcome/success_failure.hpp>
#include <boost/outcome/try.hpp> #include <boost/outcome/try.hpp>
#include <soralog/common.hpp>
// To define OUTCOME_TRY macro, we will need to create OUTCOME_TRY_1 and // To define OUTCOME_TRY macro, we will need to create OUTCOME_TRY_1 and
// OUTCOME_TRY_2 depending on number of arguments // OUTCOME_TRY_2 depending on number of arguments
#define OUTCOME_TRY_1(...) BOOST_OUTCOME_TRY(__VA_ARGS__) #define OUTCOME_TRY_1(...) BOOST_OUTCOME_TRY(__VA_ARGS__)
@ -43,4 +45,64 @@ namespace libp2p::outcome {
// @see /docs/result.md // @see /docs/result.md
template <>
struct fmt::formatter<std::error_code> {
// Parses format specifications. Must be empty
constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
// Parse the presentation format and store it in the formatter:
auto it = ctx.begin(), end = ctx.end();
// Check if reached the end of the range:
if (it != end && *it != '}') {
throw format_error("invalid format");
}
// Return an iterator past the end of the parsed range:
return it;
}
// Formats the std::error_code
template <typename FormatContext>
auto format(const std::error_code &ec, FormatContext &ctx) const
-> decltype(ctx.out()) {
// ctx.out() is an output iterator to write to.
return soralog::fmt::format_to(ctx.out(), "{}", ec.message());
}
};
template <typename Result, typename Failure>
struct fmt::formatter<libp2p::outcome::result<Result, Failure>> {
// Parses format specifications. Must be empty
constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
// Parse the presentation format and store it in the formatter:
auto it = ctx.begin(), end = ctx.end();
// Check if reached the end of the range:
if (it != end && *it != '}') {
throw format_error("invalid format");
}
// Return an iterator past the end of the parsed range:
return it;
}
// Formats the outcome result
template <typename FormatContext>
auto format(const libp2p::outcome::result<Result, Failure> &res,
FormatContext &ctx) const -> decltype(ctx.out()) {
// ctx.out() is an output iterator to write to.
if (res.has_value()) {
if constexpr (not std::is_void_v<Result>) {
return soralog::fmt::format_to(ctx.out(), "{}", res.value());
} else {
return soralog::fmt::format_to(ctx.out(), "<success>");
}
} else {
return soralog::fmt::format_to(ctx.out(), "{}", res.error());
}
}
};
#endif // LIBP2P_OUTCOME_HPP #endif // LIBP2P_OUTCOME_HPP

36
test/libp2p/muxer/muxers_and_streams_test.cpp

@ -60,7 +60,43 @@ namespace libp2p::regression {
} }
return os; return os;
} }
} // namespace libp2p::regression
template <>
struct fmt::formatter<libp2p::regression::Stats::Event> {
// Parses format specifications. Must be empty
constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
// Parse the presentation format and store it in the formatter:
auto it = ctx.begin(), end = ctx.end();
// Check if reached the end of the range:
if (it != end && *it != '}') {
throw format_error("invalid format");
}
// Return an iterator past the end of the parsed range:
return it;
}
// Formats the std::error_code
template <typename FormatContext>
auto format(const libp2p::regression::Stats::Event &ev,
FormatContext &ctx) const -> decltype(ctx.out()) {
// ctx.out() is an output iterator to write to.
switch (ev) {
#define PRINT_EVENT(E) \
case libp2p::regression::Stats::E: \
return soralog::fmt::format_to(ctx.out(), #E);
EVENTS(PRINT_EVENT)
#undef PRINT_EVENT
default:
return soralog::fmt::format_to(ctx.out(), "<unknown>");
}
}
};
namespace libp2p::regression {
class Node : public std::enable_shared_from_this<Node> { class Node : public std::enable_shared_from_this<Node> {
public: public:
using Behavior = std::function<void(Node &node)>; using Behavior = std::function<void(Node &node)>;

1
test/libp2p/protocol/gossip/CMakeLists.txt

@ -7,6 +7,7 @@ addtest(gossip_structures_test
target_link_libraries(gossip_structures_test target_link_libraries(gossip_structures_test
p2p_gossip p2p_gossip
p2p_testutil_peer p2p_testutil_peer
p2p_basic_scheduler
) )
addtest(gossip_local_subs_test addtest(gossip_local_subs_test

Loading…
Cancel
Save