Files
quic-go/qlog/qlog.go
2020-02-12 10:36:27 +07:00

51 lines
1.0 KiB
Go

package qlog
import (
"io"
"github.com/francoispqt/gojay"
"github.com/lucas-clemente/quic-go/internal/protocol"
)
// A Tracer records events to be exported to a qlog.
type Tracer interface {
Export() error
}
type tracer struct {
w io.WriteCloser
odcid protocol.ConnectionID
perspective protocol.Perspective
events []event
}
var _ Tracer = &tracer{}
// NewTracer creates a new tracer to record a qlog.
func NewTracer(w io.WriteCloser, p protocol.Perspective, odcid protocol.ConnectionID) Tracer {
return &tracer{
w: w,
perspective: p,
odcid: odcid,
}
}
// Export writes a qlog.
func (t *tracer) Export() error {
enc := gojay.NewEncoder(t.w)
tl := &topLevel{
traces: traces{
{
VantagePoint: vantagePoint{Type: t.perspective},
CommonFields: commonFields{ODCID: connectionID(t.odcid), GroupID: connectionID(t.odcid)},
EventFields: eventFields[:],
Events: t.events,
},
}}
if err := enc.Encode(tl); err != nil {
return err
}
return t.w.Close()
}