| 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 |
/// Set the process-wide priority mask to `mask` and return the previous mask |
| 99 |
/// value. |
| 100 |
/// |
| 101 |
/// Calls to `syslog()` with a priority level not set in `mask` are ignored. The |
| 102 |
/// default is to log all priorities. |
| 103 |
/// |
| 104 |
/// If the `mask` argument is `None`, the current logmask is not modified, this |
| 105 |
/// can be used to query the current log mask. |
| 106 |
pub fn setlogmask(mask: Option<LogMask>) -> LogMask { |
| 107 |
let mask = match mask { |
| 108 |
Some(mask) => mask.0, |
| 109 |
None => 0, |
| 110 |
}; |
| 111 |
let prev_mask = unsafe { libc::setlogmask(mask) }; |
| 112 |
LogMask(prev_mask) |
| 113 |
} |
| 114 |
|
| 115 |
/// Closes the log file. |
| 116 |
pub fn closelog() { |
| 117 |
unsafe { libc::closelog() } |
| 118 |
} |
| 119 |
|
| 120 |
/// System log priority mask. |
| 121 |
#[derive(Debug, Clone, Copy)] |
| 122 |
pub struct LogMask(libc::c_int); |
| 123 |
|
| 124 |
impl LogMask { |
| 125 |
/// Creates a mask of all priorities up to and including `priority`. |
| 126 |
#[doc(alias("LOG_UPTO"))] |
| 127 |
pub fn up_to(priority: Severity) -> Self { |
| 128 |
let pri = priority as libc::c_int; |
| 129 |
Self((1 << (pri + 1)) - 1) |
| 130 |
} |
| 131 |
|
| 132 |
/// Creates a mask for the specified priority. |
| 133 |
#[doc(alias("LOG_MASK"))] |
| 134 |
pub fn of_priority(priority: Severity) -> Self { |
| 135 |
let pri = priority as libc::c_int; |
| 136 |
Self(1 << pri) |
| 137 |
} |
| 138 |
|
| 139 |
/// Returns if the mask for the specified `priority` is set. |
| 140 |
pub fn contains(&self, priority: Severity) -> bool { |
| 141 |
let priority = Self::of_priority(priority); |
| 142 |
let and_result = *self & priority; |
| 143 |
and_result.0 != 0 |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl std::ops::BitOr for LogMask { |
| 148 |
type Output = Self; |
| 149 |
fn bitor(self, rhs: Self) -> Self::Output { |
| 150 |
Self(self.0 | rhs.0) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl std::ops::BitAnd for LogMask { |
| 155 |
type Output = Self; |
| 156 |
fn bitand(self, rhs: Self) -> Self::Output { |
| 157 |
Self(self.0 & rhs.0) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl std::ops::BitOrAssign for LogMask { |
| 162 |
fn bitor_assign(&mut self, rhs: Self) { |
| 163 |
self.0 |= rhs.0; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
impl std::ops::BitAndAssign for LogMask { |
| 168 |
fn bitand_assign(&mut self, rhs: Self) { |
| 169 |
self.0 &= rhs.0; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
impl std::ops::Not for LogMask { |
| 174 |
type Output = Self; |
| 175 |
fn not(self) -> Self::Output { |
| 176 |
Self(!self.0) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/// The priority for a log message. |
| 181 |
#[derive(Debug, Clone, Copy)] |
| 182 |
pub struct Priority(libc::c_int); |
| 183 |
|
| 184 |
impl Priority { |
| 185 |
/// Create a new priority from a facility and severity level. |
| 186 |
pub fn new(severity: Severity, facility: Facility) -> Self { |
| 187 |
let priority = (facility as libc::c_int) | (severity as libc::c_int); |
| 188 |
Priority(priority) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl From<Severity> for Priority { |
| 193 |
fn from(severity: Severity) -> Self { |
| 194 |
let priority = severity as libc::c_int; |
| 195 |
Priority(priority) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
libc_bitflags! { |
| 200 |
/// Options for system logging. |
| 201 |
pub struct LogFlags: libc::c_int { |
| 202 |
/// Log the process id with each message: useful for identifying instantiations of |
| 203 |
/// daemons. |
| 204 |
LOG_PID; |
| 205 |
/// If syslog() cannot pass the message to syslogd(8) it will attempt to write the |
| 206 |
/// message to the console ("/dev/console"). |
| 207 |
LOG_CONS; |
| 208 |
/// The converse of [`LOG_NDELAY`][LogFlags::LOG_NDELAY]; opening of the connection is |
| 209 |
/// delayed until `syslog` is called. |
| 210 |
/// |
| 211 |
/// This is the default, and need not be specified. |
| 212 |
LOG_ODELAY; |
| 213 |
/// Open the connection to syslogd(8) immediately. Normally the open is delayed until |
| 214 |
/// the first message is logged. Useful for programs that need to manage the order in |
| 215 |
/// which file descriptors are allocated. |
| 216 |
LOG_NDELAY; |
| 217 |
/// Write the message to standard error output as well to the system log. |
| 218 |
#[cfg(not(any(solarish, target_os = "redox", target_os = "cygwin")))] |
| 219 |
LOG_PERROR; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
libc_enum! { |
| 224 |
/// Severity levels for log messages. |
| 225 |
#[repr(i32)] |
| 226 |
#[non_exhaustive] |
| 227 |
pub enum Severity { |
| 228 |
/// A panic condition. |
| 229 |
/// |
| 230 |
/// This is normally broadcast to all users. |
| 231 |
LOG_EMERG, |
| 232 |
/// A condition that should be corrected immediately, such as a corrupted system database. |
| 233 |
LOG_ALERT, |
| 234 |
/// Critical conditions, e.g., hard device errors. |
| 235 |
LOG_CRIT, |
| 236 |
/// Errors. |
| 237 |
LOG_ERR, |
| 238 |
/// Warning messages. |
| 239 |
LOG_WARNING, |
| 240 |
/// Conditions that are not error conditions, but should possibly be handled specially. |
| 241 |
LOG_NOTICE, |
| 242 |
/// Informational messages. |
| 243 |
LOG_INFO, |
| 244 |
/// Messages that contain information normally of use only when debugging a program. |
| 245 |
LOG_DEBUG, |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
libc_enum! { |
| 250 |
/// Facilities for log messages. |
| 251 |
#[repr(i32)] |
| 252 |
#[non_exhaustive] |
| 253 |
pub enum Facility { |
| 254 |
/// Messages generated by the kernel. |
| 255 |
/// |
| 256 |
/// These cannot be generated by any user processes. |
| 257 |
LOG_KERN, |
| 258 |
/// Messages generated by random user processes. |
| 259 |
/// |
| 260 |
/// This is the default facility identifier if none is specified. |
| 261 |
LOG_USER, |
| 262 |
/// The mail system. |
| 263 |
LOG_MAIL, |
| 264 |
/// System daemons, such as routed(8), that are not provided for explicitly by other facilities. |
| 265 |
LOG_DAEMON, |
| 266 |
/// The authorization system: login(1), su(1), getty(8), etc. |
| 267 |
LOG_AUTH, |
| 268 |
/// Messages generated internally by syslogd(8). |
| 269 |
LOG_SYSLOG, |
| 270 |
/// The line printer spooling system: cups-lpd(8), cupsd(8), etc. |
| 271 |
LOG_LPR, |
| 272 |
/// The network news system. |
| 273 |
LOG_NEWS, |
| 274 |
/// The uucp system. |
| 275 |
LOG_UUCP, |
| 276 |
/// Reserved for local use. |
| 277 |
LOG_LOCAL0, |
| 278 |
/// Reserved for local use. |
| 279 |
LOG_LOCAL1, |
| 280 |
/// Reserved for local use. |
| 281 |
LOG_LOCAL2, |
| 282 |
/// Reserved for local use. |
| 283 |
LOG_LOCAL3, |
| 284 |
/// Reserved for local use. |
| 285 |
LOG_LOCAL4, |
| 286 |
/// Reserved for local use. |
| 287 |
LOG_LOCAL5, |
| 288 |
/// Reserved for local use. |
| 289 |
LOG_LOCAL6, |
| 290 |
/// Reserved for local use. |
| 291 |
LOG_LOCAL7, |
| 292 |
} |
| 293 |
} |