From: Ruben Kerkhof Date: Sun, 14 Aug 2016 10:07:59 +0000 (+0200) Subject: Lua plugin: turn the collectd functions into a lib X-Git-Tag: collectd-5.6.0~33^2~9 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=c6d13fbae8ca35535bcd9ae857985c78051db872 Lua plugin: turn the collectd functions into a lib Now the module shows up in 'package.loaded': for name, func in pairs(package.loaded['collectd']) do print(name, func) end returns: dispatch_values function: 0x7f38b8a472c0 log_debug function: 0x7f38b8a47130 register_write function: 0x7f38b8a47520 log_info function: 0x7f38b8a470d0 register_read function: 0x7f38b8a47360 log_warning function: 0x7f38b8a47070 log_error function: 0x7f38b8a47100 log_notice function: 0x7f38b8a470a0 --- diff --git a/src/lua.c b/src/lua.c index a3f2037e..a0e36036 100644 --- a/src/lua.c +++ b/src/lua.c @@ -362,7 +362,7 @@ static int lua_cb_register_write(lua_State *L) /* {{{ */ return 0; } /* }}} int lua_cb_register_write */ -static luaL_Reg lua_c_functions[] = { +static luaL_Reg collectdlib[] = { {"log_debug", lua_cb_log_debug}, {"log_error", lua_cb_log_error}, {"log_info", lua_cb_log_info}, @@ -372,6 +372,12 @@ static luaL_Reg lua_c_functions[] = { {"register_read", lua_cb_register_read}, {"register_write", lua_cb_register_write}}; +static int open_collectd(lua_State *L) /* {{{ */ +{ + luaL_newlib(L, collectdlib); + return 1; +} /* }}} */ + static void lua_script_free(lua_script_t *script) /* {{{ */ { if (script == NULL) @@ -404,13 +410,9 @@ static int lua_script_init(lua_script_t *script) /* {{{ */ /* Open up all the standard Lua libraries. */ luaL_openlibs(script->lua_state); - /* Register all the functions we implement in C */ - lua_newtable(script->lua_state); - for (size_t i = 0; i < STATIC_ARRAY_SIZE(lua_c_functions); i++) { - lua_pushcfunction(script->lua_state, lua_c_functions[i].func); - lua_setfield(script->lua_state, -2, lua_c_functions[i].name); - } - lua_setglobal(script->lua_state, "collectd"); + /* Load the 'collectd' library */ + luaL_requiref(script->lua_state, "collectd", open_collectd, 1); + lua_pop(script->lua_state, 1); /* Prepend BasePath to package.path */ if (base_path[0] != '\0') {