Files
solana/program-runtime/src/log_collector.rs
Alessandro Decina 805d53fc10 program-runtime: add LogCollector::new_ref_with_limit() (#21691)
program-runtime: add LogCollector::new_ref_with_limit()

LogCollector::new_ref_with_limit(limit: Option<usize>) can be used to
initialize a collector with the given bytes limit. The limit can be None, in
which case logs are never truncated.

new_ref_with_limit(None) is used by cargo-run-bpf-tests so that the output of
cargo test --target=bpfel-unknown-unknown is not truncated.
2021-12-10 13:38:03 +11:00

120 lines
3.3 KiB
Rust

pub use log;
use std::{cell::RefCell, rc::Rc};
const LOG_MESSAGES_BYTES_LIMIT: usize = 10 * 1000;
pub struct LogCollector {
messages: Vec<String>,
bytes_written: usize,
bytes_limit: Option<usize>,
limit_warning: bool,
}
impl Default for LogCollector {
fn default() -> Self {
Self {
messages: Vec::new(),
bytes_written: 0,
bytes_limit: Some(LOG_MESSAGES_BYTES_LIMIT),
limit_warning: false,
}
}
}
impl LogCollector {
pub fn log(&mut self, message: &str) {
let limit = match self.bytes_limit {
Some(limit) => limit,
None => {
self.messages.push(message.to_string());
return;
}
};
let bytes_written = self.bytes_written.saturating_add(message.len());
if bytes_written >= limit {
if !self.limit_warning {
self.limit_warning = true;
self.messages.push(String::from("Log truncated"));
}
} else {
self.bytes_written = bytes_written;
self.messages.push(message.to_string());
}
}
pub fn get_recorded_content(&self) -> &[String] {
self.messages.as_slice()
}
pub fn new_ref() -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self::default()))
}
pub fn new_ref_with_limit(bytes_limit: Option<usize>) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
bytes_limit,
..Self::default()
}))
}
}
impl From<LogCollector> for Vec<String> {
fn from(log_collector: LogCollector) -> Self {
log_collector.messages
}
}
/// Convenience macro to log a message with an `Option<Rc<RefCell<LogCollector>>>`
#[macro_export]
macro_rules! ic_logger_msg {
($log_collector:expr, $message:expr) => {
$crate::log_collector::log::debug!("{}", $message);
if let Some(log_collector) = $log_collector.as_ref() {
if let Ok(mut log_collector) = log_collector.try_borrow_mut() {
log_collector.log($message);
}
}
};
($log_collector:expr, $fmt:expr, $($arg:tt)*) => {
$crate::log_collector::log::debug!($fmt, $($arg)*);
if let Some(log_collector) = $log_collector.as_ref() {
if let Ok(mut log_collector) = log_collector.try_borrow_mut() {
log_collector.log(&format!($fmt, $($arg)*));
}
}
};
}
/// Convenience macro to log a message with an `InvokeContext`
#[macro_export]
macro_rules! ic_msg {
($invoke_context:expr, $message:expr) => {
$crate::ic_logger_msg!($invoke_context.get_log_collector(), $message)
};
($invoke_context:expr, $fmt:expr, $($arg:tt)*) => {
$crate::ic_logger_msg!($invoke_context.get_log_collector(), $fmt, $($arg)*)
};
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[test]
fn test_log_messages_bytes_limit() {
let mut lc = LogCollector::default();
for _i in 0..LOG_MESSAGES_BYTES_LIMIT * 2 {
lc.log("x");
}
let logs: Vec<_> = lc.into();
assert_eq!(logs.len(), LOG_MESSAGES_BYTES_LIMIT);
for log in logs.iter().take(LOG_MESSAGES_BYTES_LIMIT - 1) {
assert_eq!(*log, "x".to_string());
}
assert_eq!(logs.last(), Some(&"Log truncated".to_string()));
}
}