1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! # Log Hz
//! A logging library that provides macros for logging at a throttled rate.
//!
//! This crate was inspired by experience with [ROS_LOG_THROTTLE](https://docs.ros.org/en/jade/api/rosconsole/html/console_8h.html) from ROS 1.
//!
//! In robotics applications we often have loops running at fixed (often very high) rates.
//! In these loops log messages can be very useful for debugging, but can also quickly flood the log with duplicate information.
//! Throttled logging macros that prevent excess log spam can be very useful.
//!
//! ```no_run,rust
//! use log_hz::*;
//!
//! fn get_io_pin() -> Result<u8, String> {
//! Err("Your IO Device Isn't Connected!".to_string())
//! }
//!
//! fn main() {
//! loop {
//! let io_pin = get_io_pin();
//! match io_pin {
//! Ok(pin) => {
//! debug_hz!(1.0, "IO Pin State: {}", pin);
//! },
//! Err(e) => {
//! error_hz!(1.0, "Failed to get IO Pin: {}", e);
//! }
//! }
//! }
//! }
//! ```
//!
//! This crate provides the following throttled logging macros, matching their equivalents from the `log` crate:
//! [error_hz!], [warn_hz!], [info_hz!], [debug_hz!], and [trace_hz!].
//!
//! The rate is specified in Hz, and can be any expression that can be compile time cast to a `f32` with `as f32`.
//!
//! ```rust
//! use log_hz::*;
//!
//! fn main() {
//! // Rate can't be dynamic:
//! let rate = 1.0;
//! // info_hz!(rate, "Hello, world!"); // This won't compile
//!
//! // Instead, use a literal or something that can be compile time cast to a f32
//! info_hz!(1.0, "Hello, world!");
//! info_hz!(1.0f32, "Hello, world!");
//! info_hz!(1, "Hello, world!");
//!
//! const fn load_rate() -> f32 { 1.0 }
//! info_hz!(load_rate(), "Hello, world!");
//!
//! static RATE: f32 = 1.0;
//! info_hz!(RATE, "Hello, world!");
//! }
//! ```
//!
//! It also re-exports all of the `log` crate's macros and functions, so you can use them as you normally would without needing to import it separately:
//!
//! ```rust
//! // Cargo.toml only contains `log_hz = "0.1.0"`, no need to import `log` separately
//! use log_hz::*;
//!
//! fn main() {
//! // This comes from the log crate
//! error!("This is an error message");
//! // This comes from the log_hz crate
//! error_hz!(1.0, "This is an error message that will only log once per second");
//! }
//! ```
//!
//! This crate is compatible with all the amazing loggers that the `log` crate is compatible with. An abbreviated list include:
//! - [env_logger](https://crates.io/crates/env_logger) - Extremely common logger for getting started with Enviroment variable configuration using `RUST_LOG`.
//! - [simple_logger](https://crates.io/crates/simple_logger) - Good for simple logging needs, manually configured in code.
//! - [flexi_logger](https://crates.io/crates/flexi_logger) - Good for combining multiple loggers, and logging to multiple destinations.
//! - [log4rs](https://crates.io/crates/log4rs) - Rust equivalent of the popular Java logging framework, or log4cxx the C++ equivalent.
//! - [slog](https://crates.io/crates/slog) - Big logging ecosystem with a ton of power.
//! - [testing_logger](https://crates.io/crates/testing_logger) - If you want to unit test your log messages.
//!
//! You will need to initialize a logger before log messages from this crate will be visible.
//! See the documentation for the logger you are using for more information.
//!
//! Note: This crate uses `std::time::Instant` to track time, which is not available in `no_std` environments.
//! If you're interested in alternative timing backends for this crate, feel free to open an issue or PR to add them behind features.
pub use *;
/// Log a message at [Level::Error] at a throttled rate, first call will always log.
/// Log a message at [Level::Warn] at a throttled rate, first call will always log.
/// Log a message at [Level::Info] at a throttled rate, first call will always log.
/// Log a message at [Level::Debug] at a throttled rate, first call will always log.
/// Log a message at [Level::Trace] at a throttled rate, first call will always log.
/// Log a message at the specified level at a throttled rate, first call will always log.