Skip to content

Commit c1098e4

Browse files
authored
Merge pull request #300 from posborne/no-log-clutter-by-default
Avoid cluttering tree with stdout/stderr log files
2 parents 2dffe0d + 7bc759c commit c1098e4

File tree

2 files changed

+85
-27
lines changed

2 files changed

+85
-27
lines changed

crates/cli/src/benchmark.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

138143
impl 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
}

crates/cli/src/clean.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,55 @@ pub struct CleanCommand {}
99

1010
impl CleanCommand {
1111
pub fn execute(&self) -> Result<()> {
12-
// Remove log files.
13-
let log_file_regex = Regex::new(r"^(stdout|stderr)\-\w+\-\d+-\d+.log$").unwrap();
14-
for entry in std::env::current_dir()?.read_dir()? {
15-
let entry = entry?;
12+
let log_file_regex = Regex::new(r"^(stdout|stderr)\-\w+\-\d+.log$").unwrap();
13+
let mut removed_count = 0;
1614

17-
// Only consider files, not dirs or symlinks.
18-
if !entry.file_type()?.is_file() {
15+
// Clean log files from both current directory (legacy) and temp directory (new)
16+
let dirs_to_clean = vec![
17+
std::env::current_dir()?,
18+
std::env::temp_dir().join("sightglass-logs"),
19+
];
20+
21+
for dir in dirs_to_clean {
22+
if !dir.exists() {
1923
continue;
2024
}
2125

22-
let path = entry.path();
23-
24-
// If it doesn't have a file name, it definitely isn't a log file.
25-
let name = match path.file_name().and_then(|n| n.to_str()) {
26-
None => continue,
27-
Some(n) => n,
26+
let read_dir = match dir.read_dir() {
27+
Ok(rd) => rd,
28+
Err(_) => continue,
2829
};
2930

30-
// If it doesn't match our log file regex, it isn't a log file.
31-
if !log_file_regex.is_match(name) {
32-
continue;
33-
}
31+
for entry in read_dir {
32+
let entry = entry?;
33+
34+
// Only consider files, not dirs or symlinks.
35+
if !entry.file_type()?.is_file() {
36+
continue;
37+
}
3438

35-
// Okay! It's one of our log files!
36-
log::info!("Removing log file: {}", path.display());
37-
std::fs::remove_file(&path)
38-
.with_context(|| format!("failed to remove {}", path.display()))?;
39+
let path = entry.path();
40+
41+
// If it doesn't have a file name, it definitely isn't a log file.
42+
let name = match path.file_name().and_then(|n| n.to_str()) {
43+
None => continue,
44+
Some(n) => n,
45+
};
46+
47+
// If it doesn't match our log file regex, it isn't a log file.
48+
if !log_file_regex.is_match(name) {
49+
continue;
50+
}
51+
52+
// Okay! It's one of our log files!
53+
log::info!("Removing log file: {}", path.display());
54+
std::fs::remove_file(&path)
55+
.with_context(|| format!("failed to remove {}", path.display()))?;
56+
removed_count += 1;
57+
}
3958
}
4059

60+
println!("Removed {} log file(s)", removed_count);
4161
Ok(())
4262
}
4363
}

0 commit comments

Comments
 (0)