flaron-sdk 1.1.0

Official Rust SDK for writing Flaron edge flares - WebAssembly modules that run on the Flaron CDN edge runtime.
Documentation
//! Structured logging from inside a flare.
//!
//! Messages are forwarded to the edge node's `slog` stream tagged with the
//! flare name and domain. The host enforces a per-invocation cap of 100 log
//! lines and truncates each message to 4 KiB.

use crate::{ffi, mem};

/// Log an informational message.
pub fn info(msg: &str) {
    let (ptr, len) = mem::host_arg_str(msg);
    unsafe { ffi::log_info(ptr, len) }
}

/// Log a warning.
pub fn warn(msg: &str) {
    let (ptr, len) = mem::host_arg_str(msg);
    unsafe { ffi::log_warn(ptr, len) }
}

/// Log an error.
pub fn error(msg: &str) {
    let (ptr, len) = mem::host_arg_str(msg);
    unsafe { ffi::log_error(ptr, len) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ffi::test_host;

    #[test]
    fn info_records_message_at_info_level() {
        test_host::reset();
        info("hello world");
        let logs = test_host::read_mock(|m| m.logs.clone());
        assert_eq!(logs, vec![("info", "hello world".into())]);
    }

    #[test]
    fn warn_records_message_at_warn_level() {
        test_host::reset();
        warn("careful");
        let logs = test_host::read_mock(|m| m.logs.clone());
        assert_eq!(logs, vec![("warn", "careful".into())]);
    }

    #[test]
    fn error_records_message_at_error_level() {
        test_host::reset();
        error("oh no");
        let logs = test_host::read_mock(|m| m.logs.clone());
        assert_eq!(logs, vec![("error", "oh no".into())]);
    }

    #[test]
    fn multiple_logs_preserve_order() {
        test_host::reset();
        info("1");
        warn("2");
        error("3");
        info("4");
        let logs = test_host::read_mock(|m| m.logs.clone());
        assert_eq!(
            logs,
            vec![
                ("info", "1".into()),
                ("warn", "2".into()),
                ("error", "3".into()),
                ("info", "4".into()),
            ]
        );
    }
}