1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
use crate::context::Context;
#[cfg(not(feature = "no_logging"))]
use crate::error::RenderError;
use crate::helpers::{HelperDef, HelperResult};
#[cfg(not(feature = "no_logging"))]
use crate::json::value::JsonRender;
use crate::output::Output;
use crate::registry::Registry;
use crate::render::{Helper, RenderContext};
#[cfg(not(feature = "no_logging"))]
use log::Level;
#[cfg(not(feature = "no_logging"))]
use std::str::FromStr;
#[derive(Clone, Copy)]
pub struct LogHelper;
#[cfg(not(feature = "no_logging"))]
impl HelperDef for LogHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
_: &'reg Registry<'reg>,
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
_: &mut dyn Output,
) -> HelperResult {
let param_to_log = h
.params()
.iter()
.map(|p| {
if let Some(ref relative_path) = p.relative_path() {
format!("{}: {}", relative_path, p.value().render())
} else {
p.value().render()
}
})
.collect::<Vec<String>>()
.join(", ");
let level = h
.hash_get("level")
.and_then(|v| v.value().as_str())
.unwrap_or("info");
if let Ok(log_level) = Level::from_str(level) {
log!(log_level, "{}", param_to_log)
} else {
return Err(RenderError::new(&format!(
"Unsupported logging level {}",
level
)));
}
Ok(())
}
}
#[cfg(feature = "no_logging")]
impl HelperDef for LogHelper {
fn call<'reg: 'rc, 'rc>(
&self,
_: &Helper<'reg, 'rc>,
_: &Registry<'reg>,
_: &Context,
_: &mut RenderContext<'reg, 'rc>,
_: &mut dyn Output,
) -> HelperResult {
Ok(())
}
}
pub static LOG_HELPER: LogHelper = LogHelper;
|