gnostr_scopetime/
lib.rs

1//! simple macro to insert a scope based runtime measure that logs the
2//! result
3
4#![forbid(unsafe_code)]
5#![deny(unused_imports)]
6#![deny(clippy::unwrap_used)]
7#![deny(clippy::perf)]
8
9use std::time::Instant;
10
11pub struct ScopeTimeLog<'a> {
12	title: &'a str,
13	mod_path: &'a str,
14	file: &'a str,
15	line: u32,
16	time: Instant,
17}
18
19impl<'a> ScopeTimeLog<'a> {
20	pub fn new(
21		mod_path: &'a str,
22		title: &'a str,
23		file: &'a str,
24		line: u32,
25	) -> Self {
26		Self {
27			title,
28			mod_path,
29			file,
30			line,
31			time: Instant::now(),
32		}
33	}
34}
35
36impl<'a> Drop for ScopeTimeLog<'a> {
37	fn drop(&mut self) {
38		log::trace!(
39			"scopetime: {:?} ms [{}::{}] @{}:{}",
40			self.time.elapsed().as_millis(),
41			self.mod_path,
42			self.title,
43			self.file,
44			self.line,
45		);
46	}
47}
48
49/// measures runtime of scope and prints it into log
50#[cfg(feature = "enabled")]
51#[macro_export]
52macro_rules! scope_time {
53	($target:literal) => {
54		#[allow(unused_variables)]
55		let time = $crate::ScopeTimeLog::new(
56			module_path!(),
57			$target,
58			file!(),
59			line!(),
60		);
61	};
62}
63
64#[doc(hidden)]
65#[cfg(not(feature = "enabled"))]
66#[macro_export]
67macro_rules! scope_time {
68	($target:literal) => {};
69}