From c2ffaf95e6e5b47c1d14318a88ab32d356464be6 Mon Sep 17 00:00:00 2001 From: Jan Moskyto Matejka Date: Mon, 6 Feb 2017 15:25:48 +0100 Subject: Lua: common log functions --- lua/Makefile | 2 +- lua/common.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lua/filter.c | 2 ++ lua/lua.h | 5 ++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 lua/common.c create mode 100644 lua/lua.h diff --git a/lua/Makefile b/lua/Makefile index 493b9475..b74309de 100644 --- a/lua/Makefile +++ b/lua/Makefile @@ -1,4 +1,4 @@ -src := filter.c +src := common.c filter.c obj := $(src-o-files) $(all-daemon) #$(cf-local) diff --git a/lua/common.c b/lua/common.c new file mode 100644 index 00000000..fea030f5 --- /dev/null +++ b/lua/common.c @@ -0,0 +1,77 @@ +#include "nest/bird.h" +#include "filter/filter.h" +#include "lua.h" + +#include +#include +#include + +static int luaB_err(lua_State *L) { + int n = lua_gettop(L); + if (n != 1) + log(L_WARN "bird.err() accepts exactly 1 argument"); + + if (n < 1) + return 0; + + log(L_ERR "%s", lua_tostring(L, 1)); + return 0; +} + +static int luaB_warn(lua_State *L) { + int n = lua_gettop(L); + if (n != 1) + log(L_WARN "bird.warn() accepts exactly 1 argument"); + + if (n < 1) + return 0; + + log(L_WARN "%s", lua_tostring(L, 1)); + return 0; +} + +static int luaB_info(lua_State *L) { + int n = lua_gettop(L); + if (n != 1) + log(L_WARN "bird.info() accepts exactly 1 argument"); + + if (n < 1) + return 0; + + log(L_INFO "%s", lua_tostring(L, 1)); + return 0; +} + +static int luaB_trace(lua_State *L) { + int n = lua_gettop(L); + if (n != 1) + log(L_WARN "bird.trace() accepts exactly 1 argument"); + + if (n < 1) + return 0; + + log(L_TRACE "%s", lua_tostring(L, 1)); + return 0; +} + +void luaB_push_bird_table(lua_State *L) { + lua_newtable(L); + + lua_pushstring(L, "err"); + lua_pushcfunction(L, luaB_err); + lua_settable(L, -3); + + lua_pushstring(L, "warn"); + lua_pushcfunction(L, luaB_warn); + lua_settable(L, -3); + + lua_pushstring(L, "info"); + lua_pushcfunction(L, luaB_info); + lua_settable(L, -3); + + lua_pushstring(L, "trace"); + lua_pushcfunction(L, luaB_trace); + lua_settable(L, -3); + + lua_setglobal(L, "bird"); +} diff --git a/lua/filter.c b/lua/filter.c index c40a5207..9dabb28f 100644 --- a/lua/filter.c +++ b/lua/filter.c @@ -1,5 +1,6 @@ #include "nest/bird.h" #include "filter/filter.h" +#include "lua.h" #include #include @@ -8,6 +9,7 @@ int filter_lua_chunk(const char *chunk, struct rte **e, struct rta *a, struct ea_list **ea, struct linpool *lp) { lua_State *L = luaL_newstate(); luaL_openlibs(L); + luaB_push_bird_table(L); int le = luaL_dostring(L, chunk); int out; if (le) { diff --git a/lua/lua.h b/lua/lua.h new file mode 100644 index 00000000..7e6f31ce --- /dev/null +++ b/lua/lua.h @@ -0,0 +1,5 @@ +#include "nest/bird.h" + +#include + +void luaB_push_bird_table(lua_State *L); -- cgit v1.2.3