@@ -133,6 +133,11 @@ pub struct BenchmarkCommand {
133133 /// `cpu_affinity` in the `sightglass-recorder` crate for more information.
134134 #[ structopt( long) ]
135135 pin : bool ,
136+
137+ /// Keep log files after successful benchmark runs. By default, logs are
138+ /// only kept on failures.
139+ #[ structopt( short = "k" , long = "keep-logs" ) ]
140+ keep_logs : bool ,
136141}
137142
138143impl BenchmarkCommand {
@@ -203,10 +208,15 @@ impl BenchmarkCommand {
203208 wasm_file. hash ( & mut hasher) ;
204209 hasher. finish ( )
205210 } ;
206- let stdout = format ! ( "stdout-{:x}-{}.log" , wasm_hash, std:: process:: id( ) ) ;
207- let stdout = Path :: new ( & stdout) ;
208- let stderr = format ! ( "stderr-{:x}-{}.log" , wasm_hash, std:: process:: id( ) ) ;
209- let stderr = Path :: new ( & stderr) ;
211+
212+ // Create log files in temp directory
213+ let log_dir = std:: env:: temp_dir ( ) . join ( "sightglass-logs" ) ;
214+ std:: fs:: create_dir_all ( & log_dir) . context ( "Failed to create log directory" ) ?;
215+
216+ let stdout =
217+ log_dir. join ( format ! ( "stdout-{wasm_hash:x}-{}.log" , std:: process:: id( ) ) ) ;
218+ let stderr =
219+ log_dir. join ( format ! ( "stderr-{wasm_hash:x}-{}.log" , std:: process:: id( ) ) ) ;
210220 let stdin = None ;
211221
212222 let engine = sightglass_data:: Engine {
@@ -226,8 +236,8 @@ impl BenchmarkCommand {
226236 let engine = Engine :: new (
227237 & mut bench_api,
228238 & working_dir,
229- stdout,
230- stderr,
239+ & stdout,
240+ & stderr,
231241 stdin,
232242 & mut measurements,
233243 & mut measure,
@@ -265,7 +275,31 @@ impl BenchmarkCommand {
265275 }
266276 }
267277
268- self . check_output ( Path :: new ( wasm_file) , stdout, stderr) ?;
278+ let check_result = self . check_output ( Path :: new ( wasm_file) , & stdout, & stderr) ;
279+
280+ // Handle log cleanup based on success/failure and --keep-logs flag
281+ match check_result {
282+ Ok ( ( ) ) => {
283+ if !self . keep_logs {
284+ let _ = fs:: remove_file ( & stdout) ;
285+ let _ = fs:: remove_file ( & stderr) ;
286+ } else {
287+ log:: info!(
288+ "Kept log files: {} and {}" ,
289+ stdout. display( ) ,
290+ stderr. display( )
291+ ) ;
292+ }
293+ }
294+ Err ( e) => {
295+ // Failure: keep logs and inform user
296+ eprintln ! ( "Benchmark output check failed. Log files preserved:" ) ;
297+ eprintln ! ( " stdout: {}" , stdout. display( ) ) ;
298+ eprintln ! ( " stderr: {}" , stderr. display( ) ) ;
299+ return Err ( e) ;
300+ }
301+ }
302+
269303 engine
270304 . as_mut ( )
271305 . map ( |e| e. measurements ( ) )
@@ -394,6 +428,10 @@ impl BenchmarkCommand {
394428 command. arg ( "--pin" ) ;
395429 }
396430
431+ if self . keep_logs {
432+ command. arg ( "--keep-logs" ) ;
433+ }
434+
397435 if self . small_workloads {
398436 command. env ( "WASM_BENCH_USE_SMALL_WORKLOAD" , "1" ) ;
399437 }
0 commit comments