github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/qlog/trace.go (about) 1 package qlog 2 3 import ( 4 "runtime/debug" 5 "time" 6 7 "github.com/TugasAkhir-QUIC/quic-go/internal/protocol" 8 "github.com/TugasAkhir-QUIC/quic-go/logging" 9 10 "github.com/francoispqt/gojay" 11 ) 12 13 // Setting of this only works when quic-go is used as a library. 14 // When building a binary from this repository, the version can be set using the following go build flag: 15 // -ldflags="-X github.com/quic-go/quic-go/qlog.quicGoVersion=foobar" 16 var quicGoVersion = "(devel)" 17 18 func init() { 19 if quicGoVersion != "(devel)" { // variable set by ldflags 20 return 21 } 22 info, ok := debug.ReadBuildInfo() 23 if !ok { // no build info available. This happens when quic-go is not used as a library. 24 return 25 } 26 for _, d := range info.Deps { 27 if d.Path == "github.com/quic-go/quic-go" { 28 quicGoVersion = d.Version 29 if d.Replace != nil { 30 if len(d.Replace.Version) > 0 { 31 quicGoVersion = d.Version 32 } else { 33 quicGoVersion += " (replaced)" 34 } 35 } 36 break 37 } 38 } 39 } 40 41 type topLevel struct { 42 trace trace 43 } 44 45 func (topLevel) IsNil() bool { return false } 46 func (l topLevel) MarshalJSONObject(enc *gojay.Encoder) { 47 enc.StringKey("qlog_format", "NDJSON") 48 enc.StringKey("qlog_version", "draft-02") 49 enc.StringKeyOmitEmpty("title", "quic-go qlog") 50 enc.ObjectKey("configuration", configuration{Version: quicGoVersion}) 51 enc.ObjectKey("trace", l.trace) 52 } 53 54 type configuration struct { 55 Version string 56 } 57 58 func (c configuration) IsNil() bool { return false } 59 func (c configuration) MarshalJSONObject(enc *gojay.Encoder) { 60 enc.StringKey("code_version", c.Version) 61 } 62 63 type vantagePoint struct { 64 Name string 65 Type protocol.Perspective 66 } 67 68 func (p vantagePoint) IsNil() bool { return false } 69 func (p vantagePoint) MarshalJSONObject(enc *gojay.Encoder) { 70 enc.StringKeyOmitEmpty("name", p.Name) 71 switch p.Type { 72 case protocol.PerspectiveClient: 73 enc.StringKey("type", "client") 74 case protocol.PerspectiveServer: 75 enc.StringKey("type", "server") 76 } 77 } 78 79 type commonFields struct { 80 ODCID logging.ConnectionID 81 GroupID logging.ConnectionID 82 ProtocolType string 83 ReferenceTime time.Time 84 } 85 86 func (f commonFields) MarshalJSONObject(enc *gojay.Encoder) { 87 enc.StringKey("ODCID", f.ODCID.String()) 88 enc.StringKey("group_id", f.ODCID.String()) 89 enc.StringKeyOmitEmpty("protocol_type", f.ProtocolType) 90 enc.Float64Key("reference_time", float64(f.ReferenceTime.UnixNano())/1e6) 91 enc.StringKey("time_format", "relative") 92 } 93 94 func (f commonFields) IsNil() bool { return false } 95 96 type trace struct { 97 VantagePoint vantagePoint 98 CommonFields commonFields 99 } 100 101 func (trace) IsNil() bool { return false } 102 func (t trace) MarshalJSONObject(enc *gojay.Encoder) { 103 enc.ObjectKey("vantage_point", t.VantagePoint) 104 enc.ObjectKey("common_fields", t.CommonFields) 105 }