@@ -125,6 +125,11 @@ pub struct BenchmarkCommand {
125125 /// `cpu_affinity` in the `sightglass-recorder` crate for more information.
126126 #[ structopt( long) ]
127127 pin : bool ,
128+
129+ /// Keep log files after successful benchmark runs. By default, logs are
130+ /// only kept on failures.
131+ #[ structopt( short = "k" , long = "keep-logs" ) ]
132+ keep_logs : bool ,
128133}
129134
130135impl BenchmarkCommand {
@@ -190,10 +195,15 @@ impl BenchmarkCommand {
190195 wasm_file. hash ( & mut hasher) ;
191196 hasher. finish ( )
192197 } ;
193- let stdout = format ! ( "stdout-{:x}-{}.log" , wasm_hash, std:: process:: id( ) ) ;
194- let stdout = Path :: new ( & stdout) ;
195- let stderr = format ! ( "stderr-{:x}-{}.log" , wasm_hash, std:: process:: id( ) ) ;
196- let stderr = Path :: new ( & stderr) ;
198+
199+ // Create log files in temp directory
200+ let log_dir = std:: env:: temp_dir ( ) . join ( "sightglass-logs" ) ;
201+ std:: fs:: create_dir_all ( & log_dir) . context ( "Failed to create log directory" ) ?;
202+
203+ let stdout =
204+ log_dir. join ( format ! ( "stdout-{wasm_hash:x}-{}.log" , std:: process:: id( ) ) ) ;
205+ let stderr =
206+ log_dir. join ( format ! ( "stderr-{wasm_hash:x}-{}.log" , std:: process:: id( ) ) ) ;
197207 let stdin = None ;
198208
199209 let mut measurements = Measurements :: new ( this_arch ( ) , engine, wasm_file) ;
@@ -209,8 +219,8 @@ impl BenchmarkCommand {
209219 let engine = Engine :: new (
210220 & mut bench_api,
211221 & working_dir,
212- stdout,
213- stderr,
222+ & stdout,
223+ & stderr,
214224 stdin,
215225 & mut measurements,
216226 & mut measure,
@@ -248,7 +258,31 @@ impl BenchmarkCommand {
248258 }
249259 }
250260
251- self . check_output ( Path :: new ( wasm_file) , stdout, stderr) ?;
261+ let check_result = self . check_output ( Path :: new ( wasm_file) , & stdout, & stderr) ;
262+
263+ // Handle log cleanup based on success/failure and --keep-logs flag
264+ match check_result {
265+ Ok ( ( ) ) => {
266+ if !self . keep_logs {
267+ let _ = fs:: remove_file ( & stdout) ;
268+ let _ = fs:: remove_file ( & stderr) ;
269+ } else {
270+ log:: info!(
271+ "Kept log files: {} and {}" ,
272+ stdout. display( ) ,
273+ stderr. display( )
274+ ) ;
275+ }
276+ }
277+ Err ( e) => {
278+ // Failure: keep logs and inform user
279+ eprintln ! ( "Benchmark output check failed. Log files preserved:" ) ;
280+ eprintln ! ( " stdout: {}" , stdout. display( ) ) ;
281+ eprintln ! ( " stderr: {}" , stderr. display( ) ) ;
282+ return Err ( e) ;
283+ }
284+ }
285+
252286 engine
253287 . as_mut ( )
254288 . map ( |e| e. measurements ( ) )
@@ -377,6 +411,10 @@ impl BenchmarkCommand {
377411 command. arg ( "--pin" ) ;
378412 }
379413
414+ if self . keep_logs {
415+ command. arg ( "--keep-logs" ) ;
416+ }
417+
380418 if self . small_workloads {
381419 command. env ( "WASM_BENCH_USE_SMALL_WORKLOAD" , "1" ) ;
382420 }
0 commit comments