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