Skip to content

Commit 953fdec

Browse files
committed
reduce error handle noise and add docs
Signed-off-by: tison <[email protected]>
1 parent a19d932 commit 953fdec

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/syslog.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
//! Interfaces for controlling system log.
22
3-
use std::ffi::CString;
3+
use crate::NixPath;
4+
use crate::Result;
5+
use std::ffi::OsStr;
46

5-
pub fn openlog(ident: &str, logopt: LogFlags, facility: Facility) {
6-
let ident = CString::new(ident).expect("TODO: handle error");
7-
unsafe {
8-
libc::openlog(ident.as_ptr(), logopt.bits(), facility as libc::c_int)
9-
}
7+
/// Logging options of subsequent [`syslog`] calls can be set by calling [`openlog`].
8+
///
9+
/// The parameter `ident` is a string that will be prepended to every message. The `logopt`
10+
/// argument specifies logging options. The `facility` parameter encodes a default facility to be
11+
/// assigned to all messages that do not have an explicit facility encoded.
12+
pub fn openlog<P: NixPath + ?Sized>(
13+
ident: &P,
14+
logopt: LogFlags,
15+
facility: Facility,
16+
) -> Result<()> {
17+
ident.with_nix_path(|ident| unsafe {
18+
libc::openlog(ident.as_ptr(), logopt.bits(), facility as libc::c_int);
19+
})
1020
}
1121

12-
pub fn syslog(priority: Priority, message: &str) {
13-
let formatter = CString::new("%s").expect("TODO: handle error");
14-
let message = CString::new(message).expect("TODO: handle error");
22+
/// Writes message to the system message logger.
23+
///
24+
/// The message is then written to the system console, log files, logged-in users, or forwarded
25+
/// to other machines as appropriate.
26+
pub fn syslog<S: AsRef<OsStr> + ?Sized>(priority: Priority, message: &S) {
27+
let formatter = OsStr::new("%s");
28+
let message = OsStr::new(message);
1529
unsafe { libc::syslog(priority.0, formatter.as_ptr(), message.as_ptr()) }
1630
}
1731

0 commit comments

Comments
 (0)