Skip to content

Commit 216b4a8

Browse files
committed
Make it work
Signed-off-by: tison <[email protected]>
1 parent ad6a2f3 commit 216b4a8

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ feature! {
200200
pub mod spawn;
201201
}
202202

203-
#[cfg(any(
204-
target_os = "macos",
205-
))]
203+
#[cfg(any(target_os = "macos"))]
206204
feature! {
207205
#![feature = "syslog"]
208206
pub mod syslog;

src/syslog.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ffi::CString;
2+
13
#[cfg(target_os = "macos")]
24
pub fn openlog(ident: &str, logopt: LogFlags, facility: Facility) {
35
let ident = CString::new(ident).expect("TODO: handle error");
@@ -6,8 +8,14 @@ pub fn openlog(ident: &str, logopt: LogFlags, facility: Facility) {
68
}
79
}
810

11+
#[cfg(target_os = "macos")]
12+
pub fn syslog(priority: libc::c_int, message: &str) {
13+
let formatter = CString::new("%s").expect("TODO: handle error");
14+
let message = CString::new(message).expect("TODO: handle error");
15+
unsafe { libc::syslog(priority, formatter.as_ptr(), message.as_ptr()) }
16+
}
17+
918
pub use self::consts::*;
10-
use std::ffi::CString;
1119

1220
#[cfg(target_os = "macos")]
1321
mod consts {
@@ -28,6 +36,20 @@ mod consts {
2836
}
2937
}
3038

39+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40+
#[repr(i32)]
41+
#[non_exhaustive]
42+
pub enum Severity {
43+
LOG_EMERG = libc::LOG_EMERG,
44+
LOG_ALERT = libc::LOG_ALERT,
45+
LOG_CRIT = libc::LOG_CRIT,
46+
LOG_ERR = libc::LOG_ERR,
47+
LOG_WARNING = libc::LOG_WARNING,
48+
LOG_NOTICE = libc::LOG_NOTICE,
49+
LOG_INFO = libc::LOG_INFO,
50+
LOG_DEBUG = libc::LOG_DEBUG,
51+
}
52+
3153
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
3254
#[repr(i32)]
3355
#[non_exhaustive]

test/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ mod test_sendfile;
4242
))]
4343
mod test_spawn;
4444

45+
#[cfg(any(target_os = "macos"))]
46+
mod test_syslog;
47+
4548
mod test_time;
4649
mod test_unistd;
4750

test/test_syslog.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[cfg(target_os = "macos")]
2+
#[test]
3+
fn test_syslog_hello_world() {
4+
use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity};
5+
6+
openlog(
7+
"test_syslog_hello_world",
8+
LogFlags::LOG_PID,
9+
Facility::LOG_USER,
10+
);
11+
syslog(Severity::LOG_ERR as libc::c_int, "Hello, nix!");
12+
}

0 commit comments

Comments
 (0)