Skip to content

Commit 11eda98

Browse files
authored
Merge pull request #544 from djkoloski/add_set_max_level_racy
Add `set_max_level_racy` and gate `set_max_level`
2 parents f212c9e + e5f1ae9 commit 11eda98

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,38 @@ where
12171217
///
12181218
/// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs.
12191219
#[inline]
1220+
#[cfg(atomic_cas)]
12201221
pub fn set_max_level(level: LevelFilter) {
12211222
MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
12221223
}
12231224

1225+
/// A thread-unsafe version of [`set_max_level`].
1226+
///
1227+
/// This function is available on all platforms, even those that do not have
1228+
/// support for atomics that is needed by [`set_max_level`].
1229+
///
1230+
/// In almost all cases, [`set_max_level`] should be preferred.
1231+
///
1232+
/// # Safety
1233+
///
1234+
/// This function is only safe to call when no other level setting function is
1235+
/// called while this function still executes.
1236+
///
1237+
/// This can be upheld by (for example) making sure that **there are no other
1238+
/// threads**, and (on embedded) that **interrupts are disabled**.
1239+
///
1240+
/// Is is safe to use all other logging functions while this function runs
1241+
/// (including all logging macros).
1242+
///
1243+
/// [`set_max_level`]: fn.set_max_level.html
1244+
#[inline]
1245+
pub unsafe fn set_max_level_racy(level: LevelFilter) {
1246+
// `MAX_LOG_LEVEL_FILTER` uses a `Cell` as the underlying primitive when a
1247+
// platform doesn't support `atomic_cas`, so even though this looks the same
1248+
// as `set_max_level` it may have different safety properties.
1249+
MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
1250+
}
1251+
12241252
/// Returns the current maximum log level.
12251253
///
12261254
/// The [`log!`], [`error!`], [`warn!`], [`info!`], [`debug!`], and [`trace!`] macros check

0 commit comments

Comments
 (0)