diff options
author | Jan Moskyto Matejka <mq@ucw.cz> | 2017-02-06 15:25:48 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2019-02-22 23:12:41 +0100 |
commit | c2ffaf95e6e5b47c1d14318a88ab32d356464be6 (patch) | |
tree | 35a0528cba57b41bcfa64a18df084feecb11c939 | |
parent | 96b1cc6c3f5ab49993ce9373e33f1f8fc04bdc61 (diff) |
Lua: common log functions
-rw-r--r-- | lua/Makefile | 2 | ||||
-rw-r--r-- | lua/common.c | 77 | ||||
-rw-r--r-- | lua/filter.c | 2 | ||||
-rw-r--r-- | lua/lua.h | 5 |
4 files changed, 85 insertions, 1 deletions
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 <lua.h> +#include <lualib.h> +#include <lauxlib.h> + +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 <lua.h> #include <lualib.h> @@ -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 <lua.h> + +void luaB_push_bird_table(lua_State *L); |