github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/trace/v2/testdata/cmd/gotraceraw/main.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 package main 6 7 import ( 8 "flag" 9 "fmt" 10 "io" 11 "log" 12 "os" 13 14 "github.com/go-asm/go/trace/v2/raw" 15 "github.com/go-asm/go/trace/v2/version" 16 ) 17 18 func init() { 19 flag.Usage = func() { 20 fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [mode]\n", os.Args[0]) 21 fmt.Fprintf(flag.CommandLine.Output(), "\n") 22 fmt.Fprintf(flag.CommandLine.Output(), "Supported modes:") 23 fmt.Fprintf(flag.CommandLine.Output(), "\n") 24 fmt.Fprintf(flag.CommandLine.Output(), "* text2bytes - converts a text format trace to bytes\n") 25 fmt.Fprintf(flag.CommandLine.Output(), "* bytes2text - converts a byte format trace to text\n") 26 fmt.Fprintf(flag.CommandLine.Output(), "\n") 27 flag.PrintDefaults() 28 } 29 log.SetFlags(0) 30 } 31 32 func main() { 33 flag.Parse() 34 if narg := flag.NArg(); narg != 1 { 35 log.Fatal("expected exactly one positional argument: the mode to operate in; see -h output") 36 } 37 38 r := os.Stdin 39 w := os.Stdout 40 41 var tr traceReader 42 var tw traceWriter 43 var err error 44 45 switch flag.Arg(0) { 46 case "text2bytes": 47 tr, err = raw.NewTextReader(r) 48 if err != nil { 49 log.Fatal(err) 50 } 51 tw, err = raw.NewWriter(w, tr.Version()) 52 if err != nil { 53 log.Fatal(err) 54 } 55 case "bytes2text": 56 tr, err = raw.NewReader(r) 57 if err != nil { 58 log.Fatal(err) 59 } 60 tw, err = raw.NewTextWriter(w, tr.Version()) 61 if err != nil { 62 log.Fatal(err) 63 } 64 } 65 for { 66 ev, err := tr.ReadEvent() 67 if err == io.EOF { 68 break 69 } 70 if err != nil { 71 log.Fatal(err) 72 break 73 } 74 if err := tw.WriteEvent(ev); err != nil { 75 log.Fatal(err) 76 break 77 } 78 } 79 } 80 81 type traceReader interface { 82 Version() version.Version 83 ReadEvent() (raw.Event, error) 84 } 85 86 type traceWriter interface { 87 WriteEvent(raw.Event) error 88 }