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
//! simple macro to insert a scope based runtime measure that logs the result

#![forbid(unsafe_code)]
#![forbid(missing_docs)]
#![deny(unused_imports)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::perf)]

use std::time::Instant;

///
pub struct ScopeTimeLog<'a> {
	title: &'a str,
	mod_path: &'a str,
	file: &'a str,
	line: u32,
	time: Instant,
}

///
impl<'a> ScopeTimeLog<'a> {
	///
	pub fn new(
		mod_path: &'a str,
		title: &'a str,
		file: &'a str,
		line: u32,
	) -> Self {
		Self {
			title,
			mod_path,
			file,
			line,
			time: Instant::now(),
		}
	}
}

impl<'a> Drop for ScopeTimeLog<'a> {
	fn drop(&mut self) {
		log::trace!(
			"scopetime: {:?} ms [{}::{}] @{}:{}",
			self.time.elapsed().as_millis(),
			self.mod_path,
			self.title,
			self.file,
			self.line,
		);
	}
}

///
#[cfg(feature = "enabled")]
#[macro_export]
macro_rules! scope_time {
	($target:literal) => {
		#[allow(unused_variables)]
		let time = $crate::ScopeTimeLog::new(
			module_path!(),
			$target,
			file!(),
			line!(),
		);
	};
}

#[doc(hidden)]
#[cfg(not(feature = "enabled"))]
#[macro_export]
macro_rules! scope_time {
	($target:literal) => {};
}