pub struct LogGuard {
pub log_buffer: VecDeque<LogEntry>,
pub log_level: LogLevel,
pub log_limit: usize,
}Expand description
Log monitoring state.
Buffers recent Tor log messages and provides functionality to dump them when circuits are closed for debugging purposes.
§Buffer Behavior
The log buffer operates as a ring buffer with a configurable maximum size. When the buffer is full, the oldest entries are discarded to make room for new ones.
§Example
use vanguards_rs::logguard::LogGuard;
use vanguards_rs::config::LogguardConfig;
let config = LogguardConfig::default();
let mut guard = LogGuard::new(&config);
// Buffer log entries
guard.log_event("NOTICE", "Circuit 123 built");
guard.log_event("INFO", "Stream attached");
assert_eq!(guard.buffer_len(), 2);
// Dump logs before circuit close
guard.dump_log_queue("123", "Pre");
assert_eq!(guard.buffer_len(), 0);§Event Handling
The guard responds to circuit events:
- On
CLOSEDorFAILEDwith reasonREQUESTED: Dumps buffered logs - This captures context around intentional circuit closures
§See Also
LogEntry- Individual log entriescrate::config::LogguardConfig- Configuration optionscrate::logger- Main logging infrastructure
Fields§
§log_buffer: VecDeque<LogEntry>Buffered log entries.
log_level: LogLevelMinimum log level to buffer.
log_limit: usizeMaximum number of entries to buffer.
Implementations§
Source§impl LogGuard
impl LogGuard
Sourcepub fn new(config: &LogguardConfig) -> Self
pub fn new(config: &LogguardConfig) -> Self
Creates a new LogGuard with the specified configuration.
Sourcepub fn log_event(&mut self, runlevel: &str, message: &str)
pub fn log_event(&mut self, runlevel: &str, message: &str)
Handles a log event from Tor.
Buffers the log entry if it meets the minimum log level requirement. Automatically trims the buffer if it exceeds the configured limit.
§Arguments
runlevel- The log level (DEBUG, INFO, NOTICE, WARN, ERR)message- The log message content
Sourcepub fn log_event_with_timestamp(
&mut self,
runlevel: &str,
message: &str,
arrived_at: f64,
)
pub fn log_event_with_timestamp( &mut self, runlevel: &str, message: &str, arrived_at: f64, )
Handles a log event with a specific timestamp.
Sourcepub fn log_warn_event(&self, message: &str)
pub fn log_warn_event(&self, message: &str)
Handles a WARN-level log event.
Logs the warning at NOTICE level for visibility.
Sourcepub fn dump_log_queue(&mut self, circ_id: &str, when: &str)
pub fn dump_log_queue(&mut self, circ_id: &str, when: &str)
Dumps the log buffer for a circuit close event.
This is called before and after circuit close. The “when” argument is “Pre” before we close a circuit and “Post” after.
§Arguments
circ_id- The circuit ID being closedwhen- “Pre” for before close, “Post” for after close
Sourcepub fn circ_event(&mut self, circ_id: &str, status: &str, reason: Option<&str>)
pub fn circ_event(&mut self, circ_id: &str, status: &str, reason: Option<&str>)
Handles a circuit event for post-close log dumping.
Dumps buffered logs after a circuit is closed with REQUESTED reason.
§Arguments
circ_id- The circuit IDstatus- The circuit status (CLOSED, FAILED, etc.)reason- The close reason
Sourcepub fn buffer_len(&self) -> usize
pub fn buffer_len(&self) -> usize
Returns the number of buffered log entries.
Sourcepub fn get_log_event_types(dump_level: LogLevel) -> Vec<&'static str>
pub fn get_log_event_types(dump_level: LogLevel) -> Vec<&'static str>
Returns the log levels that should be subscribed to based on dump_level.
Returns a list of Tor event types to subscribe to.