1// Copyright 2023 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package raw
6
7import (
8	"strconv"
9	"strings"
10
11	"internal/trace/event"
12	"internal/trace/version"
13)
14
15// Event is a simple representation of a trace event.
16//
17// Note that this typically includes much more than just
18// timestamped events, and it also represents parts of the
19// trace format's framing. (But not interpreted.)
20type Event struct {
21	Version version.Version
22	Ev      event.Type
23	Args    []uint64
24	Data    []byte
25}
26
27// String returns the canonical string representation of the event.
28//
29// This format is the same format that is parsed by the TextReader
30// and emitted by the TextWriter.
31func (e *Event) String() string {
32	spec := e.Version.Specs()[e.Ev]
33
34	var s strings.Builder
35	s.WriteString(spec.Name)
36	for i := range spec.Args {
37		s.WriteString(" ")
38		s.WriteString(spec.Args[i])
39		s.WriteString("=")
40		s.WriteString(strconv.FormatUint(e.Args[i], 10))
41	}
42	if spec.IsStack {
43		frames := e.Args[len(spec.Args):]
44		for i := 0; i < len(frames); i++ {
45			if i%4 == 0 {
46				s.WriteString("\n\t")
47			} else {
48				s.WriteString(" ")
49			}
50			s.WriteString(frameFields[i%4])
51			s.WriteString("=")
52			s.WriteString(strconv.FormatUint(frames[i], 10))
53		}
54	}
55	if e.Data != nil {
56		s.WriteString("\n\tdata=")
57		s.WriteString(strconv.Quote(string(e.Data)))
58	}
59	return s.String()
60}
61