|
| 1 | +//! Interfaces for controlling system log. |
| 2 | +
|
| 3 | +use crate::{NixPath, Result}; |
| 4 | +use std::ffi::OsStr; |
| 5 | +use std::ptr; |
| 6 | + |
| 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 | +// |
| 13 | +// On Linux, the `ident` argument needs to have static lifetime according to the |
| 14 | +// man page: |
| 15 | +// |
| 16 | +// The argument ident in the call of openlog() is probably stored as-is. Thus, |
| 17 | +// if the string it points to is changed, syslog() may start prepending the changed |
| 18 | +// string, and if the string it points to ceases to exist, the results are |
| 19 | +// undefined. Most portable is to use a string constant. |
| 20 | +#[cfg(target_os = "linux")] |
| 21 | +pub fn openlog( |
| 22 | + ident: Option<&'static std::ffi::CStr>, |
| 23 | + logopt: LogFlags, |
| 24 | + facility: Facility, |
| 25 | +) -> Result<()> { |
| 26 | + let logopt = logopt.bits(); |
| 27 | + let facility = facility as libc::c_int; |
| 28 | + match ident { |
| 29 | + None => unsafe { |
| 30 | + libc::openlog(ptr::null(), logopt, facility); |
| 31 | + }, |
| 32 | + Some(ident) => ident.with_nix_path(|ident| unsafe { |
| 33 | + libc::openlog(ident.as_ptr(), logopt, facility); |
| 34 | + })?, |
| 35 | + } |
| 36 | + |
| 37 | + Ok(()) |
| 38 | +} |
| 39 | + |
| 40 | +/// Logging options of subsequent [`syslog`] calls can be set by calling [`openlog`]. |
| 41 | +/// |
| 42 | +/// The parameter `ident` is a string that will be prepended to every message. The `logopt` |
| 43 | +/// argument specifies logging options. The `facility` parameter encodes a default facility to be |
| 44 | +/// assigned to all messages that do not have an explicit facility encoded. |
| 45 | +#[cfg(not(target_os = "linux"))] |
| 46 | +pub fn openlog<S: AsRef<OsStr> + ?Sized>( |
| 47 | + ident: Option<&S>, |
| 48 | + logopt: LogFlags, |
| 49 | + facility: Facility, |
| 50 | +) -> Result<()> { |
| 51 | + let logopt = logopt.bits(); |
| 52 | + let facility = facility as libc::c_int; |
| 53 | + match ident.map(OsStr::new) { |
| 54 | + None => unsafe { |
| 55 | + libc::openlog(ptr::null(), logopt, facility); |
| 56 | + }, |
| 57 | + Some(ident) => ident.with_nix_path(|ident| unsafe { |
| 58 | + libc::openlog(ident.as_ptr(), logopt, facility); |
| 59 | + })?, |
| 60 | + } |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
| 64 | + |
| 65 | +/// Writes message to the system message logger. |
| 66 | +/// |
| 67 | +/// The message is then written to the system console, log files, logged-in users, or forwarded |
| 68 | +/// to other machines as appropriate. |
| 69 | +/// |
| 70 | +/// # Examples |
| 71 | +/// |
| 72 | +/// ```rust |
| 73 | +/// use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity, Priority}; |
| 74 | +/// |
| 75 | +/// let priority = Priority::new(Severity::LOG_EMERG, Facility::LOG_USER); |
| 76 | +/// syslog(priority, "Hello, nix!").unwrap(); |
| 77 | +/// |
| 78 | +/// // use `format!` to format the message |
| 79 | +/// let name = "syslog"; |
| 80 | +/// syslog(priority, &format!("Hello, {name}!")).unwrap(); |
| 81 | +/// ``` |
| 82 | +pub fn syslog<P, S>(priority: P, message: &S) -> Result<()> |
| 83 | +where |
| 84 | + P: Into<Priority>, |
| 85 | + S: AsRef<OsStr> + ?Sized, |
| 86 | +{ |
| 87 | + let priority = priority.into(); |
| 88 | + let formatter = OsStr::new("%s"); |
| 89 | + let message = OsStr::new(message); |
| 90 | + formatter.with_nix_path(|formatter| { |
| 91 | + message.with_nix_path(|message| unsafe { |
| 92 | + libc::syslog(priority.0, formatter.as_ptr(), message.as_ptr()) |
| 93 | + }) |
| 94 | + })??; |
| 95 | + Ok(()) |
| 96 | +} |
| 97 | + |
| 98 | +/// Closes the log file. |
| 99 | +pub fn closelog() { |
| 100 | + unsafe { libc::closelog() } |
| 101 | +} |
| 102 | + |
| 103 | +/// The priority for a log message. |
| 104 | +#[derive(Debug, Clone, Copy)] |
| 105 | +pub struct Priority(libc::c_int); |
| 106 | + |
| 107 | +impl Priority { |
| 108 | + /// Create a new priority from a facility and severity level. |
| 109 | + pub fn new(severity: Severity, facility: Facility) -> Self { |
| 110 | + let priority = (facility as libc::c_int) | (severity as libc::c_int); |
| 111 | + Priority(priority) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl From<Severity> for Priority { |
| 116 | + fn from(severity: Severity) -> Self { |
| 117 | + let priority = severity as libc::c_int; |
| 118 | + Priority(priority) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +libc_bitflags! { |
| 123 | + /// Options for system logging. |
| 124 | + pub struct LogFlags: libc::c_int { |
| 125 | + /// Log the process id with each message: useful for identifying instantiations of |
| 126 | + /// daemons. |
| 127 | + LOG_PID; |
| 128 | + /// If syslog() cannot pass the message to syslogd(8) it will attempt to write the |
| 129 | + /// message to the console ("/dev/console"). |
| 130 | + LOG_CONS; |
| 131 | + /// The converse of [`LOG_NDELAY`][LogFlags::LOG_NDELAY]; opening of the connection is |
| 132 | + /// delayed until `syslog` is called. |
| 133 | + /// |
| 134 | + /// This is the default, and need not be specified. |
| 135 | + LOG_ODELAY; |
| 136 | + /// Open the connection to syslogd(8) immediately. Normally the open is delayed until |
| 137 | + /// the first message is logged. Useful for programs that need to manage the order in |
| 138 | + /// which file descriptors are allocated. |
| 139 | + LOG_NDELAY; |
| 140 | + /// Write the message to standard error output as well to the system log. |
| 141 | + #[cfg(not(any(target_os = "redox", target_os = "illumos")))] |
| 142 | + LOG_PERROR; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +libc_enum! { |
| 147 | + /// Severity levels for log messages. |
| 148 | + #[repr(i32)] |
| 149 | + #[non_exhaustive] |
| 150 | + pub enum Severity { |
| 151 | + /// A panic condition. |
| 152 | + /// |
| 153 | + /// This is normally broadcast to all users. |
| 154 | + LOG_EMERG, |
| 155 | + /// A condition that should be corrected immediately, such as a corrupted system database. |
| 156 | + LOG_ALERT, |
| 157 | + /// Critical conditions, e.g., hard device errors. |
| 158 | + LOG_CRIT, |
| 159 | + /// Errors. |
| 160 | + LOG_ERR, |
| 161 | + /// Warning messages. |
| 162 | + LOG_WARNING, |
| 163 | + /// Conditions that are not error conditions, but should possibly be handled specially. |
| 164 | + LOG_NOTICE, |
| 165 | + /// Informational messages. |
| 166 | + LOG_INFO, |
| 167 | + /// Messages that contain information normally of use only when debugging a program. |
| 168 | + LOG_DEBUG, |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +libc_enum! { |
| 173 | + /// Facilities for log messages. |
| 174 | + #[repr(i32)] |
| 175 | + #[non_exhaustive] |
| 176 | + pub enum Facility { |
| 177 | + /// Messages generated by the kernel. |
| 178 | + /// |
| 179 | + /// These cannot be generated by any user processes. |
| 180 | + LOG_KERN, |
| 181 | + /// Messages generated by random user processes. |
| 182 | + /// |
| 183 | + /// This is the default facility identifier if none is specified. |
| 184 | + LOG_USER, |
| 185 | + /// The mail system. |
| 186 | + LOG_MAIL, |
| 187 | + /// System daemons, such as routed(8), that are not provided for explicitly by other facilities. |
| 188 | + LOG_DAEMON, |
| 189 | + /// The authorization system: login(1), su(1), getty(8), etc. |
| 190 | + LOG_AUTH, |
| 191 | + /// Messages generated internally by syslogd(8). |
| 192 | + LOG_SYSLOG, |
| 193 | + /// The line printer spooling system: cups-lpd(8), cupsd(8), etc. |
| 194 | + LOG_LPR, |
| 195 | + /// The network news system. |
| 196 | + LOG_NEWS, |
| 197 | + /// The uucp system. |
| 198 | + LOG_UUCP, |
| 199 | + /// Reserved for local use. |
| 200 | + LOG_LOCAL0, |
| 201 | + /// Reserved for local use. |
| 202 | + LOG_LOCAL1, |
| 203 | + /// Reserved for local use. |
| 204 | + LOG_LOCAL2, |
| 205 | + /// Reserved for local use. |
| 206 | + LOG_LOCAL3, |
| 207 | + /// Reserved for local use. |
| 208 | + LOG_LOCAL4, |
| 209 | + /// Reserved for local use. |
| 210 | + LOG_LOCAL5, |
| 211 | + /// Reserved for local use. |
| 212 | + LOG_LOCAL6, |
| 213 | + /// Reserved for local use. |
| 214 | + LOG_LOCAL7, |
| 215 | + } |
| 216 | +} |
0 commit comments