log4rs_syslog_net/
consts.rs

1//! Common syslog constants.
2
3use log::Level;
4use std::io;
5
6/// The syslog `NILVALUE` constant.
7pub const NILVALUE: &str = "-";
8
9/// Facilities according to RFC 5424
10#[derive(Debug, Copy, Clone)]
11pub enum Facility {
12    /// Kernel messages
13    KERN = 0,
14    /// User-level messages
15    USER = 1 << 3,
16    /// Mail system
17    MAIL = 2 << 3,
18    /// System daemons
19    DAEMON = 3 << 3,
20    /// Security/authorization messages
21    AUTH = 4 << 3,
22    /// Messages generated internally by syslogd
23    SYSLOG = 5 << 3,
24    /// Line printer subsystem
25    LPR = 6 << 3,
26    /// Network news subsystem
27    NEWS = 7 << 3,
28    /// UUCP subsystem
29    UUCP = 8 << 3,
30    /// Clock daemon
31    CRON = 9 << 3,
32    /// Security/authorization messages
33    AUTHPRIV = 10 << 3,
34    /// FTP daemon
35    FTP = 11 << 3,
36    /// NTP subsystem
37    NTP = 12 << 3,
38    /// Log audit
39    LOGAU = 13 << 3,
40    /// Log alert
41    LOGALT = 14 << 3,
42    /// Clock daemon (note 2)
43    CRON2 = 15 << 3,
44    /// Local use 0  (local0)
45    LOCAL0 = 16 << 3,
46    /// Local use 1  (local1)
47    LOCAL1 = 17 << 3,
48    /// Local use 2  (local2)
49    LOCAL2 = 18 << 3,
50    /// Local use 3  (local3)
51    LOCAL3 = 19 << 3,
52    /// Local use 4  (local4)
53    LOCAL4 = 20 << 3,
54    /// Local use 5  (local5)
55    LOCAL5 = 21 << 3,
56    /// Local use 6  (local6)
57    LOCAL6 = 22 << 3,
58    /// Local use 7  (local7)
59    LOCAL7 = 23 << 3,
60}
61
62/// Severities as defined in RFC 3164/5424
63#[derive(Debug)]
64pub enum Severity {
65    /// Emergency: system is unusable
66    EMERGENCY = 0,
67    /// Alert: action must be taken immediately
68    ALERT = 1,
69    /// Critical: critical conditions
70    CRITICAL = 2,
71    /// Error: error conditions
72    ERROR = 3,
73    /// Warning: warning conditions
74    WARNING = 4,
75    /// Notice: normal but significant condition
76    NOTICE = 5,
77    /// Informational: informational messages
78    INFO = 6,
79    /// Debug: debug-level messages
80    DEBUG = 7,
81}
82
83/// Converts log level to syslog severity
84#[doc(hidden)]
85pub fn level_to_severity(lvl: Level) -> u8 {
86    match lvl {
87        Level::Error => Severity::ERROR as u8,
88        Level::Warn => Severity::WARNING as u8,
89        Level::Info => Severity::INFO as u8,
90        Level::Debug => Severity::DEBUG as u8,
91        Level::Trace => Severity::DEBUG as u8,
92    }
93}
94
95#[doc(hidden)]
96pub fn parse_facility(f: &str) -> Result<Facility, io::Error> {
97    let res = match f.to_lowercase().as_str() {
98        "kern" => Facility::KERN,
99        "user" => Facility::USER,
100        "mail" => Facility::MAIL,
101        "daemon" => Facility::DAEMON,
102        "auth" => Facility::AUTH,
103        "syslog" => Facility::SYSLOG,
104        "lpr" => Facility::LPR,
105        "news" => Facility::NEWS,
106        "uucp" => Facility::UUCP,
107        "cron" => Facility::CRON,
108        "authpriv" => Facility::AUTHPRIV,
109        "ftp" => Facility::FTP,
110        "ntp" => Facility::NTP,
111        "logau" => Facility::LOGAU,
112        "logalt" => Facility::LOGALT,
113        "cron2" => Facility::CRON2,
114        "local0" => Facility::LOCAL0,
115        "local1" => Facility::LOCAL1,
116        "local2" => Facility::LOCAL2,
117        "local3" => Facility::LOCAL3,
118        "local4" => Facility::LOCAL4,
119        "local5" => Facility::LOCAL5,
120        "local6" => Facility::LOCAL6,
121        "local7" => Facility::LOCAL7,
122        _ => {
123            return Err(io::Error::new(
124                io::ErrorKind::Other,
125                format!("Unsupported facility {}", f).as_str(),
126            ))
127        }
128    };
129    Ok(res)
130}