flowcode-core 0.4.1-alpha

Core execution engine for FlowCode data scripting language
Documentation
// Logging functionality
use std::collections::VecDeque;

pub struct Logger {
    logs: VecDeque<String>,
    pub capacity: usize,
}

impl Logger {
    pub fn new(capacity: usize) -> Self {
        // If capacity is 0, it means effectively no logging or no limit based on interpretation.
        // For practical purposes, a VecDeque with capacity 0 might not be useful for storing logs.
        // Let's ensure a minimum capacity if we always want to store at least one log, or handle 0 as no limit/no logs.
        // For now, we'll trust the capacity provided. If 0, logs will just be pushed and popped immediately if logic is strict.
        // A common approach: if capacity is 0, it might mean 'unlimited' (though we're implementing a cap).
        // Or, if capacity is 0, no logs are stored. Let's assume capacity > 0 for actual logging.
        Logger { 
            logs: VecDeque::with_capacity(if capacity == 0 { 1 } else { capacity }), // Ensure VecDeque capacity is at least 1 if logging happens
            capacity 
        }
    }

    // Adds a log entry
    pub fn log_action(&mut self, action_details: String) {
        if self.capacity == 0 {
            return; // If capacity is 0, don't store any logs.
        }
        if self.logs.len() == self.capacity {
            self.logs.pop_front(); // Remove the oldest log
        }
        self.logs.push_back(action_details); // Add the new log
    }

    // Retrieves all stored logs
    // Returns a clone of the logs to avoid ownership issues with the caller
    pub fn get_logs(&self) -> Vec<String> {
        self.logs.iter().cloned().collect() // Convert VecDeque to Vec for output
    }

    // Clears all logs. Primarily intended for testing or reset functionality.
    pub fn clear_logs(&mut self) {
        self.logs.clear();
    }
}