solana_log_collector/
lib.rs

1pub use log;
2use std::{cell::RefCell, rc::Rc};
3
4const LOG_MESSAGES_BYTES_LIMIT: usize = 10 * 1000;
5
6pub struct LogCollector {
7    pub messages: Vec<String>,
8    pub bytes_written: usize,
9    pub bytes_limit: Option<usize>,
10    pub limit_warning: bool,
11}
12
13impl Default for LogCollector {
14    fn default() -> Self {
15        Self {
16            messages: Vec::new(),
17            bytes_written: 0,
18            bytes_limit: Some(LOG_MESSAGES_BYTES_LIMIT),
19            limit_warning: false,
20        }
21    }
22}
23
24impl LogCollector {
25    pub fn log(&mut self, message: &str) {
26        let Some(limit) = self.bytes_limit else {
27            self.messages.push(message.to_string());
28            return;
29        };
30
31        let bytes_written = self.bytes_written.saturating_add(message.len());
32        if bytes_written >= limit {
33            if !self.limit_warning {
34                self.limit_warning = true;
35                self.messages.push(String::from("Log truncated"));
36            }
37        } else {
38            self.bytes_written = bytes_written;
39            self.messages.push(message.to_string());
40        }
41    }
42
43    pub fn get_recorded_content(&self) -> &[String] {
44        self.messages.as_slice()
45    }
46
47    pub fn new_ref() -> Rc<RefCell<Self>> {
48        Rc::new(RefCell::new(Self::default()))
49    }
50
51    pub fn new_ref_with_limit(bytes_limit: Option<usize>) -> Rc<RefCell<Self>> {
52        Rc::new(RefCell::new(Self {
53            bytes_limit,
54            ..Self::default()
55        }))
56    }
57
58    pub fn into_messages(self) -> Vec<String> {
59        self.messages
60    }
61}
62
63/// Convenience macro to log a message with an `Option<Rc<RefCell<LogCollector>>>`
64#[macro_export]
65macro_rules! ic_logger_msg {
66    ($log_collector:expr, $message:expr) => {
67        $crate::log::debug!(
68            target: "solana_runtime::message_processor::stable_log",
69            "{}",
70            $message
71        );
72        if let Some(log_collector) = $log_collector.as_ref() {
73            if let Ok(mut log_collector) = log_collector.try_borrow_mut() {
74                log_collector.log($message);
75            }
76        }
77    };
78    ($log_collector:expr, $fmt:expr, $($arg:tt)*) => {
79        $crate::log::debug!(
80            target: "solana_runtime::message_processor::stable_log",
81            $fmt,
82            $($arg)*
83        );
84        if let Some(log_collector) = $log_collector.as_ref() {
85            if let Ok(mut log_collector) = log_collector.try_borrow_mut() {
86                log_collector.log(&format!($fmt, $($arg)*));
87            }
88        }
89    };
90}
91
92/// Convenience macro to log a message with an `InvokeContext`
93#[macro_export]
94macro_rules! ic_msg {
95    ($invoke_context:expr, $message:expr) => {
96        $crate::ic_logger_msg!($invoke_context.get_log_collector(), $message)
97    };
98    ($invoke_context:expr, $fmt:expr, $($arg:tt)*) => {
99        $crate::ic_logger_msg!($invoke_context.get_log_collector(), $fmt, $($arg)*)
100    };
101}
102
103#[cfg(test)]
104pub(crate) mod tests {
105    use super::*;
106
107    #[test]
108    fn test_log_messages_bytes_limit() {
109        let mut lc = LogCollector::default();
110
111        for _i in 0..LOG_MESSAGES_BYTES_LIMIT * 2 {
112            lc.log("x");
113        }
114
115        let logs: Vec<_> = lc.into_messages();
116        assert_eq!(logs.len(), LOG_MESSAGES_BYTES_LIMIT);
117        for log in logs.iter().take(LOG_MESSAGES_BYTES_LIMIT - 1) {
118            assert_eq!(*log, "x".to_string());
119        }
120        assert_eq!(logs.last(), Some(&"Log truncated".to_string()));
121    }
122}