From d473bdc7f1e65a37384acb29af568c4b12a5a207 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Fri, 9 Aug 2019 20:13:07 +0000 Subject: [PATCH] lib: allow exporting all logging functions to Lua Add a function that will export FRR's logging functions into a Lua table, and add that table to the table of your choice (usually _ENV). For instance, to add logging to the global environment: lua_gettable(L, LUA_REGISTRYINDEX); lua_gettable(L, LUA_RIDX_GLOBALS); frrlua_export_logging(L); Then the following functions are globally accessible to any Lua scripts running with state L: - log.debug() - log.info() - log.notice() - log.warn() - log.error() These are bound to zlog_debug, zlog_info, etc. They only take one string argument for now but this shouldn't be an issue given Lua's builtin facilities for formatting strings. Signed-off-by: Quentin Young --- lib/frrlua.c | 85 ++++++++++++++++++++++++++++++++++++++-------------- lib/frrlua.h | 15 ++++++++++ 2 files changed, 77 insertions(+), 23 deletions(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index 8107102eed..4c38e08a07 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -26,23 +26,6 @@ #include "frrlua.h" #include "log.h" -/* - * Lua -> FRR function bindings. - * - * This section defines functions exportable into Lua environments. - */ - -static int lua_zlog_debug(lua_State *L) -{ - int debug_lua = 1; - const char *string = lua_tostring(L, 1); - - if (debug_lua) - zlog_debug("%s", string); - - return 0; -} - /* * FRR convenience functions. * @@ -126,6 +109,67 @@ void frrlua_newtable_interface(lua_State *L, const struct interface *ifp) lua_setfield(L, -2, "linklayer_type"); } +/* + * Logging. + * + * Lua-compatible wrappers for FRR logging functions. + */ +static const char *frrlua_log_thunk(lua_State *L) +{ + int nargs; + + nargs = lua_gettop(L); + assert(nargs == 1); + + return lua_tostring(L, 1); +} + +static int frrlua_log_debug(lua_State *L) +{ + zlog_debug("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_info(lua_State *L) +{ + zlog_info("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_notice(lua_State *L) +{ + zlog_notice("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_warn(lua_State *L) +{ + zlog_warn("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_error(lua_State *L) +{ + zlog_err("%s", frrlua_log_thunk(L)); + return 0; +} + +static const luaL_Reg log_funcs[] = { + {"debug", frrlua_log_debug}, + {"info", frrlua_log_info}, + {"notice", frrlua_log_notice}, + {"warn", frrlua_log_warn}, + {"error", frrlua_log_error}, + {}, +}; + +void frrlua_export_logging(lua_State *L) +{ + lua_newtable(L); + luaL_setfuncs(L, log_funcs, 0); + lua_setfield(L, -2, "log"); +} + /* * Experimental. @@ -169,9 +213,7 @@ lua_State *frrlua_initialize(const char *file) int status; lua_State *L = lua_newstate(frrlua_alloc, NULL); - zlog_debug("Newstate: %p", L); luaL_openlibs(L); - zlog_debug("Opened lib"); if (file) { status = luaL_loadfile(L, file); if (status) { @@ -182,11 +224,8 @@ lua_State *frrlua_initialize(const char *file) lua_pcall(L, 0, LUA_MULTRET, 0); } - zlog_debug("Setting global function"); - lua_pushcfunction(L, lua_zlog_debug); - lua_setglobal(L, "zlog_debug"); - return L; } + #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index 14cd3f0bdb..d6ee2347a9 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -98,6 +98,21 @@ const char *frrlua_table_get_string(lua_State *L, const char *key); */ int frrlua_table_get_integer(lua_State *L, const char *key); +/* + * Exports a new table containing bindings to FRR zlog functions into the + * global namespace. + * + * From Lua, these functions may be accessed as: + * + * - log.debug() + * - log.info() + * - log.warn() + * - log.error() + * + * They take a single string argument. + */ +void frrlua_export_logging(lua_State *L); + #ifdef __cplusplus } #endif -- 2.39.5