package raw
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"internal/trace/event"
"internal/trace/version"
)
type Reader struct {
r *bufio .Reader
v version .Version
specs []event .Spec
}
func NewReader (r io .Reader ) (*Reader , error ) {
br := bufio .NewReader (r )
v , err := version .ReadHeader (br )
if err != nil {
return nil , err
}
return &Reader {r : br , v : v , specs : v .Specs ()}, nil
}
func (r *Reader ) Version () version .Version {
return r .v
}
func (r *Reader ) ReadEvent () (Event , error ) {
evb , err := r .r .ReadByte ()
if err == io .EOF {
return Event {}, io .EOF
}
if err != nil {
return Event {}, err
}
if int (evb ) >= len (r .specs ) || evb == 0 {
return Event {}, fmt .Errorf ("invalid event type: %d" , evb )
}
ev := event .Type (evb )
spec := r .specs [ev ]
args , err := r .readArgs (len (spec .Args ))
if err != nil {
return Event {}, err
}
if spec .IsStack {
len := int (args [1 ])
for i := 0 ; i < len ; i ++ {
frame , err := r .readArgs (4 )
if err != nil {
return Event {}, err
}
args = append (args , frame ...)
}
}
var data []byte
if spec .HasData {
data , err = r .readData ()
if err != nil {
return Event {}, err
}
}
return Event {
Version : r .v ,
Ev : ev ,
Args : args ,
Data : data ,
}, nil
}
func (r *Reader ) readArgs (n int ) ([]uint64 , error ) {
var args []uint64
for i := 0 ; i < n ; i ++ {
val , err := binary .ReadUvarint (r .r )
if err != nil {
return nil , err
}
args = append (args , val )
}
return args , nil
}
func (r *Reader ) readData () ([]byte , error ) {
len , err := binary .ReadUvarint (r .r )
if err != nil {
return nil , err
}
var data []byte
for i := 0 ; i < int (len ); i ++ {
b , err := r .r .ReadByte ()
if err != nil {
return nil , err
}
data = append (data , b )
}
return data , nil
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .