Skip to content

Commit

Permalink
simplesim: log subscribing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaromel-Tuzerad committed Dec 9, 2023
1 parent dfa1a26 commit 0a2454d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions softwareComponents/messageLogger/include/message_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MessageLogger {
topic,
msg.ShortDebugString() ) );
}

void logReceived( std::string_view topic, const google::protobuf::Message & msg )
{
if ( !createFormattedString() ) {
Expand All @@ -41,6 +42,28 @@ class MessageLogger {
msg.ShortDebugString() ) );
}

void logSubscribe( std::string_view topic )
{
if ( !createFormattedString() ) {
return;
}

log( fmt::format( "{:%H:%M:%S} Subscribing to '{}':\n",
std::chrono::system_clock::now(),
topic ) );
}

void logUnsubscribe( std::string_view topic )
{
if ( !createFormattedString() ) {
return;
}

log( fmt::format( "{:%H:%M:%S} Unsubscribing from '{}':\n",
std::chrono::system_clock::now(),
topic ) );
}

private:
bool createFormattedString()
{
Expand Down
15 changes: 15 additions & 0 deletions softwareComponents/rofiHalSim/message_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions softwareComponents/rofiHalSim/subscriber_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class SubscriberWrapper {
throw std::runtime_error( "empty callback" );
}

logSubscription( _topic, true );

_sub = _node->Subscribe( _topic, &SubscriberWrapper::onMsg, this );
}

Expand All @@ -33,6 +35,7 @@ class SubscriberWrapper {
~SubscriberWrapper()
{
if ( _sub ) {
logSubscription( _sub->GetTopic(), false );
_sub->Unsubscribe();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Distributor {
~Distributor()
{
assert( _sub );
_logger.logUnsubscribe( _sub->GetTopic() );
_sub->Unsubscribe();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class LockedModuleCommunication {
~LockedModuleCommunication()
{
assert( _sub );
_logger.logUnsubscribe( _sub->GetTopic() );
_sub->Unsubscribe();
}

Expand Down
1 change: 1 addition & 0 deletions softwareComponents/simplesimLib/src/distributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ LockedModuleCommunication::LockedModuleCommunication( CommandHandler & commandHa
&LockedModuleCommunication::onRofiCmd,
this ) )
{
_logger.logSubscribe( "~/" + moduleTopicName + "/control" );
assert( !moduleTopicName.empty() );
assert( _pub );
assert( _sub );
Expand Down

0 comments on commit 0a2454d

Please sign in to comment.