1use std::{fmt, io::ErrorKind};
18
19#[cfg(target_family = "unix")]
20use nix::errno::Errno;
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum SyslogErrCode
25{
26 OsError(ErrorKind),
28
29 #[cfg(target_family = "unix")]
31 OsErrorErrno(Errno),
32
33 #[cfg(target_family = "windows")]
34 OsErrorErrno(i32),
35
36 InternalError,
38
39 SyslogThreadNotAvailable,
42
43 UnboundedChannelError,
46
47 MutexPoisoned,
49
50 Unusable,
52
53 SendError,
55
56 NotAvail,
58
59 CoWWriteError,
63
64 CoWAlreadyLocked,
67
68 UnixNotEnoughSpace,
70}
71
72impl fmt::Display for SyslogErrCode
73{
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
75 {
76 match self
77 {
78 Self::OsError(error_kind) =>
79 write!(f, "[ErrorKind: {}]", error_kind),
80 Self::InternalError =>
81 write!(f, "[InternalError]"),
82 Self::SyslogThreadNotAvailable =>
83 write!(f, "[SyslogThreadNotAvailable]"),
84 Self::UnboundedChannelError =>
85 write!(f, "[UnboundedChannelError]"),
86 Self::MutexPoisoned =>
87 write!(f, "[MutexPoisoned]"),
88 Self::OsErrorErrno(errn) =>
89 write!(f, "[Errno: {}]", errn),
90 Self::Unusable =>
91 write!(f, "[Unusable]"),
92 Self::SendError =>
93 write!(f, "[SendError]"),
94 Self::NotAvail =>
95 write!(f, "[NotAvail]"),
96 Self::CoWWriteError =>
97 write!(f, "[CoWWriteError]"),
98 Self::CoWAlreadyLocked =>
99 write!(f, "[CoWAlreadyLocked]"),
100 Self::UnixNotEnoughSpace =>
101 write!(f, "[UnixNotEnoughSpace(ENOBUFS)]")
102 }
103 }
104}
105
106impl SyslogErrCode
107{
108 pub
109 fn get_os_err_code(&self) -> Option<ErrorKind>
110 {
111 match *self
112 {
113 Self::OsError(errn) =>
114 return Some(errn),
115 _ =>
116 return None,
117 }
118 }
119}
120
121#[derive(Debug)]
123pub struct SyslogError
124{
125 errcode: SyslogErrCode,
126 message: String,
127}
128
129impl SyslogError
130{
131 pub
133 fn new(errcode: SyslogErrCode, msg: String) -> Self
134 {
135 return SyslogError{errcode: errcode, message: msg};
136 }
137
138 pub
140 fn new_io(err: &std::io::Error, msg: String) -> Self
141 {
142 return SyslogError{errcode: SyslogErrCode::OsError(err.kind()), message: msg};
143 }
144
145 #[cfg(target_family = "unix")]
146 pub
147 fn new_errno(err: Errno, msg: String) -> Self
148 {
149 return SyslogError{errcode: SyslogErrCode::OsErrorErrno(err), message: msg};
150 }
151
152 #[cfg(target_family = "windows")]
153 pub
154 fn new_errno(err: i32, msg: String) -> Self
155 {
156 return SyslogError{errcode: SyslogErrCode::OsErrorErrno(err), message: msg};
157 }
158
159 pub
161 fn get_errcode(&self) -> SyslogErrCode
162 {
163 return self.errcode;
164 }
165
166 pub
168 fn into_inner(self) -> String
169 {
170 return self.message;
171 }
172
173 pub
175 fn get_inner(&self) -> &String
176 {
177 return &self.message;
178 }
179}
180
181impl fmt::Display for SyslogError
182{
183 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
184 {
185 write!(f, "{} {}", self.errcode, self.message)
186 }
187}
188
189pub type SyRes<T> = Result<T, SyslogError>;
190
191#[macro_export]
192macro_rules! throw_error
193{
194 ($($arg:tt)*) => (
195 return std::result::Result::Err($crate::error::SyslogError::new($crate::error::SyslogErrCode::InternalError, format!($($arg)*)))
196 )
197}
198
199#[macro_export]
200macro_rules! throw_error_os
201{
202 ($err:expr, $($arg:tt)*) => (
203 return std::result::Result::Err($crate::error::SyslogError::new_io(&$err, format!($($arg)*)))
204 )
205}
206
207#[macro_export]
208macro_rules! throw_error_errno
209{
210 ($errno:expr, $($arg:tt)*) => (
211 return std::result::Result::Err($crate::error::SyslogError::new_errno($errno, format!($($arg)*)))
212 )
213}
214
215#[macro_export]
216macro_rules! map_error
217{
218 ($($arg:tt)*) => (
219 $crate::error::SyslogError::new($crate::error::SyslogErrCode::InternalError, format!($($arg)*))
220 )
221}
222
223#[macro_export]
224macro_rules! map_error_os
225{
226 ($err:expr, $($arg:tt)*) => (
227 $crate::error::SyslogError::new_io(&$err, format!($($arg)*))
228 )
229}
230
231#[macro_export]
232macro_rules! throw_error_code
233{
234 ($code:tt, $($arg:tt)*) => (
235 return std::result::Result::Err($crate::error::SyslogError::new($crate::error::SyslogErrCode::$code, format!($($arg)*)))
236 )
237}
238
239#[macro_export]
240macro_rules! map_error_code
241{
242 ($code:tt, $($arg:tt)*) => (
243 $crate::error::SyslogError::new($crate::error::SyslogErrCode::$code, format!($($arg)*))
244 )
245}