Skip to content

Commit 169b5d0

Browse files
committed
polish APIs
Signed-off-by: tison <[email protected]>
1 parent 12e7bdf commit 169b5d0

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

src/syslog.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,46 @@ use std::ffi::OsStr;
1010
/// argument specifies logging options. The `facility` parameter encodes a default facility to be
1111
/// assigned to all messages that do not have an explicit facility encoded.
1212
pub fn openlog<S: AsRef<OsStr> + ?Sized>(
13-
ident: &S,
13+
ident: Option<&S>,
1414
logopt: LogFlags,
1515
facility: Facility,
1616
) -> Result<()> {
17-
let ident = OsStr::new(ident);
18-
ident.with_nix_path(|ident| unsafe {
19-
libc::openlog(ident.as_ptr(), logopt.bits(), facility as libc::c_int);
20-
})
17+
let logopt = logopt.bits();
18+
let facility = facility as libc::c_int;
19+
match ident.map(OsStr::new) {
20+
None => unsafe {
21+
libc::openlog(std::ptr::null(), logopt, facility);
22+
},
23+
Some(ident) => ident.with_nix_path(|ident| unsafe {
24+
libc::openlog(ident.as_ptr(), logopt, facility);
25+
})?,
26+
}
27+
Ok(())
2128
}
2229

2330
/// Writes message to the system message logger.
2431
///
2532
/// The message is then written to the system console, log files, logged-in users, or forwarded
2633
/// to other machines as appropriate.
27-
pub fn syslog<S: AsRef<OsStr> + ?Sized>(
28-
priority: Priority,
29-
message: &S,
30-
) -> Result<()> {
34+
///
35+
/// # Examples
36+
///
37+
/// ```rust
38+
/// use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity};
39+
///
40+
/// openlog(None, LogFlags::LOG_PID, Facility::LOG_USER).unwrap();
41+
/// syslog(Severity::LOG_EMERG, "Hello, nix!").unwrap();
42+
///
43+
/// // use `format!` to format the message
44+
/// let name = "syslog";
45+
/// syslog(Severity::LOG_EMERG, &format!("Hello, {name}!")).unwrap();
46+
/// ```
47+
pub fn syslog<P, S>(priority: P, message: &S) -> Result<()>
48+
where
49+
P: Into<Priority>,
50+
S: AsRef<OsStr> + ?Sized,
51+
{
52+
let priority = priority.into();
3153
let formatter = OsStr::new("%s");
3254
let message = OsStr::new(message);
3355
formatter.with_nix_path(|formatter| {
@@ -48,15 +70,16 @@ pub fn closelog() {
4870
pub struct Priority(libc::c_int);
4971

5072
impl Priority {
51-
/// Create a new priority from a severity level.
52-
pub fn from_severity(severity: Severity) -> Self {
53-
let priority = severity as libc::c_int;
73+
/// Create a new priority from a facility and severity level.
74+
pub fn new(severity: Severity, facility: Facility) -> Self {
75+
let priority = (facility as libc::c_int) | (severity as libc::c_int);
5476
Priority(priority)
5577
}
78+
}
5679

57-
/// Create a new priority from a facility and severity level.
58-
pub fn from(severity: Severity, facility: Facility) -> Self {
59-
let priority = (facility as libc::c_int) | (severity as libc::c_int);
80+
impl From<Severity> for Priority {
81+
fn from(severity: Severity) -> Self {
82+
let priority = severity as libc::c_int;
6083
Priority(priority)
6184
}
6285
}

test/test_syslog.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use nix::syslog::{openlog, syslog, Facility, LogFlags, Priority, Severity};
1+
use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity};
22

33
#[test]
44
fn test_syslog_hello_world() {
5-
let name = "test_syslog_hello_world";
6-
let priority = Priority::from_severity(Severity::LOG_EMERG);
7-
openlog(name, LogFlags::LOG_PID, Facility::LOG_USER).unwrap();
8-
syslog(priority, "Hello, nix!").unwrap();
5+
openlog(None, LogFlags::LOG_PID, Facility::LOG_USER).unwrap();
6+
syslog(Severity::LOG_EMERG, "Hello, nix!").unwrap();
97
}

0 commit comments

Comments
 (0)