Skip to content

Commit 7bf4e8f

Browse files
committed
Impl syslog::setlogmask
Signed-off-by: tison <wander4096@gmail.com>
1 parent 33efb1a commit 7bf4e8f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/syslog.rs

+76
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,87 @@ where
9595
Ok(())
9696
}
9797

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+
98115
/// Closes the log file.
99116
pub fn closelog() {
100117
unsafe { libc::closelog() }
101118
}
102119

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 pri = priority as libc::c_int;
142+
(self.0 & (1 << pri)) != 0
143+
}
144+
}
145+
146+
impl std::ops::BitOr for LogMask {
147+
type Output = Self;
148+
fn bitor(self, rhs: Self) -> Self::Output {
149+
Self(self.0 | rhs.0)
150+
}
151+
}
152+
153+
impl std::ops::BitAnd for LogMask {
154+
type Output = Self;
155+
fn bitand(self, rhs: Self) -> Self::Output {
156+
Self(self.0 & rhs.0)
157+
}
158+
}
159+
160+
impl std::ops::BitOrAssign for LogMask {
161+
fn bitor_assign(&mut self, rhs: Self) {
162+
self.0 |= rhs.0;
163+
}
164+
}
165+
166+
impl std::ops::BitAndAssign for LogMask {
167+
fn bitand_assign(&mut self, rhs: Self) {
168+
self.0 &= rhs.0;
169+
}
170+
}
171+
172+
impl std::ops::Not for LogMask {
173+
type Output = Self;
174+
fn not(self) -> Self::Output {
175+
Self(!self.0)
176+
}
177+
}
178+
103179
/// The priority for a log message.
104180
#[derive(Debug, Clone, Copy)]
105181
pub struct Priority(libc::c_int);

0 commit comments

Comments
 (0)