Skip to main content

devela/sys/log/
diag.rs

1// devela/src/sys/log/diag.rs
2//
3//! Defines [`DiagLevel`], [`DiagOut`].
4//
5// FUTURE: DiagRecord
6
7#[doc = crate::_tags!(log)]
8/// The severity of a diagnostic emission.
9#[doc = crate::_doc_meta!{
10    location("sys/log", enum DiagLevel),
11    test_size_of(DiagLevel = 1|8; niche Option),
12}]
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
14#[allow(missing_docs)]
15pub enum DiagLevel {
16    Trace,
17    Debug,
18    #[doc = crate::_tags!(init)]
19    #[default]
20    Info,
21    Warn,
22    Error,
23}
24
25#[doc = crate::_tags!(log)]
26/// Emits leveled diagnostic text.
27#[doc = crate::_doc_meta!{
28    location("sys/log", trait DiagOut),
29}]
30/// This is the minimal semantic sink for diagnostics.
31/// It layers above plain text output by attaching a [`DiagLevel`]
32/// to each emitted message.
33///
34/// See also [`TextOut`][crate::TextOut] for non-leveled textual output.
35pub trait DiagOut {
36    /// The error returned when diagnostic emission fails.
37    type Error;
38
39    /// Emits `text` with the given diagnostic `level`.
40    fn diag(&mut self, level: DiagLevel, text: &str) -> Result<(), Self::Error>;
41
42    /// Emits a trace diagnostic.
43    fn trace(&mut self, text: &str) -> Result<(), Self::Error> {
44        self.diag(DiagLevel::Trace, text)
45    }
46    /// Emits a debug diagnostic.
47    fn debug(&mut self, text: &str) -> Result<(), Self::Error> {
48        self.diag(DiagLevel::Debug, text)
49    }
50    /// Emits an informational diagnostic.
51    fn info(&mut self, text: &str) -> Result<(), Self::Error> {
52        self.diag(DiagLevel::Info, text)
53    }
54    /// Emits a warning diagnostic.
55    fn warn(&mut self, text: &str) -> Result<(), Self::Error> {
56        self.diag(DiagLevel::Warn, text)
57    }
58    /// Emits an error diagnostic.
59    fn error(&mut self, text: &str) -> Result<(), Self::Error> {
60        self.diag(DiagLevel::Error, text)
61    }
62}