added 'log' output table to lua filter

This commit is contained in:
BroodjeAap 2022-12-10 09:22:25 +00:00
parent c1b8a1055e
commit 3a10812036
3 changed files with 28 additions and 1 deletions

View file

@ -811,8 +811,13 @@ func getFilterResultLua(filter *Filter) {
}
}
L.SetGlobal("inputs", inputs)
outputs := L.CreateTable(10, 0)
L.SetGlobal("outputs", outputs)
logs := L.CreateTable(10, 0)
L.SetGlobal("logs", logs)
err := L.DoString(filter.Var1)
if err != nil {
filter.log(err)
@ -823,4 +828,9 @@ func getFilterResultLua(filter *Filter) {
filter.Results = append(filter.Results, value.String())
},
)
logs.ForEach(
func(key lua.LValue, value lua.LValue) {
filter.Logs = append(filter.Logs, value.String())
},
)
}

View file

@ -1217,3 +1217,21 @@ if not(result==true) then error("regexp.match()") end`
})
}
}
func TestFilterLuaLogs(t *testing.T) {
script := `table.insert(logs, "test")`
filter := Filter{
Var1: script,
}
getFilterResultLua(&filter)
if len(filter.Logs) == 0 {
t.Error("Nothing in logs, expected 'test'")
}
if filter.Logs[0] != "test" {
t.Errorf("Unexpected log message: '%s'", filter.Logs[0])
}
}

View file

@ -1,5 +1,4 @@
# Todo
- add logs table to lua filter
- make generic 'notifier' interface
- notify filter var2 for specific notifier or 'all'
- telegram