pub struct RateLog { /* private fields */ }Expand description
A rate limiting logger that tracks message frequency and duration.
RateLog monitors how frequently the same message is logged and can enforce
limits based on either count (number of occurrences) or time duration.
It will output the message first time and then until the limits are exceeded.
Implementations§
Source§impl RateLog
impl RateLog
Sourcepub fn new(limit: Limit) -> Self
pub fn new(limit: Limit) -> Self
Creates a new RateLog instance with the specified limit.
The rate limiter starts with clean state - no previous messages tracked and all counters at zero.
§Arguments
limit- The rate limiting threshold to enforce
§Examples
use rate_log::{RateLog, Limit};
use std::time::Duration;
// Create count-based rate limiter
let count_limiter = RateLog::new(Limit::Rate(5));
// Create duration-based rate limiter
let time_limiter = RateLog::new(Limit::Duration(Duration::from_secs(2)));Sourcepub fn log(&mut self, msg: &str)
pub fn log(&mut self, msg: &str)
Logs a message with rate limiting applied.
This method immediately prints any new or different message to stdout, then tracks repeated messages and enforces the configured rate limit. Repeated messages are counted silently until the limit is exceeded.
§Output Behavior
- New/different message: Immediately printed to stdout and resets all counters
- Repeated message: Counted silently (no immediate output)
- Limit exceeded: Prints rate limit warning to stdout
§Rate Limiting Behavior
- Count-based: Increments counter for each repeated message
- Duration-based: Accumulates elapsed time between repeated messages
- Message change: Resets all tracking state and prints the new message
§Arguments
msg- The message to log and track for rate limiting
§Examples
use rate_log::{RateLog, Limit};
let mut logger = RateLog::new(Limit::Rate(2));
logger.log("Starting up"); // Prints: "Starting up"
logger.log("Error occurred"); // Prints: "Error occurred" (different message)
logger.log("Error occurred"); // Silent (1st repetition)
logger.log("Error occurred"); // Silent (2nd repetition)
logger.log("Error occurred"); // Prints: "Message: \"Error occurred\" repeat for 2 times in the past 15ms"
logger.log("Shutting down"); // Prints: "Shutting down" (different message)Auto Trait Implementations§
impl Freeze for RateLog
impl RefUnwindSafe for RateLog
impl Send for RateLog
impl Sync for RateLog
impl Unpin for RateLog
impl UnwindSafe for RateLog
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more