Introduce network emulated endpoint optional name for better logging

Change-Id: Iedce88400c6f1e91c30249fb49c7914723da2a8d
Bug: None
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/203141
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Andrey Logvin <landrey@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33054}
diff --git a/api/test/network_emulation_manager.h b/api/test/network_emulation_manager.h
index 4857c87..dd4c835 100644
--- a/api/test/network_emulation_manager.h
+++ b/api/test/network_emulation_manager.h
@@ -56,6 +56,8 @@
     kDebug
   };
 
+  // If specified will be used to name endpoint for logging purposes.
+  absl::optional<std::string> name = absl::nullopt;
   IpAddressFamily generated_ip_family = IpAddressFamily::kIpv4;
   // If specified will be used as IP address for endpoint node. Must be unique
   // among all created nodes.
diff --git a/test/network/BUILD.gn b/test/network/BUILD.gn
index 66c178f..7fd4992 100644
--- a/test/network/BUILD.gn
+++ b/test/network/BUILD.gn
@@ -138,7 +138,10 @@
     "../../rtc_base:rtc_event",
     "../time_controller",
   ]
-  absl_deps = [ "//third_party/abseil-cpp/absl/memory" ]
+  absl_deps = [
+    "//third_party/abseil-cpp/absl/memory",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
 }
 
 if (rtc_include_tests) {
diff --git a/test/network/cross_traffic_unittest.cc b/test/network/cross_traffic_unittest.cc
index c8191a3..ac45e11 100644
--- a/test/network/cross_traffic_unittest.cc
+++ b/test/network/cross_traffic_unittest.cc
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "absl/types/optional.h"
 #include "api/test/network_emulation_manager.h"
 #include "api/test/simulated_network.h"
 #include "call/simulated_network.h"
@@ -50,6 +51,7 @@
   TaskQueueForTest task_queue_;
   EmulatedEndpointImpl endpoint{
       /*id=*/1,
+      absl::nullopt,
       rtc::IPAddress(kTestIpAddress),
       EmulatedEndpointConfig::StatsGatheringMode::kDefault,
       /*is_enabled=*/true,
diff --git a/test/network/network_emulation.cc b/test/network/network_emulation.cc
index bf6c068..ab1c0c4 100644
--- a/test/network/network_emulation.cc
+++ b/test/network/network_emulation.cc
@@ -14,6 +14,7 @@
 #include <limits>
 #include <memory>
 
+#include "absl/types/optional.h"
 #include "api/numerics/samples_stats_counter.h"
 #include "api/units/data_size.h"
 #include "rtc_base/bind.h"
@@ -417,6 +418,7 @@
 
 EmulatedEndpointImpl::EmulatedEndpointImpl(
     uint64_t id,
+    absl::optional<std::string> name,
     const rtc::IPAddress& ip,
     EmulatedEndpointConfig::StatsGatheringMode stats_gathering_mode,
     bool is_enabled,
@@ -424,6 +426,7 @@
     rtc::TaskQueue* task_queue,
     Clock* clock)
     : id_(id),
+      log_name_(ip.ToString() + " (" + name.value_or("") + ")"),
       peer_local_addr_(ip),
       stats_gathering_mode_(stats_gathering_mode),
       is_enabled_(is_enabled),
@@ -449,6 +452,7 @@
   network_->AddIP(ip);
 
   enabled_state_checker_.Detach();
+  RTC_LOG(INFO) << "Created emulated endpoint " << log_name_ << "; id=" << id_;
 }
 EmulatedEndpointImpl::~EmulatedEndpointImpl() = default;
 
@@ -496,15 +500,15 @@
     }
   }
   RTC_CHECK(port != 0) << "Can't find free port for receiver in endpoint "
-                       << id_;
+                       << log_name_ << "; id=" << id_;
   bool result = port_to_receiver_.insert({port, receiver}).second;
   if (!result) {
     RTC_LOG(INFO) << "Can't bind receiver to used port " << desired_port
-                  << " in endpoint " << id_;
+                  << " in endpoint " << log_name_ << "; id=" << id_;
     return absl::nullopt;
   }
-  RTC_LOG(INFO) << "New receiver is binded to endpoint " << id_ << " on port "
-                << port;
+  RTC_LOG(INFO) << "New receiver is binded to endpoint " << log_name_
+                << "; id=" << id_ << " on port " << port;
   return port;
 }
 
@@ -542,8 +546,8 @@
     // It can happen, that remote peer closed connection, but there still some
     // packets, that are going to it. It can happen during peer connection close
     // process: one peer closed connection, second still sending data.
-    RTC_LOG(INFO) << "Drop packet: no receiver registered in " << id_
-                  << " on port " << packet.to.port();
+    RTC_LOG(INFO) << "Drop packet: no receiver registered in " << log_name_
+                  << "; id=" << id_ << " on port " << packet.to.port();
     stats_builder_.OnPacketDropped(packet.from.ipaddr(),
                                    DataSize::Bytes(packet.ip_packet_size()),
                                    stats_gathering_mode_);
diff --git a/test/network/network_emulation.h b/test/network/network_emulation.h
index c4d7966..84872cb 100644
--- a/test/network/network_emulation.h
+++ b/test/network/network_emulation.h
@@ -484,6 +484,7 @@
  public:
   EmulatedEndpointImpl(
       uint64_t id,
+      absl::optional<std::string> name,
       const rtc::IPAddress& ip,
       EmulatedEndpointConfig::StatsGatheringMode stats_gathering_mode,
       bool is_enabled,
@@ -527,6 +528,7 @@
   rtc::ThreadChecker enabled_state_checker_;
 
   const uint64_t id_;
+  const std::string log_name_;
   // Peer's local IP address for this endpoint network interface.
   const rtc::IPAddress peer_local_addr_;
   const EmulatedEndpointConfig::StatsGatheringMode stats_gathering_mode_;
diff --git a/test/network/network_emulation_manager.cc b/test/network/network_emulation_manager.cc
index 9ffe9e3..93bcda1 100644
--- a/test/network/network_emulation_manager.cc
+++ b/test/network/network_emulation_manager.cc
@@ -106,7 +106,7 @@
   bool res = used_ip_addresses_.insert(*ip).second;
   RTC_CHECK(res) << "IP=" << ip->ToString() << " already in use";
   auto node = std::make_unique<EmulatedEndpointImpl>(
-      next_node_id_++, *ip, config.stats_gathering_mode,
+      next_node_id_++, config.name, *ip, config.stats_gathering_mode,
       config.start_as_enabled, config.type, &task_queue_, clock_);
   EmulatedEndpoint* out = node.get();
   endpoints_.push_back(std::move(node));