endpoint_libs/libs/
warn.rs1use alloc::string::{String, ToString};
2use alloc::collections::BTreeMap as HashMap;
3use tracing::warn;
4
5#[derive(Debug)]
6pub struct WarnStats {
7 pub warn_count: u64,
8}
9#[derive(Debug, Default)]
10pub struct WarnManager {
11 warns: HashMap<String, WarnStats>,
12}
13
14impl WarnManager {
15 pub fn new() -> Self {
16 Self {
17 warns: HashMap::new(),
18 }
19 }
20
21 pub fn warn(&mut self, s: impl AsRef<str>) {
22 let s = s.as_ref();
23
24 if let Some(stats) = self.warns.get_mut(s) {
25 stats.warn_count += 1;
26 } else {
27 warn!("First warning: {}", s);
28 self.warns
29 .insert(s.to_string(), WarnStats { warn_count: 1 });
30 }
31 }
32 pub fn get_warns(&self) -> &HashMap<String, WarnStats> {
33 &self.warns
34 }
35
36 #[cfg(feature = "std")]
37 pub fn dump_stats(&self, mut file: impl std::io::Write) -> std::io::Result<()> {
38 writeln!(file, "Warnings at {}:", chrono::Utc::now())?;
39 for (msg, stats) in &self.warns {
40 writeln!(file, "count={} warn={}", stats.warn_count, msg)?;
41 }
42 Ok(())
43 }
44
45 #[cfg(not(feature = "std"))]
46 pub fn dump_stats(&self, mut file: impl core::fmt::Write) -> core::fmt::Result {
47 writeln!(file, "Warnings at {}:", crate::libs::utils::get_time_milliseconds())?;
48 for (msg, stats) in &self.warns {
49 writeln!(file, "count={} warn={}", stats.warn_count, msg)?;
50 }
51 Ok(())
52 }
53}