// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package raw

import (
	
	
	

	
	
)

// Writer emits the wire format of a trace.
//
// It may not produce a byte-for-byte compatible trace from what is
// produced by the runtime, because it may be missing extra padding
// in the LEB128 encoding that the runtime adds but isn't necessary
// when you know the data up-front.
type Writer struct {
	w     io.Writer
	buf   []byte
	v     version.Version
	specs []event.Spec
}

// NewWriter creates a new byte format writer.
func ( io.Writer,  version.Version) (*Writer, error) {
	,  := version.WriteHeader(, )
	return &Writer{w: , v: , specs: .Specs()}, 
}

// WriteEvent writes a single event to the trace wire format stream.
func ( *Writer) ( Event) error {
	// Check version.
	if .Version != .v {
		return fmt.Errorf("mismatched version between writer (go 1.%d) and event (go 1.%d)", .v, .Version)
	}

	// Write event header byte.
	.buf = append(.buf, uint8(.Ev))

	// Write out all arguments.
	 := .specs[.Ev]
	for ,  := range .Args[:len(.Args)] {
		.buf = binary.AppendUvarint(.buf, )
	}
	if .IsStack {
		 := .Args[len(.Args):]
		for  := 0;  < len(); ++ {
			.buf = binary.AppendUvarint(.buf, [])
		}
	}

	// Write out the length of the data.
	if .HasData {
		.buf = binary.AppendUvarint(.buf, uint64(len(.Data)))
	}

	// Write out varint events.
	,  := .w.Write(.buf)
	.buf = .buf[:0]
	if  != nil {
		return 
	}

	// Write out data.
	if .HasData {
		,  := .w.Write(.Data)
		return 
	}
	return nil
}