Skip to main content

cradle_plugin_api/
log.rs

1use std::cell::RefCell;
2
3thread_local! {
4    static PLUGIN_LOG: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
5}
6
7/// Stores a plugin log message for the cradle agent to drain later
8pub fn cradle_log(msg: &str) {
9    PLUGIN_LOG.with(|log| {
10        log.borrow_mut().push(msg.to_string());
11    });
12}
13
14/// Drains all stored logs and returns them
15pub fn drain_log() -> Vec<String> {
16    PLUGIN_LOG.with(|log| log.borrow_mut().drain(..).collect())
17}
18
19/// Helpful macro to simplify basic logging without identification
20#[macro_export]
21macro_rules! clog {
22    ($($arg:tt)*) => {
23        $crate::cradle_log(&format!($($arg)*))
24    };
25}
26
27/// Logging macro that logs with the plugin name for identification
28#[macro_export]
29macro_rules! plog {
30    ($plugin:expr, $($arg:tt)*) => {
31        $crate::cradle_log(&format!("[{}] {}", $plugin.name(), format!($($arg)*)))
32    };
33}