|
2 | 2 |
|
3 | 3 | use std::ffi::CString;
|
4 | 4 |
|
5 |
| -#[cfg(target_os = "macos")] |
6 | 5 | pub fn openlog(ident: &str, logopt: LogFlags, facility: Facility) {
|
7 | 6 | let ident = CString::new(ident).expect("TODO: handle error");
|
8 | 7 | unsafe {
|
9 | 8 | libc::openlog(ident.as_ptr(), logopt.bits(), facility as libc::c_int)
|
10 | 9 | }
|
11 | 10 | }
|
12 | 11 |
|
13 |
| -#[cfg(target_os = "macos")] |
14 |
| -pub fn syslog(priority: libc::c_int, message: &str) { |
| 12 | +pub fn syslog(priority: Priority, message: &str) { |
15 | 13 | let formatter = CString::new("%s").expect("TODO: handle error");
|
16 | 14 | let message = CString::new(message).expect("TODO: handle error");
|
17 |
| - unsafe { libc::syslog(priority, formatter.as_ptr(), message.as_ptr()) } |
| 15 | + unsafe { libc::syslog(priority.0, formatter.as_ptr(), message.as_ptr()) } |
| 16 | +} |
| 17 | + |
| 18 | +/// The priority for a log message. |
| 19 | +#[derive(Debug, Clone, Copy)] |
| 20 | +pub struct Priority(libc::c_int); |
| 21 | + |
| 22 | +impl Priority { |
| 23 | + /// Create a new priority from a severity level. |
| 24 | + pub fn from_severity(severity: Severity) -> Self { |
| 25 | + let priority = severity as libc::c_int; |
| 26 | + Priority(priority) |
| 27 | + } |
| 28 | + |
| 29 | + /// Create a new priority from a facility and severity level. |
| 30 | + pub fn from(severity: Severity, facility: Facility) -> Self { |
| 31 | + let priority = (facility as libc::c_int) | (severity as libc::c_int); |
| 32 | + Priority(priority) |
| 33 | + } |
18 | 34 | }
|
19 | 35 |
|
20 | 36 | libc_bitflags! {
|
|
0 commit comments