generated from lucas/rust-template
Expose logging functions to Lua
This commit is contained in:
parent
d2cb39f9a2
commit
23d27389d3
3 changed files with 38 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,3 @@
|
|||
/target
|
||||
.envrc
|
||||
*.lua
|
||||
lua/
|
||||
|
|
|
@ -160,13 +160,21 @@ pub(crate) enum Method {
|
|||
Post,
|
||||
}
|
||||
|
||||
impl Default for Method {
|
||||
fn default() -> Self {
|
||||
Self::Get
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub(crate) struct ApiTask {
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub delay: u64,
|
||||
#[serde(default)]
|
||||
pub method: Method,
|
||||
pub url: String,
|
||||
#[serde(default)]
|
||||
pub body: serde_json::Value,
|
||||
#[serde(default)]
|
||||
pub query: HashMap<String, String>,
|
||||
|
|
|
@ -19,6 +19,34 @@ pub fn worker(
|
|||
let config = lua.load(config).set_name("config");
|
||||
|
||||
lua.scope(|scope| {
|
||||
let log_trace_fn = scope.create_function(|_, s: String| {
|
||||
tracing::trace!(name: "lua", "{}", s);
|
||||
Ok(())
|
||||
})?;
|
||||
let log_debug_fn = scope.create_function(|_, s: String| {
|
||||
tracing::debug!(name: "lua", "{}", s);
|
||||
Ok(())
|
||||
})?;
|
||||
let log_info_fn = scope.create_function(|_, s: String| {
|
||||
tracing::info!(name: "lua", "{}", s);
|
||||
Ok(())
|
||||
})?;
|
||||
let log_warn_fn = scope.create_function(|_, s: String| {
|
||||
tracing::warn!(name: "lua", "{}", s);
|
||||
Ok(())
|
||||
})?;
|
||||
let log_error_fn = scope.create_function(|_, s: String| {
|
||||
tracing::error!(name: "lua", "{}", s);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let log_tbl = lua.create_table_with_capacity(0, 5)?;
|
||||
log_tbl.set("trace", log_trace_fn)?;
|
||||
log_tbl.set("debug", log_debug_fn)?;
|
||||
log_tbl.set("info", log_info_fn)?;
|
||||
log_tbl.set("warn", log_warn_fn)?;
|
||||
log_tbl.set("error", log_error_fn)?;
|
||||
|
||||
let ntfy_fn = scope.create_function_mut(|_, data: mlua::Value| {
|
||||
let data = lua.from_value(data)?;
|
||||
tracing::trace!("Sending Ntfy message: {:?}", data);
|
||||
|
@ -45,6 +73,7 @@ pub fn worker(
|
|||
}
|
||||
})?;
|
||||
|
||||
globals.set("log", log_tbl)?;
|
||||
globals.set("ntfy", ntfy_fn)?;
|
||||
globals.set("api_task", set_api_task_fn)?;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue