Skip to content

Commit 8f6e850

Browse files
committed
feat: 优化 QEMU 输出处理逻辑,改用字节流读取并检查输出行
1 parent 8fd2706 commit 8f6e850

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

ostool/src/run/qemu.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
ffi::OsString,
3-
io::{BufRead, BufReader},
3+
io::{BufReader, Read},
44
path::PathBuf,
55
process::{Child, Stdio},
66
};
@@ -178,15 +178,27 @@ impl QemuRunner {
178178
let mut qemu_result: Option<anyhow::Result<()>> = None;
179179

180180
let stdout = BufReader::new(child.stdout.take().unwrap());
181-
for line in stdout.lines() {
182-
let line = match line {
183-
Ok(l) => l,
181+
let mut line_buf = Vec::new();
182+
183+
for byte in stdout.bytes() {
184+
let byte = match byte {
185+
Ok(b) => b,
184186
Err(e) => {
185187
println!("stdout: {:?}", e);
186188
continue;
187189
}
188190
};
189-
self.on_qemu_output(&line, &mut child, &mut qemu_result)?;
191+
let _ = std::io::stdout().write_all(&[byte]);
192+
let _ = std::io::stdout().flush();
193+
194+
line_buf.push(byte);
195+
if byte != b'\n' {
196+
continue;
197+
}
198+
199+
let line = String::from_utf8_lossy(&line_buf).to_string();
200+
201+
self.check_output(&line, &mut child, &mut qemu_result)?;
190202
}
191203

192204
let out = child.wait_with_output()?;
@@ -246,17 +258,17 @@ impl QemuRunner {
246258
Ok(bios_path)
247259
}
248260

249-
fn on_qemu_output(
261+
fn check_output(
250262
&self,
251-
line: &str,
263+
out: &str,
252264
child: &mut Child,
253265
res: &mut Option<anyhow::Result<()>>,
254266
) -> anyhow::Result<()> {
255-
// Process QEMU output line here
256-
println!("{}", line);
267+
// // Process QEMU output line here
268+
// println!("{}", line);
257269

258270
for regex in &self.fail_regex {
259-
if regex.is_match(line) {
271+
if regex.is_match(out) {
260272
*res = Some(Err(anyhow!(
261273
"Detected failure pattern '{}' in QEMU output.",
262274
regex.as_str()
@@ -268,7 +280,7 @@ impl QemuRunner {
268280
}
269281

270282
for regex in &self.success_regex {
271-
if regex.is_match(line) {
283+
if regex.is_match(out) {
272284
*res = Some(Ok(()));
273285
println!(
274286
"{}",

0 commit comments

Comments
 (0)