diff --git a/iceoryx_posh/include/iceoryx_posh/capro/service_description.hpp b/iceoryx_posh/include/iceoryx_posh/capro/service_description.hpp index 54c9e194dc..99cdf8d1af 100644 --- a/iceoryx_posh/include/iceoryx_posh/capro/service_description.hpp +++ b/iceoryx_posh/include/iceoryx_posh/capro/service_description.hpp @@ -20,6 +20,7 @@ #include "iceoryx_hoofs/cxx/serialization.hpp" #include "iceoryx_hoofs/cxx/string.hpp" #include "iceoryx_hoofs/cxx/vector.hpp" +#include "iceoryx_hoofs/log/logstream.hpp" #include "iceoryx_posh/iceoryx_posh_types.hpp" #include @@ -182,6 +183,8 @@ bool serviceMatch(const ServiceDescription& first, const ServiceDescription& sec /// @return the reference to `stream` which was provided as input parameter std::ostream& operator<<(std::ostream& stream, const ServiceDescription& service) noexcept; +log::LogStream& operator<<(log::LogStream& stream, const ServiceDescription& service) noexcept; + } // namespace capro } // namespace iox diff --git a/iceoryx_posh/source/capro/service_description.cpp b/iceoryx_posh/source/capro/service_description.cpp index 8dabdcc5cd..0af7eb07a0 100644 --- a/iceoryx_posh/source/capro/service_description.cpp +++ b/iceoryx_posh/source/capro/service_description.cpp @@ -235,5 +235,13 @@ std::ostream& operator<<(std::ostream& stream, const ServiceDescription& service return stream; } +log::LogStream& operator<<(log::LogStream& stream, const ServiceDescription& service) noexcept +{ + /// @todo #415 Add classHash, scope and interface + stream << "Service: " << service.getServiceIDString() << ", Instance: " << service.getInstanceIDString() + << ", Event: " << service.getEventIDString(); + return stream; +} + } // namespace capro } // namespace iox diff --git a/iceoryx_posh/test/moduletests/test_capro_service.cpp b/iceoryx_posh/test/moduletests/test_capro_service.cpp index 48c2e6e135..a3d44ad74c 100644 --- a/iceoryx_posh/test/moduletests/test_capro_service.cpp +++ b/iceoryx_posh/test/moduletests/test_capro_service.cpp @@ -20,6 +20,7 @@ #include "iceoryx_hoofs/cxx/convert.hpp" #include "iceoryx_hoofs/cxx/serialization.hpp" #include "iceoryx_hoofs/cxx/string.hpp" +#include "iceoryx_hoofs/testing/mocks/logger_mock.hpp" #include "iceoryx_posh/capro/service_description.hpp" /// @todo #415 replace the service registry include with the new discovery API header #include "iceoryx_posh/internal/roudi/service_registry.hpp" @@ -428,6 +429,25 @@ TEST_F(ServiceDescription_test, LessThanOperatorReturnsFalseIfEventStringOfFirst EXPECT_FALSE(serviceDescription1 < serviceDescription2); } +TEST_F(ServiceDescription_test, LogStreamConvertsServiceDescriptionToString) +{ + ::testing::Test::RecordProperty("TEST_ID", "42bc3f21-d9f4-4cc3-a37e-6508e1f981c1"); + Logger_Mock loggerMock; + + const IdString_t SERVICE_ID{"S"}; + const IdString_t INSTANCE_ID{"I"}; + const IdString_t EVENT_ID{"E"}; + const std::string SERVICE_DESCRIPTION_AS_STRING{"Service: S, Instance: I, Event: E"}; + auto sut = ServiceDescription{SERVICE_ID, INSTANCE_ID, EVENT_ID}; + + { + auto logstream = iox::log::LogStream(loggerMock); + logstream << sut; + } + + ASSERT_THAT(loggerMock.m_logs.size(), Eq(1U)); + EXPECT_THAT(loggerMock.m_logs[0].message, StrEq(SERVICE_DESCRIPTION_AS_STRING)); +} /// END SERVICEDESCRIPTION TESTS