Skip to content

Commit fb9f726

Browse files
terasumkaralabe
authored andcommitted
cmd/evm: change error msg output to stderr (#17118)
* cmd/evm: change error msg output to stderr * cmd/evm: fix some linter error
1 parent d927cbb commit fb9f726

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

cmd/evm/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ func compileCmd(ctx *cli.Context) error {
5050
if err != nil {
5151
return err
5252
}
53-
fmt.Println(bin)
53+
fmt.Fprintln(ctx.App.Writer, bin)
5454
return nil
5555
}

cmd/evm/disasm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ func disasmCmd(ctx *cli.Context) error {
4545
}
4646

4747
code := strings.TrimSpace(string(in[:]))
48-
fmt.Printf("%v\n", code)
48+
fmt.Fprintf(ctx.App.Writer, "%v\n", code)
4949
return asm.PrintDisassembled(code)
5050
}

cmd/evm/internal/compiler/compiler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ func Compile(fn string, src []byte, debug bool) (string, error) {
3030
bin, compileErrors := compiler.Compile()
3131
if len(compileErrors) > 0 {
3232
// report errors
33+
errs := ""
3334
for _, err := range compileErrors {
34-
fmt.Printf("%s:%v\n", fn, err)
35+
errs += fmt.Sprintf("%s:%v\n", fn, err)
3536
}
36-
return "", errors.New("compiling failed")
37+
return "", errors.New(errs + "compiling failed\n")
3738
}
3839
return bin, nil
3940
}

cmd/evm/runner.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ func runCmd(ctx *cli.Context) error {
128128
if ctx.GlobalString(CodeFileFlag.Name) == "-" {
129129
//Try reading from stdin
130130
if hexcode, err = ioutil.ReadAll(os.Stdin); err != nil {
131-
fmt.Printf("Could not load code from stdin: %v\n", err)
131+
fmt.Fprintf(ctx.App.ErrWriter, "Could not load code from stdin: %v\n", err)
132132
os.Exit(1)
133133
}
134134
} else {
135135
// Codefile with hex assembly
136136
if hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name)); err != nil {
137-
fmt.Printf("Could not load code from file: %v\n", err)
137+
fmt.Fprintf(ctx.App.ErrWriter, "Could not load code from file: %v\n", err)
138138
os.Exit(1)
139139
}
140140
}
@@ -172,11 +172,11 @@ func runCmd(ctx *cli.Context) error {
172172
if cpuProfilePath := ctx.GlobalString(CPUProfileFlag.Name); cpuProfilePath != "" {
173173
f, err := os.Create(cpuProfilePath)
174174
if err != nil {
175-
fmt.Println("could not create CPU profile: ", err)
175+
fmt.Fprintf(ctx.App.ErrWriter, "could not create CPU profile: %v\n", err)
176176
os.Exit(1)
177177
}
178178
if err := pprof.StartCPUProfile(f); err != nil {
179-
fmt.Println("could not start CPU profile: ", err)
179+
fmt.Fprintf(ctx.App.ErrWriter, "could not start CPU profile: %v\n", err)
180180
os.Exit(1)
181181
}
182182
defer pprof.StopCPUProfile()
@@ -200,35 +200,35 @@ func runCmd(ctx *cli.Context) error {
200200

201201
if ctx.GlobalBool(DumpFlag.Name) {
202202
statedb.IntermediateRoot(true)
203-
fmt.Println(string(statedb.Dump()))
203+
fmt.Fprintln(ctx.App.Writer, string(statedb.Dump()))
204204
}
205205

206206
if memProfilePath := ctx.GlobalString(MemProfileFlag.Name); memProfilePath != "" {
207207
f, err := os.Create(memProfilePath)
208208
if err != nil {
209-
fmt.Println("could not create memory profile: ", err)
209+
fmt.Fprintf(ctx.App.ErrWriter, "could not create memory profile: %v\n", err)
210210
os.Exit(1)
211211
}
212212
if err := pprof.WriteHeapProfile(f); err != nil {
213-
fmt.Println("could not write memory profile: ", err)
213+
fmt.Fprintf(ctx.App.ErrWriter, "could not create memory profile: %v\n", err)
214214
os.Exit(1)
215215
}
216216
f.Close()
217217
}
218218

219219
if ctx.GlobalBool(DebugFlag.Name) {
220220
if debugLogger != nil {
221-
fmt.Fprintln(os.Stderr, "#### TRACE ####")
222-
vm.WriteTrace(os.Stderr, debugLogger.StructLogs())
221+
fmt.Fprintln(ctx.App.ErrWriter, "#### TRACE ####")
222+
vm.WriteTrace(ctx.App.ErrWriter, debugLogger.StructLogs())
223223
}
224-
fmt.Fprintln(os.Stderr, "#### LOGS ####")
225-
vm.WriteLogs(os.Stderr, statedb.Logs())
224+
fmt.Fprintln(ctx.App.ErrWriter, "#### LOGS ####")
225+
vm.WriteLogs(ctx.App.ErrWriter, statedb.Logs())
226226
}
227227

228228
if ctx.GlobalBool(StatDumpFlag.Name) {
229229
var mem goruntime.MemStats
230230
goruntime.ReadMemStats(&mem)
231-
fmt.Fprintf(os.Stderr, `evm execution time: %v
231+
fmt.Fprintf(ctx.App.ErrWriter, `evm execution time: %v
232232
heap objects: %d
233233
allocations: %d
234234
total allocations: %d
@@ -238,9 +238,9 @@ Gas used: %d
238238
`, execTime, mem.HeapObjects, mem.Alloc, mem.TotalAlloc, mem.NumGC, initialGas-leftOverGas)
239239
}
240240
if tracer == nil {
241-
fmt.Printf("0x%x\n", ret)
241+
fmt.Fprintf(ctx.App.Writer, "0x%x\n", ret)
242242
if err != nil {
243-
fmt.Printf(" error: %v\n", err)
243+
fmt.Fprintf(ctx.App.ErrWriter, " error: %v\n", err)
244244
}
245245
}
246246

cmd/evm/staterunner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ func stateTestCmd(ctx *cli.Context) error {
107107
}
108108
// print state root for evmlab tracing (already committed above, so no need to delete objects again
109109
if ctx.GlobalBool(MachineFlag.Name) && state != nil {
110-
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", state.IntermediateRoot(false))
110+
fmt.Fprintf(ctx.App.ErrWriter, "{\"stateRoot\": \"%x\"}\n", state.IntermediateRoot(false))
111111
}
112112

113113
results = append(results, *result)
114114

115115
// Print any structured logs collected
116116
if ctx.GlobalBool(DebugFlag.Name) {
117117
if debugger != nil {
118-
fmt.Fprintln(os.Stderr, "#### TRACE ####")
119-
vm.WriteTrace(os.Stderr, debugger.StructLogs())
118+
fmt.Fprintln(ctx.App.ErrWriter, "#### TRACE ####")
119+
vm.WriteTrace(ctx.App.ErrWriter, debugger.StructLogs())
120120
}
121121
}
122122
}
123123
}
124124
out, _ := json.MarshalIndent(results, "", " ")
125-
fmt.Println(string(out))
125+
fmt.Fprintln(ctx.App.Writer, string(out))
126126
return nil
127127
}

0 commit comments

Comments
 (0)