Compare commits

...

2 Commits

Author SHA1 Message Date
e21ea0f34e Implement custom lua print function that calls info
All checks were successful
Build and deploy / build (push) Successful in 11m54s
Build and deploy / Deploy container (push) Successful in 45s
2025-09-01 02:47:47 +02:00
fb7e1f1472 Small cleanup 2025-09-01 02:47:29 +02:00
2 changed files with 26 additions and 2 deletions

View File

@@ -2,7 +2,6 @@ use std::fmt::Debug;
use automation_cast::Cast; use automation_cast::Cast;
use dyn_clone::DynClone; use dyn_clone::DynClone;
use google_home::traits::OnOff;
use mlua::ObjectLike; use mlua::ObjectLike;
use crate::event::OnMqtt; use crate::event::OnMqtt;
@@ -18,7 +17,7 @@ pub trait LuaDeviceCreate {
} }
pub trait Device: pub trait Device:
Debug + DynClone + Sync + Send + Cast<dyn google_home::Device> + Cast<dyn OnMqtt> + Cast<dyn OnOff> Debug + DynClone + Sync + Send + Cast<dyn google_home::Device> + Cast<dyn OnMqtt>
{ {
fn get_id(&self) -> String; fn get_id(&self) -> String;
} }

View File

@@ -1,3 +1,4 @@
#![feature(iter_intersperse)]
mod web; mod web;
use std::net::SocketAddr; use std::net::SocketAddr;
@@ -83,6 +84,30 @@ async fn app() -> anyhow::Result<()> {
warn!("{text}"); warn!("{text}");
Ok(()) Ok(())
}); });
let print = lua.create_function(|lua, values: mlua::Variadic<mlua::Value>| {
// Fortmat the values the same way lua does by default
let text: String = values
.iter()
.map(|value| {
value.to_string().unwrap_or_else(|_| {
format!("{}: {}", value.type_name(), value.to_pointer().addr())
})
})
.intersperse("\t".to_owned())
.collect();
// Level 1 of the stack gives us the location that called this function
let stack = lua.inspect_stack(1).unwrap();
let file = stack.source().short_src.unwrap_or("?".into());
let line = stack.curr_line();
// The target is overridden to make it possible to filter for logs originating from the
// config
info!(target: "automation_config", %file, line, "{text}");
Ok(())
})?;
lua.globals().set("print", print)?;
let automation = lua.create_table()?; let automation = lua.create_table()?;
let event_channel = device_manager.event_channel(); let event_channel = device_manager.event_channel();