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 Jan 20, 2024
1 parent a03a694 commit 170720c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
30 changes: 27 additions & 3 deletions softwareComponents/messageLogger/include/message_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MessageLogger {

void logSending( std::string_view topic, const google::protobuf::Message & msg )
{
if ( !createFormattedString() ) {
if ( !loggingEnabled() ) {
return;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
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 @@ -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 )
Expand Down

0 comments on commit 170720c

Please sign in to comment.