improved simplified IcmpPinger logging (using LogPrefix like in DNS and PingScheduler)
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 20 May 2015 16:09:23 +0000 (18:09 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 20 May 2015 16:09:23 +0000 (18:09 +0200)
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h

index 9b69521..92a30fc 100644 (file)
@@ -95,7 +95,8 @@ IcmpPinger::IcmpPinger(
     ReplyReceived( false ),
     EchoReplyTimeoutInSec( echo_reply_timeout_in_sec ),
     PingerStatus( PingStatus_NotSent ),
-    PingDoneCallback()
+    PingDoneCallback(),
+    LogPrefix("IcmpPinger")
 {
     // Create "unique" identifier
     boost::uuids::random_generator random_gen;
@@ -103,6 +104,8 @@ IcmpPinger::IcmpPinger(
 
     BOOST_ASSERT( sizeof(Identifier) <= random_tag.size() );
     memcpy( &Identifier, random_tag.data, sizeof(Identifier) );
+
+    LogPrefix = "IPing(no IP yet): ";
 }
 
 /**
@@ -139,18 +142,12 @@ void IcmpPinger::ping(
 
 void IcmpPinger::stop_pinging()
 {
-    GlobalLogger.debug()
-           << DestinationEndpoint.address().to_string()
-           << ": stop_pinging" << endl;
+    GlobalLogger.debug() << LogPrefix << "stop_pinging" << endl;
 
-    GlobalLogger.debug()
-           << DestinationEndpoint.address().to_string()
-           << ": cancel timer" << endl;
+    GlobalLogger.debug() << LogPrefix << "cancel timer" << endl;
     IcmpPacketReceiveTimer.cancel();
 
-    GlobalLogger.debug()
-           << DestinationEndpoint.address().to_string()
-           << ": unregister" << endl;
+    GlobalLogger.debug() << LogPrefix << "unregister" << endl;
 
     IcmpPingerItem icmp_item = boost::static_pointer_cast<IcmpPinger>( get_myself().lock() );
     if ( icmp_item )
@@ -158,9 +155,8 @@ void IcmpPinger::stop_pinging()
         PacketDistributor->unregister_pinger( icmp_item );
     } else
     {
-        GlobalLogger.warning()
-            << DestinationEndpoint.address().to_string()
-            << ": weak pointer to pinger broken is empty. Huh?" << endl;
+        GlobalLogger.warning() << LogPrefix
+            << "weak pointer to pinger broken is empty. Huh?" << endl;
     }
 }
 
@@ -169,6 +165,11 @@ void IcmpPinger::set_destination_endpoint( const address &destination_ip )
 {
     uint16_t port = 0;
     DestinationEndpoint = icmp::endpoint( destination_ip, port );
+
+    // update LogPrefix
+    std::stringstream temp;
+    temp << "IPing(" << DestinationEndpoint.address().to_string() << "): ";
+    LogPrefix = temp.str();
 }
 
 bool IcmpPinger::start_send()
@@ -188,9 +189,7 @@ bool IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
     ostream os( &request_buffer );
     if ( !icmp_packet->write( os ) )
     {
-        GlobalLogger.error()
-           << DestinationEndpoint.address().to_string()
-           << ": fail writing ping data." << endl;
+        GlobalLogger.error() << LogPrefix << "fail writing ping data." << endl;
     }
 
     TimeSent = microsec_clock::universal_time();
@@ -202,25 +201,21 @@ bool IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
     size_t bytes_sent = 0;
     try
     {
-        GlobalLogger.info()
-                    << DestinationEndpoint.address().to_string()
-                    << ": sending ping" << endl;
+        GlobalLogger.info() << LogPrefix << "sending ping" << endl;
         const_buffers_1 data = request_buffer.data();
 
         // Block until send the data
         bytes_sent = PacketDistributor->get_socket()->send_to( data, DestinationEndpoint );
         if ( bytes_sent != buffer_size( data ) )
         {
-            GlobalLogger.error()
-                   << DestinationEndpoint.address().to_string()
-                   << ": fail sending ping data." << endl;
+            GlobalLogger.error() << LogPrefix << "fail sending ping data."
+                                 << endl;
         }
     }
     catch ( const exception &ex )
     {
-        GlobalLogger.error()
-                   << DestinationEndpoint.address().to_string()
-                   << ": fail sending ping data. " << ex.what() << endl;
+        GlobalLogger.error() << LogPrefix << "fail sending ping data. "
+                             << ex.what() << endl;
     }
 
     ReplyReceived = false;
@@ -252,11 +247,15 @@ void IcmpPinger::handle_timeout(const boost::system::error_code& error)
         if ( error ==  boost::asio::error::operation_aborted )
         {
             if (! ReplyReceived)
-                GlobalLogger.notice() << "Timer waiting for ICMP echo reply was cancelled!" << endl;
-            // otherwise probably called by IcmpPacketReceiveTimer.cancel in handle_receive_icmp_packet!
+                GlobalLogger.notice() << LogPrefix
+                    << "Timer waiting for ICMP echo reply was cancelled!"
+                    << endl;
+            // otherwise probably called by IcmpPacketReceiveTimer.cancel in
+            // handle_receive_icmp_packet!
         }
         else
-            GlobalLogger.notice() << "Error " << error << " waiting for ICMP echo reply!" << endl;
+            GlobalLogger.notice() << LogPrefix << "Error " << error
+                << " waiting for ICMP echo reply!" << endl;
 
         // Still continue with rest of function, so PingStatus is updated and Callback executed
         //   when timer was cancelled
@@ -266,9 +265,7 @@ void IcmpPinger::handle_timeout(const boost::system::error_code& error)
     // is also called by Timer.cancel();
     if ( !ReplyReceived )
     {
-        GlobalLogger.info()
-                   << DestinationEndpoint.address().to_string()
-                   << ": Request timed out" << endl;
+        GlobalLogger.info() << LogPrefix << "Request timed out" << endl;
 
         set_ping_status( PingStatus_FailureTimeout );
     }
@@ -311,9 +308,7 @@ bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
                                 Identifier, SequenceNumber,
                                 DestinationEndpoint.address() ) )
         {
-            GlobalLogger.info()
-               << DestinationEndpoint.address().to_string()
-               << ": Received reply" << endl;
+            GlobalLogger.info() << LogPrefix << "Received reply" << endl;
 
             ReplyReceived = true;
             does_match = true;
@@ -328,9 +323,8 @@ bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
                                      Identifier, SequenceNumber,
                                      DestinationEndpoint.address() ) )
         {
-            GlobalLogger.info()
-               << DestinationEndpoint.address().to_string()
-               << ": Received destination unreachable" << endl;
+            GlobalLogger.info() << LogPrefix
+                                << "Received destination unreachable" << endl;
 
             ReplyReceived = true;
             does_match = true;
@@ -345,9 +339,8 @@ bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
                                      Identifier, SequenceNumber,
                                      DestinationEndpoint.address() ) )
         {
-            GlobalLogger.info()
-               << DestinationEndpoint.address().to_string()
-               << ": Received time exceeded" << endl;
+            GlobalLogger.info() << LogPrefix
+                                << "Received time exceeded" << endl;
 
             ReplyReceived = true;
             does_match = true;
@@ -360,17 +353,15 @@ bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
         }
         else
         {
-            GlobalLogger.debug()
-               << DestinationEndpoint.address().to_string()
-               << ": Received packet that does not match or has wrong seq.nr"
+            GlobalLogger.debug() << LogPrefix
+               << "Received packet that does not match or has wrong seq.nr"
                << endl;
         }
     }
     catch ( std::exception &exc)
     {
-        GlobalLogger.warning()
-            << DestinationEndpoint.address().to_string()
-            << ": Caught exception in packet interpretation: " << exc.what()
+        GlobalLogger.warning() << LogPrefix
+            << "Caught exception in packet interpretation: " << exc.what()
             << std::endl;
         if ( IcmpPacketFactory::PacketDumpMode == DUMP_ALWAYS ||
              IcmpPacketFactory::PacketDumpMode == DUMP_IF_ERROR )
@@ -379,9 +370,8 @@ bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
     }
     catch ( ... )
     {
-        GlobalLogger.warning()
-            << DestinationEndpoint.address().to_string()
-            << ": Caught unspecified exception in packet interpretation!"
+        GlobalLogger.warning() << LogPrefix
+            << "Caught unspecified exception in packet interpretation!"
             << std::endl;
         if ( IcmpPacketFactory::PacketDumpMode == DUMP_ALWAYS ||
              IcmpPacketFactory::PacketDumpMode == DUMP_IF_ERROR )
index c1e1e6a..f6ce523 100644 (file)
@@ -177,6 +177,8 @@ private:
     PingStatus PingerStatus;
     /// Callback to notify when the ping is done (got reply/timeout)
     boost::function< void(bool) > PingDoneCallback;
+    /// prefix to logging output lines
+    std::string LogPrefix;
 };
 
 #endif // ICMP_PINGER_H