Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-error/src/warning.rs
6939 views
1
use parking_lot::RwLock;
2
3
type WarningFunction = fn(&str, PolarsWarning);
4
static WARNING_FUNCTION: RwLock<WarningFunction> = RwLock::new(eprintln);
5
6
fn eprintln(fmt: &str, warning: PolarsWarning) {
7
eprintln!("{warning:?}: {fmt}");
8
}
9
10
/// Set the function that will be called by the `polars_warn!` macro.
11
/// You can use this to set logging in polars.
12
pub fn set_warning_function(function: WarningFunction) {
13
*WARNING_FUNCTION.write() = function;
14
}
15
16
pub fn get_warning_function() -> WarningFunction {
17
*WARNING_FUNCTION.read()
18
}
19
20
#[derive(Debug)]
21
pub enum PolarsWarning {
22
Deprecation,
23
UserWarning,
24
CategoricalRemappingWarning,
25
MapWithoutReturnDtypeWarning,
26
}
27
28
#[macro_export]
29
macro_rules! polars_warn {
30
($variant:ident, $fmt:literal $(, $arg:expr)* $(,)?) => {
31
{{
32
let func = $crate::get_warning_function();
33
let warn = $crate::PolarsWarning::$variant;
34
func(format!($fmt, $($arg)*).as_ref(), warn)
35
}}
36
};
37
($fmt:literal, $($arg:expr)+) => {
38
{{
39
let func = $crate::get_warning_function();
40
func(format!($fmt, $($arg)+).as_ref(), $crate::PolarsWarning::UserWarning)
41
}}
42
};
43
($($arg:tt)+) => {
44
polars_warn!("{}", $($arg)+);
45
};
46
}
47
48