Skip to content

Commit 020e6b4

Browse files
tisonkunSteveLauC
andauthored
Impl syslog::setlogmask (#2579)
* Impl syslog::setlogmask Signed-off-by: tison <[email protected]> * Update src/syslog.rs Co-authored-by: SteveLauC <[email protected]> * fix compile Signed-off-by: tison <[email protected]> --------- Signed-off-by: tison <[email protected]> Co-authored-by: SteveLauC <[email protected]>
1 parent 73e832d commit 020e6b4

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

changelog/2579.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for syslog's `setlogmask` on all `unix`.

src/syslog.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,88 @@ 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 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+
103180
/// The priority for a log message.
104181
#[derive(Debug, Clone, Copy)]
105182
pub struct Priority(libc::c_int);

0 commit comments

Comments
 (0)