solana_program_runtime/
log_collector.rs

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