gear_node_wrapper/
log.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Log filter for the node
use anyhow::{anyhow, Result};
use smallvec::SmallVec;
use std::{
    io::{BufRead, BufReader, Read},
    process::Child,
    sync::{Arc, RwLock},
    thread,
    thread::JoinHandle,
};

const DEFAULT_LOGS_LIMIT: usize = 256;
const BLOCK_INITIALIZATION: &str = "Imported #1";

/// Log filter for the node
#[derive(Default)]
pub struct Log {
    /// Join handle of logs
    handle: Option<JoinHandle<()>>,
    /// Filtered logs from the node output
    pub logs: Arc<RwLock<SmallVec<[String; DEFAULT_LOGS_LIMIT]>>>,
}

impl Log {
    /// New log with holding limits
    pub fn new(limit: Option<usize>) -> Self {
        let mut this = Log::default();
        if let Some(limit) = limit {
            this.resize(limit);
        }

        this
    }

    /// Resize the limit logs that this instance holds
    pub fn resize(&mut self, limit: usize) {
        if let Ok(mut logs) = self.logs.write() {
            if limit > DEFAULT_LOGS_LIMIT {
                logs.grow(limit)
            } else {
                logs.reserve(limit)
            }
        }
    }

    /// Spawn logs from the child process
    pub fn spawn(&mut self, ps: &mut Child) -> Result<()> {
        let Some(stderr) = ps.stderr.take() else {
            return Err(anyhow!("Not stderr found"));
        };

        // Blocking after initialization.
        let mut reader = BufReader::new(stderr);
        for line in reader.by_ref().lines().map_while(|result| result.ok()) {
            if line.contains(BLOCK_INITIALIZATION) {
                break;
            }
        }

        // Mapping logs to memory
        let logs = Arc::clone(&self.logs);
        let handle = thread::spawn(move || {
            for line in reader.lines().map_while(|result| result.ok()) {
                if let Ok(mut logs) = logs.write() {
                    logs.push(line);
                }
            }
        });

        self.handle = Some(handle);
        Ok(())
    }
}