1
2
3
4
5 package raw
6
7 import (
8 "encoding/binary"
9 "strconv"
10 "strings"
11
12 "internal/trace/event"
13 "internal/trace/version"
14 )
15
16
17
18
19
20
21 type Event struct {
22 Version version.Version
23 Ev event.Type
24 Args []uint64
25 Data []byte
26 }
27
28
29
30
31
32 func (e *Event) String() string {
33 spec := e.Version.Specs()[e.Ev]
34
35 var s strings.Builder
36 s.WriteString(spec.Name)
37 for i := range spec.Args {
38 s.WriteString(" ")
39 s.WriteString(spec.Args[i])
40 s.WriteString("=")
41 s.WriteString(strconv.FormatUint(e.Args[i], 10))
42 }
43 if spec.IsStack {
44 frames := e.Args[len(spec.Args):]
45 for i := 0; i < len(frames); i++ {
46 if i%4 == 0 {
47 s.WriteString("\n\t")
48 } else {
49 s.WriteString(" ")
50 }
51 s.WriteString(frameFields[i%4])
52 s.WriteString("=")
53 s.WriteString(strconv.FormatUint(frames[i], 10))
54 }
55 }
56 if e.Data != nil {
57 s.WriteString("\n\tdata=")
58 s.WriteString(strconv.Quote(string(e.Data)))
59 }
60 return s.String()
61 }
62
63
64 func (e *Event) EncodedSize() int {
65 size := 1
66 var buf [binary.MaxVarintLen64]byte
67 for _, arg := range e.Args {
68 size += binary.PutUvarint(buf[:], arg)
69 }
70 spec := e.Version.Specs()[e.Ev]
71 if spec.HasData {
72 size += binary.PutUvarint(buf[:], uint64(len(e.Data)))
73 size += len(e.Data)
74 }
75 return size
76 }
77
View as plain text