golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/trace/internal/raw/textwriter.go (about)

     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  
     5  // Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.
     6  
     7  //go:build go1.21
     8  
     9  package raw
    10  
    11  import (
    12  	"fmt"
    13  	"io"
    14  
    15  	"golang.org/x/exp/trace/internal/version"
    16  )
    17  
    18  // TextWriter emits the text format of a trace.
    19  type TextWriter struct {
    20  	w io.Writer
    21  	v version.Version
    22  }
    23  
    24  // NewTextWriter creates a new write for the trace text format.
    25  func NewTextWriter(w io.Writer, v version.Version) (*TextWriter, error) {
    26  	_, err := fmt.Fprintf(w, "Trace Go1.%d\n", v)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return &TextWriter{w: w, v: v}, nil
    31  }
    32  
    33  // WriteEvent writes a single event to the stream.
    34  func (w *TextWriter) WriteEvent(e Event) error {
    35  	// Check version.
    36  	if e.Version != w.v {
    37  		return fmt.Errorf("mismatched version between writer (go 1.%d) and event (go 1.%d)", w.v, e.Version)
    38  	}
    39  
    40  	// Write event.
    41  	_, err := fmt.Fprintln(w.w, e.String())
    42  	return err
    43  }