Skip to content

Commit 06d5b00

Browse files
committed
Impl syslog::setlogmask
Signed-off-by: tison <[email protected]>
1 parent 33efb1a commit 06d5b00

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/syslog.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,91 @@ 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+
112+
let prev_mask = unsafe {
113+
libc::setlogmask(mask)
114+
};
115+
116+
LogMask(prev_mask)
117+
}
118+
98119
/// Closes the log file.
99120
pub fn closelog() {
100121
unsafe { libc::closelog() }
101122
}
102123

124+
/// System log priority mask.
125+
#[derive(Debug, Clone, Copy)]
126+
pub struct LogMask(libc::c_int);
127+
128+
impl LogMask {
129+
/// Creates a mask of all priorities up to and including `priority`.
130+
#[doc(alias("LOG_UPTO"))]
131+
pub fn up_to(priority: Severity) -> Self {
132+
let pri = priority as libc::c_int;
133+
Self((1 << (pri + 1)) - 1)
134+
}
135+
136+
/// Creates a mask for the specified priority.
137+
#[doc(alias("LOG_MASK"))]
138+
pub fn of_priority(priority: Severity) -> Self {
139+
let pri = priority as libc::c_int;
140+
Self(1 << pri)
141+
}
142+
143+
/// Returns if the mask for the specified `priority` is set.
144+
pub fn contains(&self, priority: Severity) -> bool {
145+
let pri = priority as libc::c_int;
146+
(self.0 & (1 << pri)) != 0
147+
}
148+
}
149+
150+
impl std::ops::BitOr for LogMask {
151+
type Output = Self;
152+
fn bitor(self, rhs: Self) -> Self::Output {
153+
Self(self.0 | rhs.0)
154+
}
155+
}
156+
157+
impl std::ops::BitAnd for LogMask {
158+
type Output = Self;
159+
fn bitand(self, rhs: Self) -> Self::Output {
160+
Self(self.0 & rhs.0)
161+
}
162+
}
163+
164+
impl std::ops::BitOrAssign for LogMask {
165+
fn bitor_assign(&mut self, rhs: Self) {
166+
self.0 |= rhs.0;
167+
}
168+
}
169+
170+
impl std::ops::BitAndAssign for LogMask {
171+
fn bitand_assign(&mut self, rhs: Self) {
172+
self.0 &= rhs.0;
173+
}
174+
}
175+
176+
impl std::ops::Not for LogMask {
177+
type Output = Self;
178+
fn not(self) -> Self::Output {
179+
Self(!self.0)
180+
}
181+
}
182+
103183
/// The priority for a log message.
104184
#[derive(Debug, Clone, Copy)]
105185
pub struct Priority(libc::c_int);

0 commit comments

Comments
 (0)