diff --git a/softwareComponents/messageLogger/include/message_logger.hpp b/softwareComponents/messageLogger/include/message_logger.hpp index e816ae7a9..5106a1aab 100644 --- a/softwareComponents/messageLogger/include/message_logger.hpp +++ b/softwareComponents/messageLogger/include/message_logger.hpp @@ -20,7 +20,7 @@ class MessageLogger { void logSending( std::string_view topic, const google::protobuf::Message & msg ) { - if ( !createFormattedString() ) { + if ( !loggingEnabled() ) { return; } @@ -29,9 +29,10 @@ class MessageLogger { topic, msg.ShortDebugString() ) ); } + void logReceived( std::string_view topic, const google::protobuf::Message & msg ) { - if ( !createFormattedString() ) { + if ( !loggingEnabled() ) { return; } @@ -41,8 +42,31 @@ class MessageLogger { msg.ShortDebugString() ) ); } + void logSubscribe( std::string_view topic ) + { + if ( !loggingEnabled() ) { + return; + } + + log( fmt::format( "{:%H:%M:%S} Subscribing to '{}':\n", + std::chrono::system_clock::now(), + topic ) ); + } + + void logUnsubscribe( std::string_view topic ) + { + if ( !loggingEnabled() ) { + return; + } + + log( fmt::format( "{:%H:%M:%S} Unsubscribing from '{}':\n", + std::chrono::system_clock::now(), + topic ) ); + } + private: - bool createFormattedString() + // Prevents unnecessary creation of the log string + bool loggingEnabled() { return _verbose || _ostr; } diff --git a/softwareComponents/rofiHalSim/message_logger.hpp b/softwareComponents/rofiHalSim/message_logger.hpp index a2f4c7ff0..d7144b2cf 100644 --- a/softwareComponents/rofiHalSim/message_logger.hpp +++ b/softwareComponents/rofiHalSim/message_logger.hpp @@ -37,4 +37,19 @@ inline void logMessage( [[maybe_unused]] const std::string & topic, #endif } +inline void logSubscription( [[maybe_unused]] const std::string & topic, + [[maybe_unused]] bool starting ) +{ +#if LOG_MESSAGES + auto now = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() ); + if ( starting ) { + std::cerr << std::put_time( std::localtime( &now ), "%T" ) << " Subscribing to '" << topic + << std::endl; + } else { + std::cerr << std::put_time( std::localtime( &now ), "%T" ) << " Unsubscribing from '" << topic + << std::endl; + } +#endif +} + } // namespace rofi::hal diff --git a/softwareComponents/rofiHalSim/subscriber_wrapper.hpp b/softwareComponents/rofiHalSim/subscriber_wrapper.hpp index 46c6d8c23..2dab621eb 100644 --- a/softwareComponents/rofiHalSim/subscriber_wrapper.hpp +++ b/softwareComponents/rofiHalSim/subscriber_wrapper.hpp @@ -24,6 +24,8 @@ class SubscriberWrapper { throw std::runtime_error( "empty callback" ); } + logSubscription( _topic, true ); + _sub = _node->Subscribe( _topic, &SubscriberWrapper::onMsg, this ); } @@ -33,6 +35,7 @@ class SubscriberWrapper { ~SubscriberWrapper() { if ( _sub ) { + logSubscription( _sub->GetTopic(), false ); _sub->Unsubscribe(); } } diff --git a/softwareComponents/simplesimLib/include/simplesim/distributor.hpp b/softwareComponents/simplesimLib/include/simplesim/distributor.hpp index f092a5835..7455c8adf 100644 --- a/softwareComponents/simplesimLib/include/simplesim/distributor.hpp +++ b/softwareComponents/simplesimLib/include/simplesim/distributor.hpp @@ -39,6 +39,7 @@ class Distributor { ~Distributor() { assert( _sub ); + _logger.logUnsubscribe( _sub->GetTopic() ); _sub->Unsubscribe(); } diff --git a/softwareComponents/simplesimLib/include/simplesim/locked_module_communication.hpp b/softwareComponents/simplesimLib/include/simplesim/locked_module_communication.hpp index dc091e6d3..74b36ddc4 100644 --- a/softwareComponents/simplesimLib/include/simplesim/locked_module_communication.hpp +++ b/softwareComponents/simplesimLib/include/simplesim/locked_module_communication.hpp @@ -33,6 +33,7 @@ class LockedModuleCommunication { ~LockedModuleCommunication() { assert( _sub ); + _logger.logUnsubscribe( _sub->GetTopic() ); _sub->Unsubscribe(); } diff --git a/softwareComponents/simplesimLib/src/distributor.cpp b/softwareComponents/simplesimLib/src/distributor.cpp index 1c391cbe4..8c2296daf 100644 --- a/softwareComponents/simplesimLib/src/distributor.cpp +++ b/softwareComponents/simplesimLib/src/distributor.cpp @@ -14,6 +14,7 @@ Distributor::Distributor( gazebo::transport::Node & node, if ( !_pub ) { throw std::runtime_error( "Publisher could not be created" ); } + _logger.logSubscribe( "~/distributor/request" ); _sub = node.Subscribe( "~/distributor/request", &Distributor::onRequestCallback, this ); if ( !_sub ) { throw std::runtime_error( "Subcriber could not be created" ); diff --git a/softwareComponents/simplesimLib/src/locked_module_communication.cpp b/softwareComponents/simplesimLib/src/locked_module_communication.cpp index 584932c09..2240fe37b 100644 --- a/softwareComponents/simplesimLib/src/locked_module_communication.cpp +++ b/softwareComponents/simplesimLib/src/locked_module_communication.cpp @@ -21,6 +21,7 @@ LockedModuleCommunication::LockedModuleCommunication( CommandHandler & commandHa assert( !moduleTopicName.empty() ); assert( _pub ); assert( _sub ); + _logger.logSubscribe( "~/" + moduleTopicName + "/control" ); } void LockedModuleCommunication::onRofiCmd( const LockedModuleCommunication::RofiCmdPtr & msg )