|
1 | 1 | //! Interfaces for controlling system log.
|
2 | 2 |
|
3 |
| -use std::ffi::CString; |
| 3 | +use crate::NixPath; |
| 4 | +use crate::Result; |
| 5 | +use std::ffi::OsStr; |
4 | 6 |
|
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 | + }) |
10 | 20 | }
|
11 | 21 |
|
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); |
15 | 29 | unsafe { libc::syslog(priority.0, formatter.as_ptr(), message.as_ptr()) }
|
16 | 30 | }
|
17 | 31 |
|
|
0 commit comments