diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2019-08-09 20:13:07 +0000 |
|---|---|---|
| committer | Quentin Young <qlyoung@nvidia.com> | 2020-12-01 18:37:14 -0500 |
| commit | d473bdc7f1e65a37384acb29af568c4b12a5a207 (patch) | |
| tree | db53e564149e4b313a5954fb9d0465926959ca2f /lib | |
| parent | cd6ca660c5e96f14f50b6a2f4f6f5cd83cb70d13 (diff) | |
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 <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/frrlua.c | 85 | ||||
| -rw-r--r-- | 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 @@ -27,23 +27,6 @@ #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. * * This section has convenience functions used to make interacting with the Lua @@ -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 |
