Source file src/internal/trace/raw/textwriter.go

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

View as plain text