Skip to content

Commit 9f3596e

Browse files
committed
fixup all
Signed-off-by: tison <[email protected]>
1 parent c98a454 commit 9f3596e

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

src/syslog.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
//! Interfaces for controlling system log.
22
3-
use crate::NixPath;
4-
use crate::Result;
5-
use std::ffi::OsStr;
3+
use crate::{NixPath, Result};
4+
use std::ffi::{CStr, OsStr};
5+
use std::ptr;
66

77
/// Logging options of subsequent [`syslog`] calls can be set by calling [`openlog`].
88
///
99
/// The parameter `ident` is a string that will be prepended to every message. The `logopt`
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.
12-
pub fn openlog<S>(
13-
ident: Option<&S>,
12+
pub fn openlog(
13+
ident: Option<&'static CStr>,
1414
logopt: LogFlags,
1515
facility: Facility,
16-
) -> Result<()>
17-
where
18-
S: AsRef<OsStr> + ?Sized,
19-
{
16+
) -> Result<()> {
17+
let ident = ident.map_or(ptr::null(), |ident| ident.as_ptr());
2018
let logopt = logopt.bits();
2119
let facility = facility as libc::c_int;
22-
match ident.map(OsStr::new) {
23-
None => unsafe {
24-
libc::openlog(std::ptr::null(), logopt, facility);
25-
},
26-
Some(ident) => ident.with_nix_path(|ident| unsafe {
27-
libc::openlog(ident.as_ptr(), logopt, facility);
28-
})?,
20+
unsafe {
21+
libc::openlog(ident, logopt, facility);
2922
}
3023
Ok(())
3124
}
@@ -40,12 +33,8 @@ where
4033
/// ```rust
4134
/// use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity};
4235
///
43-
/// #[cfg(not(target_os = "haiku"))]
4436
/// let flags = LogFlags::LOG_PID;
45-
/// #[cfg(target_os = "haiku")]
46-
/// let flags = LogFlags::empty();
47-
///
48-
/// openlog(None::<&str>, flags, Facility::LOG_USER).unwrap();
37+
/// openlog(None, flags, Facility::LOG_USER).unwrap();
4938
/// syslog(Severity::LOG_EMERG, "Hello, nix!").unwrap();
5039
///
5140
/// // use `format!` to format the message
@@ -97,25 +86,21 @@ libc_bitflags! {
9786
pub struct LogFlags: libc::c_int {
9887
/// Log the process id with each message: useful for identifying instantiations of
9988
/// daemons.
100-
#[cfg(not(target_os = "haiku"))]
10189
LOG_PID;
10290
/// If syslog() cannot pass the message to syslogd(8) it will attempt to write the
10391
/// message to the console ("/dev/console").
104-
#[cfg(not(target_os = "haiku"))]
10592
LOG_CONS;
10693
/// The converse of [`LOG_NDELAY`][LogFlags::LOG_NDELAY]; opening of the connection is
10794
/// delayed until `syslog` is called.
10895
///
10996
/// This is the default, and need not be specified.
110-
#[cfg(not(target_os = "haiku"))]
11197
LOG_ODELAY;
11298
/// Open the connection to syslogd(8) immediately. Normally the open is delayed until
11399
/// the first message is logged. Useful for programs that need to manage the order in
114100
/// which file descriptors are allocated.
115-
#[cfg(not(target_os = "haiku"))]
116101
LOG_NDELAY;
117102
/// Write the message to standard error output as well to the system log.
118-
#[cfg(not(target_os = "haiku"))]
103+
#[cfg(all(not(target_os = "redox"), not(target_os = "illumos")))]
119104
LOG_PERROR;
120105
}
121106
}

test/test_syslog.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity};
2+
use std::ffi::CStr;
23

34
#[test]
45
fn test_syslog_hello_world() {
5-
#[cfg(not(target_os = "haiku"))]
66
let flags = LogFlags::LOG_PID;
7-
#[cfg(target_os = "haiku")]
8-
let flags = LogFlags::empty();
9-
10-
openlog(None::<&str>, flags, Facility::LOG_USER).unwrap();
7+
openlog(None, flags, Facility::LOG_USER).unwrap();
118
syslog(Severity::LOG_EMERG, "Hello, nix!").unwrap();
129

1310
let name = "syslog";
1411
syslog(Severity::LOG_NOTICE, &format!("Hello, {name}!")).unwrap();
1512
}
13+
14+
#[test]
15+
fn test_openlog_with_ident() {
16+
const IDENT: &CStr = unsafe {
17+
CStr::from_bytes_with_nul_unchecked(b"test_openlog_with_ident\0")
18+
};
19+
20+
let flags = LogFlags::LOG_PID;
21+
openlog(Some(IDENT), flags, Facility::LOG_USER).unwrap();
22+
syslog(Severity::LOG_EMERG, "Hello, ident!").unwrap();
23+
}

0 commit comments

Comments
 (0)