Add a QuicCoalescedPacket::ToString which will be used for net-log in chrome.

gfe-relnote: n/a (logging only)
PiperOrigin-RevId: 304000959
Change-Id: I989dfecb3c5c05ea86d3f2e285dd3e5a6c265b8c
diff --git a/quic/core/quic_coalesced_packet.cc b/quic/core/quic_coalesced_packet.cc
index 3dd9b24..6393f5d 100644
--- a/quic/core/quic_coalesced_packet.cc
+++ b/quic/core/quic_coalesced_packet.cc
@@ -6,6 +6,7 @@
 
 #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
 #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
+#include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h"
 
 namespace quic {
 
@@ -49,9 +50,7 @@
       QUIC_BUG << "Max packet length changes in the middle of the write path";
       return false;
     }
-    if (!encrypted_buffers_[packet.encryption_level].empty() ||
-        (packet.encryption_level == ENCRYPTION_INITIAL &&
-         initial_packet_ != nullptr)) {
+    if (ContainsPacketOfEncryptionLevel(packet.encryption_level)) {
       // Do not coalesce packets of the same encryption level.
       return false;
     }
@@ -116,4 +115,29 @@
   return true;
 }
 
+bool QuicCoalescedPacket::ContainsPacketOfEncryptionLevel(
+    EncryptionLevel level) const {
+  return !encrypted_buffers_[level].empty() ||
+         (level == ENCRYPTION_INITIAL && initial_packet_ != nullptr);
+}
+
+std::string QuicCoalescedPacket::ToString(size_t serialized_length) const {
+  // Total length and padding size.
+  std::string info = quiche::QuicheStrCat(
+      "total_length: ", serialized_length,
+      " padding_size: ", serialized_length - length_, " packets: {");
+  // Packets' encryption levels.
+  bool first_packet = true;
+  for (int8_t i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) {
+    if (ContainsPacketOfEncryptionLevel(static_cast<EncryptionLevel>(i))) {
+      info = quiche::QuicheStrCat(
+          info, first_packet ? "" : ", ",
+          EncryptionLevelToString(static_cast<EncryptionLevel>(i)));
+      first_packet = false;
+    }
+  }
+  info = quiche::QuicheStrCat(info, "}");
+  return info;
+}
+
 }  // namespace quic
diff --git a/quic/core/quic_coalesced_packet.h b/quic/core/quic_coalesced_packet.h
index f03f875..1974f77 100644
--- a/quic/core/quic_coalesced_packet.h
+++ b/quic/core/quic_coalesced_packet.h
@@ -34,6 +34,8 @@
                             size_t buffer_len,
                             size_t* length_copied) const;
 
+  std::string ToString(size_t serialized_length) const;
+
   const SerializedPacket* initial_packet() const {
     return initial_packet_.get();
   }
@@ -47,6 +49,9 @@
   QuicPacketLength max_packet_length() const { return max_packet_length_; }
 
  private:
+  // Returns true if this coalesced packet contains packet of |level|.
+  bool ContainsPacketOfEncryptionLevel(EncryptionLevel level) const;
+
   // self/peer addresses are set when trying to coalesce the first packet.
   // Packets with different self/peer addresses cannot be coalesced.
   QuicSocketAddress self_address_;
diff --git a/quic/core/quic_coalesced_packet_test.cc b/quic/core/quic_coalesced_packet_test.cc
index 6e7c32a..0a7a51e 100644
--- a/quic/core/quic_coalesced_packet_test.cc
+++ b/quic/core/quic_coalesced_packet_test.cc
@@ -15,6 +15,8 @@
 
 TEST(QuicCoalescedPacketTest, MaybeCoalescePacket) {
   QuicCoalescedPacket coalesced;
+  EXPECT_EQ("total_length: 0 padding_size: 0 packets: {}",
+            coalesced.ToString(0));
   SimpleBufferAllocator allocator;
   EXPECT_EQ(0u, coalesced.length());
   char buffer[1000];
@@ -30,6 +32,9 @@
                                             &allocator, 1500));
   EXPECT_EQ(1500u, coalesced.max_packet_length());
   EXPECT_EQ(500u, coalesced.length());
+  EXPECT_EQ(
+      "total_length: 1500 padding_size: 1000 packets: {ENCRYPTION_INITIAL}",
+      coalesced.ToString(1500));
 
   // Cannot coalesce packet of the same encryption level.
   SerializedPacket packet2(QuicPacketNumber(2), PACKET_4BYTE_PACKET_NUMBER,
@@ -45,6 +50,10 @@
                                             &allocator, 1500));
   EXPECT_EQ(1500u, coalesced.max_packet_length());
   EXPECT_EQ(1000u, coalesced.length());
+  EXPECT_EQ(
+      "total_length: 1500 padding_size: 500 packets: {ENCRYPTION_INITIAL, "
+      "ENCRYPTION_ZERO_RTT}",
+      coalesced.ToString(1500));
 
   SerializedPacket packet4(QuicPacketNumber(4), PACKET_4BYTE_PACKET_NUMBER,
                            buffer, 500, false, false);