|
1 | 1 | //! Interfaces for controlling system log. |
2 | 2 |
|
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; |
6 | 6 |
|
7 | 7 | /// Logging options of subsequent [`syslog`] calls can be set by calling [`openlog`]. |
8 | 8 | /// |
9 | 9 | /// The parameter `ident` is a string that will be prepended to every message. The `logopt` |
10 | 10 | /// argument specifies logging options. The `facility` parameter encodes a default facility to be |
11 | 11 | /// 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>, |
14 | 14 | logopt: LogFlags, |
15 | 15 | 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()); |
20 | 18 | let logopt = logopt.bits(); |
21 | 19 | 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); |
29 | 22 | } |
30 | 23 | Ok(()) |
31 | 24 | } |
|
40 | 33 | /// ```rust |
41 | 34 | /// use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity}; |
42 | 35 | /// |
43 | | -/// #[cfg(not(target_os = "haiku"))] |
44 | 36 | /// 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(); |
49 | 38 | /// syslog(Severity::LOG_EMERG, "Hello, nix!").unwrap(); |
50 | 39 | /// |
51 | 40 | /// // use `format!` to format the message |
@@ -97,25 +86,21 @@ libc_bitflags! { |
97 | 86 | pub struct LogFlags: libc::c_int { |
98 | 87 | /// Log the process id with each message: useful for identifying instantiations of |
99 | 88 | /// daemons. |
100 | | - #[cfg(not(target_os = "haiku"))] |
101 | 89 | LOG_PID; |
102 | 90 | /// If syslog() cannot pass the message to syslogd(8) it will attempt to write the |
103 | 91 | /// message to the console ("/dev/console"). |
104 | | - #[cfg(not(target_os = "haiku"))] |
105 | 92 | LOG_CONS; |
106 | 93 | /// The converse of [`LOG_NDELAY`][LogFlags::LOG_NDELAY]; opening of the connection is |
107 | 94 | /// delayed until `syslog` is called. |
108 | 95 | /// |
109 | 96 | /// This is the default, and need not be specified. |
110 | | - #[cfg(not(target_os = "haiku"))] |
111 | 97 | LOG_ODELAY; |
112 | 98 | /// Open the connection to syslogd(8) immediately. Normally the open is delayed until |
113 | 99 | /// the first message is logged. Useful for programs that need to manage the order in |
114 | 100 | /// which file descriptors are allocated. |
115 | | - #[cfg(not(target_os = "haiku"))] |
116 | 101 | LOG_NDELAY; |
117 | 102 | /// 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")))] |
119 | 104 | LOG_PERROR; |
120 | 105 | } |
121 | 106 | } |
|
0 commit comments