Skip to content

Commit a19d932

Browse files
committed
wrap priority
Signed-off-by: tison <[email protected]>
1 parent 216911e commit a19d932

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/syslog.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22
33
use std::ffi::CString;
44

5-
#[cfg(target_os = "macos")]
65
pub fn openlog(ident: &str, logopt: LogFlags, facility: Facility) {
76
let ident = CString::new(ident).expect("TODO: handle error");
87
unsafe {
98
libc::openlog(ident.as_ptr(), logopt.bits(), facility as libc::c_int)
109
}
1110
}
1211

13-
#[cfg(target_os = "macos")]
14-
pub fn syslog(priority: libc::c_int, message: &str) {
12+
pub fn syslog(priority: Priority, message: &str) {
1513
let formatter = CString::new("%s").expect("TODO: handle error");
1614
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+
}
1834
}
1935

2036
libc_bitflags! {

0 commit comments

Comments
 (0)