move connection logging to a separate file, absorb logutils package (#4586)

No functional change expected.
This commit is contained in:
Marten Seemann
2024-07-21 12:31:32 -06:00
committed by GitHub
parent c40d4ccb7f
commit 72092fbc29
5 changed files with 179 additions and 192 deletions

View File

@@ -1,50 +0,0 @@
package logutils
import (
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/internal/wire"
"github.com/quic-go/quic-go/logging"
)
// ConvertFrame converts a wire.Frame into a logging.Frame.
// This makes it possible for external packages to access the frames.
// Furthermore, it removes the data slices from CRYPTO and STREAM frames.
func ConvertFrame(frame wire.Frame) logging.Frame {
switch f := frame.(type) {
case *wire.AckFrame:
// We use a pool for ACK frames.
// Implementations of the tracer interface may hold on to frames, so we need to make a copy here.
return ConvertAckFrame(f)
case *wire.CryptoFrame:
return &logging.CryptoFrame{
Offset: f.Offset,
Length: protocol.ByteCount(len(f.Data)),
}
case *wire.StreamFrame:
return &logging.StreamFrame{
StreamID: f.StreamID,
Offset: f.Offset,
Length: f.DataLen(),
Fin: f.Fin,
}
case *wire.DatagramFrame:
return &logging.DatagramFrame{
Length: logging.ByteCount(len(f.Data)),
}
default:
return logging.Frame(frame)
}
}
func ConvertAckFrame(f *wire.AckFrame) *logging.AckFrame {
ranges := make([]wire.AckRange, 0, len(f.AckRanges))
ranges = append(ranges, f.AckRanges...)
ack := &logging.AckFrame{
AckRanges: ranges,
DelayTime: f.DelayTime,
ECNCE: f.ECNCE,
ECT0: f.ECT0,
ECT1: f.ECT1,
}
return ack
}

View File

@@ -1,52 +0,0 @@
package logutils
import (
"github.com/quic-go/quic-go/internal/wire"
"github.com/quic-go/quic-go/logging"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("CRYPTO frame", func() {
It("converts CRYPTO frames", func() {
f := ConvertFrame(&wire.CryptoFrame{
Offset: 1234,
Data: []byte("foobar"),
})
Expect(f).To(BeAssignableToTypeOf(&logging.CryptoFrame{}))
cf := f.(*logging.CryptoFrame)
Expect(cf.Offset).To(Equal(logging.ByteCount(1234)))
Expect(cf.Length).To(Equal(logging.ByteCount(6)))
})
It("converts STREAM frames", func() {
f := ConvertFrame(&wire.StreamFrame{
StreamID: 42,
Offset: 1234,
Data: []byte("foo"),
Fin: true,
})
Expect(f).To(BeAssignableToTypeOf(&logging.StreamFrame{}))
sf := f.(*logging.StreamFrame)
Expect(sf.StreamID).To(Equal(logging.StreamID(42)))
Expect(sf.Offset).To(Equal(logging.ByteCount(1234)))
Expect(sf.Length).To(Equal(logging.ByteCount(3)))
Expect(sf.Fin).To(BeTrue())
})
It("converts DATAGRAM frames", func() {
f := ConvertFrame(&wire.DatagramFrame{Data: []byte("foobar")})
Expect(f).To(BeAssignableToTypeOf(&logging.DatagramFrame{}))
df := f.(*logging.DatagramFrame)
Expect(df.Length).To(Equal(logging.ByteCount(6)))
})
It("converts other frames", func() {
f := ConvertFrame(&wire.MaxDataFrame{MaximumData: 1234})
Expect(f).To(BeAssignableToTypeOf(&logging.MaxDataFrame{}))
Expect(f).ToNot(BeAssignableToTypeOf(&logging.MaxStreamDataFrame{}))
mdf := f.(*logging.MaxDataFrame)
Expect(mdf.MaximumData).To(Equal(logging.ByteCount(1234)))
})
})

View File

@@ -1,13 +0,0 @@
package logutils
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestLogutils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Logutils Suite")
}