log/
severity.rs

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/// The severity of a given log message.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Severity {
  /// Debug, for
  Debug,
  /// Info, for
  Info,
  /// Warning, for
  Warning,
  /// Error, for
  Error,
  /// Critical, for
  Critical,
}

impl std::fmt::Display for Severity {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Severity::Debug => "debug",
      Severity::Info => "info",
      Severity::Warning => "warning",
      Severity::Error => "error",
      Severity::Critical => "critical",
    }
    .fmt(f)
  }
}

/// A filter for log [`Severity`] levels.
#[repr(transparent)]
#[derive(Copy, Clone, Default)]
pub struct SeverityFilter(u8);

impl SeverityFilter {
  const DEBUG_BIT: u8 = (1 << 0);
  const INFO_BIT: u8 = (1 << 1);
  const WARNING_BIT: u8 = (1 << 2);
  const ERROR_BIT: u8 = (1 << 3);
  const CRITICAL_BIT: u8 = (1 << 4);
  const MASK: u8 = (1 << 5) - 1;

  /// Creates a filter that allows all [`Severity`] values.
  #[inline(always)]
  pub const fn all() -> Self {
    Self::mask(!0)
  }

  /// Creates a filter that allows no [`Severity`] values.
  #[inline(always)]
  pub const fn none() -> Self {
    Self(0)
  }

  /// Creates a filter that only allows the specified [`Severity`] `s`
  ///
  /// # Parameters
  ///
  /// * `s` - the severity.
  #[inline(always)]
  pub const fn only(s: Severity) -> Self {
    Self(Self::bit(s))
  }

  /// Constructs a filter that only allows [`Severity`] values at or above
  /// the specified severity `s`
  ///
  /// # Parameters
  ///
  /// * `s` - the severity.
  #[inline(always)]
  pub const fn above_or_eq(s: Severity) -> Self {
    Self::mask(!(Self::bit(s) - 1))
  }

  /// Constructs a filter tht allows only [`Severity`] values above the specified
  /// severity `s`
  ///
  /// # Parameters
  ///
  /// * `s` - the severity.
  #[inline(always)]
  pub const fn above(s: Severity) -> Self {
    Self::mask(!((Self::bit(s) << 1) - 1))
  }

  /// Constructs a filter that allows only [`Severity`] values at or below the
  /// specified severity `s`.
  ///
  /// # Parameters
  ///
  /// * `s` - the severity.
  #[inline(always)]
  pub const fn below_or_eq(s: Severity) -> Self {
    Self((Self::bit(s) << 1) - 1)
  }

  /// Constructs a filter that allows only [`Severity`] values below the specified
  /// severity `s`.
  ///
  /// # Parameters
  ///
  /// * `s` - the severity.
  #[inline(always)]
  pub const fn below(s: Severity) -> Self {
    Self(Self::bit(s) - 1)
  }

  /// Returns a filter that enables [`Severity::Debug`] messages.
  #[inline(always)]
  pub const fn debug(mut self) -> Self {
    self.0 |= Self::DEBUG_BIT;
    self
  }

  /// Returns a filter that enables [`Severity::Info`] messages.
  #[inline(always)]
  pub const fn info(mut self) -> Self {
    self.0 |= Self::INFO_BIT;
    self
  }

  /// Returns a filter that enables [`Severity::Warning`] messages.
  #[inline(always)]
  pub const fn warning(mut self) -> Self {
    self.0 |= Self::WARNING_BIT;
    self
  }

  /// Returns a filter that enables [`Severity::Error`] messages.
  #[inline(always)]
  pub const fn error(mut self) -> Self {
    self.0 |= Self::ERROR_BIT;
    self
  }

  /// Returns a filter that enables [`Severity::Critical`] messages.
  #[inline(always)]
  pub const fn critical(mut self) -> Self {
    self.0 |= Self::CRITICAL_BIT;
    self
  }

  /// Checks if this filter allows the specified [`Severity`] `s`.
  ///
  /// # Parameters
  ///
  /// * `s` - the severity.
  #[inline(always)]
  pub const fn allows(&self, s: Severity) -> bool {
    self.0 & Self::bit(s) != 0
  }

  /// Constructs a filter from the specified bits.
  ///
  /// # Parameters
  ///
  /// * `bits` - the bits to use.
  #[inline(always)]
  const fn mask(bits: u8) -> Self {
    Self(Self::MASK & bits)
  }

  const fn bit(s: Severity) -> u8 {
    match s {
      Severity::Critical => Self::CRITICAL_BIT,
      Severity::Error => Self::ERROR_BIT,
      Severity::Warning => Self::WARNING_BIT,
      Severity::Info => Self::INFO_BIT,
      Severity::Debug => Self::DEBUG_BIT,
    }
  }
}