Skip to main content

cradle_plugin_api/
log.rs

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