RateLog

Struct RateLog 

Source
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

Source

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)));
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.