Debian Codegraph @

src/log.rs

raw symbols SCIP
1 use std::fmt;
2
3 pub const LOG_EMERG: i32 = 0;
4 pub const LOG_ALERT: i32 = 1;
5 pub const LOG_CRIT: i32 = 2;
6 pub const LOG_ERR: i32 = 3;
7 pub const LOG_WARNING: i32 = 4;
8 pub const LOG_NOTICE: i32 = 5;
9 pub const LOG_INFO: i32 = 6;
10 pub const LOG_DEBUG: i32 = 7;
11
12 #[repr(i32)]
13 #[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
14 pub enum LogPriority {
15 /// System is untenable
16 Emergency = LOG_EMERG,
17 /// Action must be taken immediately
18 Alert = LOG_ALERT,
19 /// Critical conditions
20 Critical = LOG_CRIT,
21 /// Error conditions
22 Error = LOG_ERR,
23 /// Warning conditions
24 Warning = LOG_WARNING,
25 /// Normal but significant condition
26 Notice = LOG_NOTICE,
27 /// Informational
28 #[default]
29 Info = LOG_INFO,
30 /// Debug-level messages
31 Debug = LOG_DEBUG,
32 }
33
34 impl LogPriority {
35 /// Creates a new [LogPriority].
36 pub const fn new() -> Self {
37 Self::Info
38 }
39 }
40
41 impl From<i32> for LogPriority {
42 fn from(val: i32) -> Self {
43 match val {
44 LOG_EMERG => Self::Emergency,
45 LOG_ALERT => Self::Alert,
46 LOG_CRIT => Self::Critical,
47 LOG_ERR => Self::Error,
48 LOG_WARNING => Self::Warning,
49 LOG_NOTICE => Self::Notice,
50 LOG_INFO => Self::Info,
51 LOG_DEBUG => Self::Debug,
52 _ => Self::Info,
53 }
54 }
55 }
56
57 impl From<&LogPriority> for i32 {
58 fn from(val: &LogPriority) -> Self {
59 (*val).into()
60 }
61 }
62
63 impl From<LogPriority> for i32 {
64 fn from(val: LogPriority) -> Self {
65 val as i32
66 }
67 }
68
69 impl From<&LogPriority> for &'static str {
70 fn from(val: &LogPriority) -> Self {
71 match val {
72 LogPriority::Emergency => "EMERG",
73 LogPriority::Alert => "ALERT",
74 LogPriority::Critical => "CRITICAL",
75 LogPriority::Error => "ERR",
76 LogPriority::Warning => "WARNING",
77 LogPriority::Notice => "NOTICE",
78 LogPriority::Info => "INFO",
79 LogPriority::Debug => "DEBUG",
80 }
81 }
82 }
83
84 impl From<LogPriority> for &'static str {
85 fn from(val: LogPriority) -> Self {
86 (&val).into()
87 }
88 }
89
90 impl fmt::Display for LogPriority {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 write!(f, "{}", <&str>::from(self))
93 }
94 }
95
96 #[cfg(test)]
97 mod tests {
98 use super::*;
99
100 #[test]
101 fn test_log_priority() {
102 assert_eq!(LogPriority::from(LOG_EMERG), LogPriority::Emergency);
103 assert_eq!(LogPriority::from(LOG_ALERT), LogPriority::Alert);
104 assert_eq!(LogPriority::from(LOG_CRIT), LogPriority::Critical);
105 assert_eq!(LogPriority::from(LOG_ERR), LogPriority::Error);
106 assert_eq!(LogPriority::from(LOG_WARNING), LogPriority::Warning);
107 assert_eq!(LogPriority::from(LOG_NOTICE), LogPriority::Notice);
108 assert_eq!(LogPriority::from(LOG_INFO), LogPriority::Info);
109 assert_eq!(LogPriority::from(LOG_DEBUG), LogPriority::Debug);
110
111 assert_eq!(i32::from(LogPriority::Emergency), LOG_EMERG);
112 assert_eq!(i32::from(LogPriority::Alert), LOG_ALERT);
113 assert_eq!(i32::from(LogPriority::Critical), LOG_CRIT);
114 assert_eq!(i32::from(LogPriority::Error), LOG_ERR);
115 assert_eq!(i32::from(LogPriority::Warning), LOG_WARNING);
116 assert_eq!(i32::from(LogPriority::Notice), LOG_NOTICE);
117 assert_eq!(i32::from(LogPriority::Info), LOG_INFO);
118 assert_eq!(i32::from(LogPriority::Debug), LOG_DEBUG);
119
120 assert_eq!(LogPriority::new(), LogPriority::default());
121 }
122 }