burn_telemetry 0.1.0

burn training logger
Documentation
//! Configuration for Rerun logger

use std::path::PathBuf;

/// Configuration for Rerun visualization
#[derive(Debug, Clone)]
pub struct RerunConfig {
    /// Application ID for Rerun
    pub application_id: String,

    /// Optional recording ID for multiple simultaneous recordings
    pub recording_id: Option<String>,

    /// Whether to spawn the Rerun viewer
    pub spawn_viewer: bool,

    /// Optional host:port to connect to (e.g., "127.0.0.1:9876")
    pub connect_addr: Option<String>,

    /// Optional path to save recording to disk
    pub save_path: Option<String>,

    /// Optional mirror path to persist the stream as an `.rrd` file while still streaming elsewhere
    pub record_to_path: Option<PathBuf>,

    /// Default timeline name for metrics
    pub default_timeline: String,
}

impl Default for RerunConfig {
    fn default() -> Self {
        Self {
            application_id: "burn-training".to_string(),
            recording_id: None,
            spawn_viewer: true,
            connect_addr: None,
            save_path: None,
            record_to_path: None,
            default_timeline: "epoch".to_string(),
        }
    }
}

impl RerunConfig {
    /// Create a new configuration with the given application ID
    pub fn new(application_id: impl Into<String>) -> Self {
        Self {
            application_id: application_id.into(),
            ..Default::default()
        }
    }

    /// Set the recording ID
    pub fn with_recording_id(mut self, recording_id: impl Into<String>) -> Self {
        self.recording_id = Some(recording_id.into());
        self
    }

    /// Set whether to spawn the viewer
    pub fn with_spawn_viewer(mut self, spawn: bool) -> Self {
        self.spawn_viewer = spawn;
        self
    }

    /// Set the connection address
    pub fn with_connect_addr(mut self, addr: impl Into<String>) -> Self {
        self.connect_addr = Some(addr.into());
        self
    }

    /// Set the save path
    pub fn with_save_path(mut self, path: impl Into<String>) -> Self {
        self.save_path = Some(path.into());
        self
    }

    /// Mirror the live stream into an `.rrd` recording while continuing to stream elsewhere.
    pub fn with_record_to_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.record_to_path = Some(path.into());
        self
    }

    /// Set the default timeline name
    pub fn with_default_timeline(mut self, timeline: impl Into<String>) -> Self {
        self.default_timeline = timeline.into();
        self
    }
}

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

    #[test]
    fn test_default_config() {
        let config = RerunConfig::default();
        assert_eq!(config.application_id, "burn-training");
        assert!(config.spawn_viewer);
        assert_eq!(config.default_timeline, "epoch");
    }

    #[test]
    fn test_builder_pattern() {
        let config = RerunConfig::new("test-app")
            .with_recording_id("test-recording")
            .with_spawn_viewer(false)
            .with_connect_addr("127.0.0.1:9876")
            .with_save_path("/tmp/recording.rrd")
            .with_default_timeline("iteration")
            .with_record_to_path("mirror.rrd");

        assert_eq!(config.application_id, "test-app");
        assert_eq!(config.recording_id, Some("test-recording".to_string()));
        assert!(!config.spawn_viewer);
        assert_eq!(config.connect_addr, Some("127.0.0.1:9876".to_string()));
        assert_eq!(config.save_path, Some("/tmp/recording.rrd".to_string()));
        assert_eq!(config.default_timeline, "iteration");
        assert_eq!(config.record_to_path, Some(PathBuf::from("mirror.rrd")));
    }
}