@@ -22,6 +22,7 @@ import (
22
22
"io/ioutil"
23
23
"math/big"
24
24
"os"
25
+ "path"
25
26
26
27
"github.com/ethereum/go-ethereum/common"
27
28
"github.com/ethereum/go-ethereum/core"
@@ -75,11 +76,22 @@ func Main(ctx *cli.Context) error {
75
76
log .Root ().SetHandler (glogger )
76
77
77
78
var (
78
- err error
79
- tracer vm.Tracer
79
+ err error
80
+ tracer vm.Tracer
81
+ baseDir = ""
80
82
)
81
- var getTracer func (txIndex int ) (vm.Tracer , error )
83
+ var getTracer func (txIndex int , txHash common. Hash ) (vm.Tracer , error )
82
84
85
+ // If user specified a basedir, make sure it exists
86
+ if ctx .IsSet (OutputBasedir .Name ) {
87
+ if base := ctx .String (OutputBasedir .Name ); len (base ) > 0 {
88
+ err := os .MkdirAll (base , 0755 ) // //rw-r--r--
89
+ if err != nil {
90
+ return NewError (ErrorIO , fmt .Errorf ("failed creating output basedir: %v" , err ))
91
+ }
92
+ baseDir = base
93
+ }
94
+ }
83
95
if ctx .Bool (TraceFlag .Name ) {
84
96
// Configure the EVM logger
85
97
logConfig := & vm.LogConfig {
@@ -95,19 +107,19 @@ func Main(ctx *cli.Context) error {
95
107
prevFile .Close ()
96
108
}
97
109
}()
98
- getTracer = func (txIndex int ) (vm.Tracer , error ) {
110
+ getTracer = func (txIndex int , txHash common. Hash ) (vm.Tracer , error ) {
99
111
if prevFile != nil {
100
112
prevFile .Close ()
101
113
}
102
- traceFile , err := os .Create (fmt .Sprintf ("trace-%d.jsonl" , txIndex ))
114
+ traceFile , err := os .Create (path . Join ( baseDir , fmt .Sprintf ("trace-%d-%v .jsonl" , txIndex , txHash . String ()) ))
103
115
if err != nil {
104
116
return nil , NewError (ErrorIO , fmt .Errorf ("failed creating trace-file: %v" , err ))
105
117
}
106
118
prevFile = traceFile
107
119
return vm .NewJSONLogger (logConfig , traceFile ), nil
108
120
}
109
121
} else {
110
- getTracer = func (txIndex int ) (tracer vm.Tracer , err error ) {
122
+ getTracer = func (txIndex int , txHash common. Hash ) (tracer vm.Tracer , err error ) {
111
123
return nil , nil
112
124
}
113
125
}
@@ -197,7 +209,7 @@ func Main(ctx *cli.Context) error {
197
209
//postAlloc := state.DumpGenesisFormat(false, false, false)
198
210
collector := make (Alloc )
199
211
state .DumpToCollector (collector , false , false , false , nil , - 1 )
200
- return dispatchOutput (ctx , result , collector )
212
+ return dispatchOutput (ctx , baseDir , result , collector )
201
213
202
214
}
203
215
@@ -224,39 +236,39 @@ func (g Alloc) OnAccount(addr common.Address, dumpAccount state.DumpAccount) {
224
236
}
225
237
226
238
// saveFile marshalls the object to the given file
227
- func saveFile (filename string , data interface {}) error {
239
+ func saveFile (baseDir , filename string , data interface {}) error {
228
240
b , err := json .MarshalIndent (data , "" , " " )
229
241
if err != nil {
230
242
return NewError (ErrorJson , fmt .Errorf ("failed marshalling output: %v" , err ))
231
243
}
232
- if err = ioutil .WriteFile (filename , b , 0644 ); err != nil {
244
+ if err = ioutil .WriteFile (path . Join ( baseDir , filename ) , b , 0644 ); err != nil {
233
245
return NewError (ErrorIO , fmt .Errorf ("failed writing output: %v" , err ))
234
246
}
235
247
return nil
236
248
}
237
249
238
250
// dispatchOutput writes the output data to either stderr or stdout, or to the specified
239
251
// files
240
- func dispatchOutput (ctx * cli.Context , result * ExecutionResult , alloc Alloc ) error {
252
+ func dispatchOutput (ctx * cli.Context , baseDir string , result * ExecutionResult , alloc Alloc ) error {
241
253
stdOutObject := make (map [string ]interface {})
242
254
stdErrObject := make (map [string ]interface {})
243
- dispatch := func (fName , name string , obj interface {}) error {
255
+ dispatch := func (baseDir , fName , name string , obj interface {}) error {
244
256
switch fName {
245
257
case "stdout" :
246
258
stdOutObject [name ] = obj
247
259
case "stderr" :
248
260
stdErrObject [name ] = obj
249
261
default : // save to file
250
- if err := saveFile (fName , obj ); err != nil {
262
+ if err := saveFile (baseDir , fName , obj ); err != nil {
251
263
return err
252
264
}
253
265
}
254
266
return nil
255
267
}
256
- if err := dispatch (ctx .String (OutputAllocFlag .Name ), "alloc" , alloc ); err != nil {
268
+ if err := dispatch (baseDir , ctx .String (OutputAllocFlag .Name ), "alloc" , alloc ); err != nil {
257
269
return err
258
270
}
259
- if err := dispatch (ctx .String (OutputResultFlag .Name ), "result" , result ); err != nil {
271
+ if err := dispatch (baseDir , ctx .String (OutputResultFlag .Name ), "result" , result ); err != nil {
260
272
return err
261
273
}
262
274
if len (stdOutObject ) > 0 {
0 commit comments