flowcode_core/logger/
mod.rs

1// Logging functionality
2use std::collections::VecDeque;
3
4pub struct Logger {
5    logs: VecDeque<String>,
6    pub capacity: usize,
7}
8
9impl Logger {
10    pub fn new(capacity: usize) -> Self {
11        // If capacity is 0, it means effectively no logging or no limit based on interpretation.
12        // For practical purposes, a VecDeque with capacity 0 might not be useful for storing logs.
13        // Let's ensure a minimum capacity if we always want to store at least one log, or handle 0 as no limit/no logs.
14        // For now, we'll trust the capacity provided. If 0, logs will just be pushed and popped immediately if logic is strict.
15        // A common approach: if capacity is 0, it might mean 'unlimited' (though we're implementing a cap).
16        // Or, if capacity is 0, no logs are stored. Let's assume capacity > 0 for actual logging.
17        Logger { 
18            logs: VecDeque::with_capacity(if capacity == 0 { 1 } else { capacity }), // Ensure VecDeque capacity is at least 1 if logging happens
19            capacity 
20        }
21    }
22
23    // Adds a log entry
24    pub fn log_action(&mut self, action_details: String) {
25        if self.capacity == 0 {
26            return; // If capacity is 0, don't store any logs.
27        }
28        if self.logs.len() == self.capacity {
29            self.logs.pop_front(); // Remove the oldest log
30        }
31        self.logs.push_back(action_details); // Add the new log
32    }
33
34    // Retrieves all stored logs
35    // Returns a clone of the logs to avoid ownership issues with the caller
36    pub fn get_logs(&self) -> Vec<String> {
37        self.logs.iter().cloned().collect() // Convert VecDeque to Vec for output
38    }
39
40    // Clears all logs. Primarily intended for testing or reset functionality.
41    pub fn clear_logs(&mut self) {
42        self.logs.clear();
43    }
44}