Skip to main content

vv_agent/runtime/processes/
output.rs

1use std::fs::{self, File, OpenOptions};
2use std::io::Read;
3use std::path::{Path, PathBuf};
4use std::sync::atomic::{AtomicU64, Ordering};
5
6static PROCESS_COUNTER: AtomicU64 = AtomicU64::new(1);
7
8pub(super) fn next_output_path() -> PathBuf {
9    let counter = PROCESS_COUNTER.fetch_add(1, Ordering::Relaxed);
10    std::env::temp_dir().join(format!(
11        "vv_agent_process_{}_{}.log",
12        std::process::id(),
13        counter
14    ))
15}
16
17pub(super) fn open_output_file(path: &Path) -> std::io::Result<File> {
18    OpenOptions::new()
19        .create_new(true)
20        .write(true)
21        .read(true)
22        .open(path)
23}
24
25pub fn read_captured_output(path: &Path, limit_chars: usize) -> String {
26    if limit_chars == 0 {
27        return String::new();
28    }
29    let Ok(mut file) = File::open(path) else {
30        return String::new();
31    };
32    let mut output = Vec::new();
33    if file.read_to_end(&mut output).is_err() {
34        return String::new();
35    }
36    String::from_utf8_lossy(&output)
37        .chars()
38        .take(limit_chars)
39        .collect()
40}
41
42pub fn remove_captured_output(path: &Path) {
43    let _ = fs::remove_file(path);
44}