polars_error/
warning.rs

1type WarningFunction = fn(&str, PolarsWarning);
2static mut WARNING_FUNCTION: Option<WarningFunction> = None;
3
4/// Set the function that will be called by the `polars_warn!` macro.
5/// You can use this to set logging in polars.
6///
7/// # Safety
8/// The caller must ensure there is no other thread accessing this function
9/// or calling `polars_warn!`.
10pub unsafe fn set_warning_function(function: WarningFunction) {
11    WARNING_FUNCTION = Some(function)
12}
13#[derive(Debug)]
14pub enum PolarsWarning {
15    UserWarning,
16    CategoricalRemappingWarning,
17    MapWithoutReturnDtypeWarning,
18}
19
20fn eprintln(fmt: &str, warning: PolarsWarning) {
21    eprintln!("{:?}: {}", warning, fmt);
22}
23
24pub fn get_warning_function() -> WarningFunction {
25    unsafe { WARNING_FUNCTION.unwrap_or(eprintln) }
26}
27#[macro_export]
28macro_rules! polars_warn {
29    ($variant:ident, $fmt:literal $(, $arg:tt)*) => {
30        {{
31        let func = $crate::get_warning_function();
32        let warn = $crate::PolarsWarning::$variant;
33        func(format!($fmt, $($arg)*).as_ref(), warn)
34        }}
35    };
36    ($fmt:literal, $($arg:tt)+) => {
37        {{
38        let func = $crate::get_warning_function();
39        func(format!($fmt, $($arg)+).as_ref(), $crate::PolarsWarning::UserWarning)
40        }}
41    };
42    ($($arg:tt)+) => {
43        polars_warn!("{}", $($arg)+);
44    };
45}