Skip to main content

rivet_logger/processors/
memory_usage.rs

1use crate::logger::{BoxError, LogRecord, LogValue, Processor};
2
3use super::memory_common::{current_process_memory, format_memory, MemoryOptions};
4
5#[derive(Default)]
6pub struct MemoryUsage {
7    options: MemoryOptions,
8}
9
10impl MemoryUsage {
11    pub fn new(real_usage: bool, use_formatting: bool) -> Self {
12        Self {
13            options: MemoryOptions {
14                real_usage,
15                use_formatting,
16            },
17        }
18    }
19}
20
21impl Processor for MemoryUsage {
22    fn process(&self, mut record: LogRecord) -> Result<LogRecord, BoxError> {
23        if let Some(bytes) = current_process_memory(self.options.real_usage) {
24            let value = if self.options.use_formatting {
25                format_memory(bytes)
26            } else {
27                LogValue::U64(bytes)
28            };
29            record.extra.insert("memory_usage".to_string(), value);
30        }
31
32        Ok(record)
33    }
34}