Skip to content

Commit

Permalink
fix: deduplicate code and fix dmesg logic
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualEhrmanntraut committed Jan 14, 2025
1 parent f45d307 commit 6a862b3
Showing 1 changed file with 53 additions and 77 deletions.
130 changes: 53 additions & 77 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,45 @@ use std::process::Command;
use std::sync::Arc;
use std::time::Duration;

fn cmd_loop(program: &str, args: &[&str], date: &str, timestamp: &str) -> ! {
let program_name = program.split("/").last().unwrap_or(program);

let mut file =
std::fs::File::create(format!("/Library/Logs/{program_name}-{timestamp}.txt")).unwrap();

let cmdline = program_name.to_owned() + " " + &args.join(" ");
let cmdline = cmdline.trim();
let header = format!(
"Command: {cmdline}\nDate: {date}\n#######################################################\n\n"
);

loop {
let Ok(output) = Command::new(program).args(args).output() else {
eprintln!("warning: failed to execute `{cmdline}` command");
continue;
};
if !output.status.success() {
eprintln!("warning: failed to execute `{cmdline}` command");
continue;
}
let buf_reader = BufReader::new(&file);
if buf_reader.buffer().len() > header.len()
&& buf_reader.buffer()[header.len()..] == output.stdout
{
continue;
}
file.seek(std::io::SeekFrom::Start(0)).unwrap();
file.write_all(header.as_bytes()).unwrap();
file.write_all(&output.stdout).unwrap();
file.sync_all().unwrap();
std::thread::sleep(Duration::from_millis(10));
}
}

fn main() {
let date = Command::new("date").arg("-Iseconds").output().unwrap();
assert!(date.status.success());
let date = String::from_utf8(date.stdout).unwrap();
let header_template = Arc::new(format!(
"Command: !CMDLINE\nDate: {}\n#######################################################\n\n",
date.trim()
));
let date = Arc::new(String::from_utf8(date.stdout).unwrap());

let timestamp = Command::new("date").arg("+%s").output().unwrap();
assert!(timestamp.status.success());
Expand All @@ -22,88 +53,33 @@ fn main() {
);

let dmesg_loop = {
let timestamp = timestamp.clone();
let header_template = header_template.clone();
let date = Arc::clone(&date);
let timestamp = Arc::clone(&timestamp);
std::thread::spawn(move || {
let mut file =
std::fs::File::create(format!("/Library/Logs/dmesg-{timestamp}.txt")).unwrap();
file.write_all(&header_template.replace("!CMDLINE", "dmesg").into_bytes())
.unwrap();
file.sync_all().unwrap();
let mut length = BufReader::new(&file).buffer().len();

loop {
let Ok(output) = Command::new("dmesg").output() else {
eprintln!("warning: failed to execute `dmesg` command");
continue;
};
file.write_all(&output.stdout[length..]).unwrap();
file.sync_all().unwrap();
length += output.stdout.len() - length;
std::thread::sleep(Duration::from_millis(10));
}
cmd_loop("dmesg", &[], &date, &timestamp);
})
};

let ioreg_loop = {
let timestamp = timestamp.clone();
let header_template = header_template.clone();

let date = Arc::clone(&date);
let timestamp = Arc::clone(&timestamp);
std::thread::spawn(move || {
let mut file =
std::fs::File::create(format!("/Library/Logs/ioreg-{timestamp}.txt")).unwrap();

loop {
let Ok(output) = Command::new("ioreg").args(["-w0", "-flx"]).output() else {
eprintln!("warning: failed to execute `ioreg -w0 -flx` command");
continue;
};
let buf_reader = BufReader::new(&file);
if buf_reader.buffer() == output.stdout {
continue;
}
file.seek(std::io::SeekFrom::Start(0)).unwrap();
file.write_all(
&header_template
.replace("!CMDLINE", "ioreg -w0 -flx")
.into_bytes(),
)
.unwrap();
file.write_all(&output.stdout).unwrap();
file.sync_all().unwrap();
std::thread::sleep(Duration::from_millis(10));
}
cmd_loop("ioreg", &["-w0", "-flx"], &date, &timestamp);
})
};

let agdcdiagnose_loop = std::thread::spawn(move || {
let mut file =
std::fs::File::create(format!("/Library/Logs/AGDCDiagnose-{timestamp}.txt")).unwrap();

loop {
let Ok(output) = Command::new(
let agdcdiagnose_loop = {
let date = Arc::clone(&date);
let timestamp = Arc::clone(&timestamp);
std::thread::spawn(move || {
cmd_loop(
"/System/Library/Extensions/AppleGraphicsControl.kext/Contents/MacOS/AGDCDiagnose",
)
.output() else {
eprintln!("warning: failed to execute `/System/Library/Extensions/AppleGraphicsControl.kext/Contents/MacOS/AGDCDiagnose` command");
continue;
};
let buf_reader = BufReader::new(&file);
if buf_reader.buffer() == output.stdout {
continue;
}
file.seek(std::io::SeekFrom::Start(0)).unwrap();
file.write_all(
&header_template
.replace("!CMDLINE", "/System/Library/Extensions/AppleGraphicsControl.kext/Contents/MacOS/AGDCDiagnose")
.into_bytes(),
)
.unwrap();
file.write_all(&output.stdout).unwrap();
file.sync_all().unwrap();
std::thread::sleep(Duration::from_millis(10));
}
});
&[],
&date,
&timestamp,
);
})
};

dmesg_loop.join().unwrap();
ioreg_loop.join().unwrap();
Expand Down

0 comments on commit 6a862b3

Please sign in to comment.