-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from Consensys/80-organisation-of-lt-binary-t…
…race-format feat: Support `lt` Binary Trace Format
- Loading branch information
Showing
11 changed files
with
660 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/consensys/go-corset/pkg/trace" | ||
"github.com/consensys/go-corset/pkg/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// traceCmd represents the trace command for manipulating traces. | ||
var traceCmd = &cobra.Command{ | ||
Use: "trace [flags] trace_file", | ||
Short: "Operate on a trace file.", | ||
Long: `Operate on a trace file, such as converting | ||
it from one format (e.g. lt) to another (e.g. json), | ||
or filtering out modules, or listing columns, etc.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
fmt.Println(cmd.UsageString()) | ||
os.Exit(1) | ||
} | ||
// Parse trace | ||
trace := readTraceFile(args[0]) | ||
list := getFlag(cmd, "list") | ||
filter := getString(cmd, "filter") | ||
output := getString(cmd, "out") | ||
// | ||
if filter != "" { | ||
trace = filterColumns(trace, filter) | ||
} | ||
if list { | ||
listColumns(trace) | ||
} | ||
// | ||
if output != "" { | ||
writeTraceFile(output, trace) | ||
} | ||
}, | ||
} | ||
|
||
// Construct a new trace containing only those columns from the original who | ||
// name begins with the given prefix. | ||
func filterColumns(tr trace.Trace, prefix string) trace.Trace { | ||
ntr := trace.EmptyArrayTrace() | ||
// | ||
for i := uint(0); i < tr.Width(); i++ { | ||
ith := tr.ColumnByIndex(i) | ||
if strings.HasPrefix(ith.Name(), prefix) { | ||
ntr.Add(ith) | ||
} | ||
} | ||
// Done | ||
return ntr | ||
} | ||
|
||
func listColumns(tr trace.Trace) { | ||
tbl := util.NewTablePrinter(3, tr.Width()) | ||
|
||
for i := uint(0); i < tr.Width(); i++ { | ||
ith := tr.ColumnByIndex(i) | ||
elems := fmt.Sprintf("%d rows", ith.Height()) | ||
bytes := fmt.Sprintf("%d bytes", ith.Width()*ith.Height()) | ||
tbl.SetRow(i, ith.Name(), elems, bytes) | ||
} | ||
|
||
// | ||
tbl.SetMaxWidth(64) | ||
tbl.Print() | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(traceCmd) | ||
traceCmd.Flags().BoolP("list", "l", false, "detail the columns in the trace file") | ||
traceCmd.Flags().StringP("out", "o", "", "Specify output file to write trace") | ||
traceCmd.Flags().StringP("filter", "f", "", "Filter columns beginning with prefix") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.