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
use std::time::Instant;
/// Starts a timer when created which logs the elapsed lifetime when it is dropped.
///
/// While this can be created and used manually, the [`proflogger_proc::profile`] macro from proflogger-proc is the more ergonomic method of use.
pub struct AutoLogger {
name: &'static str,
start: Instant,
level: log::Level,
}
impl AutoLogger {
#[must_use]
pub fn new(name: &'static str, level: log::Level) -> Self {
Self {
name,
start: Instant::now(),
level,
}
}
}
impl Drop for AutoLogger {
fn drop(&mut self) {
log::log!(
self.level,
"{}: {}",
self.name,
self.start.elapsed().as_secs_f64()
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::profile;
#[test]
fn test_macro() {
// These tests have to be tested manually for now.
// First, setting RUST_LOG=trace should show all functions print profiles.
// Setting RUST_LOG=warn should show only function2.
// Running cargo test --release should not show any.
env_logger::init();
#[profile]
fn test_profiled_function() {
let a = 0;
println!("{a}");
}
#[profile("Error")]
fn test_profiled_function2() {
let a = 0;
println!("{a}");
}
#[profile]
fn test_profiled_function3(a: i32) -> i32 {
println!("{a}");
a
}
#[profile]
pub fn test_profiled_function4<T>(a: T) -> T {
println!("0");
return a;
}
#[profile]
fn test_profiled_function5(a: usize) -> usize {
(0..a).sum()
}
test_profiled_function();
test_profiled_function2();
test_profiled_function3(0);
test_profiled_function4(0);
test_profiled_function5(1_000_000);
}
}