From: Florian Forster Date: Wed, 24 Nov 2010 14:11:16 +0000 (+0100) Subject: lua plugin: Move the "collectd_log()" function to src/lua.c. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=7672ab7335794916a62a120cf29476403462c714;p=collectd.git lua plugin: Move the "collectd_log()" function to src/lua.c. --- diff --git a/contrib/collectd.lua b/contrib/collectd.lua new file mode 100644 index 00000000..f4b83466 --- /dev/null +++ b/contrib/collectd.lua @@ -0,0 +1,26 @@ +-- contrib/collectd.lua +-- +-- Auxilliary functions to use in plugins written in Lua. Load this file using +-- dofile ("collectd.lua"); +-- at the beginning of your script. + +function collectd_error (msg) + return (collectd_log (3, msg)); +end + +function collectd_warning (msg) + return (collectd_log (4, msg)); +end + +function collectd_notice (msg) + return (collectd_log (5, msg)); +end + +function collectd_info (msg) + return (collectd_log (6, msg)); +end + +function collectd_debug (msg) + return (collectd_log (7, msg)); +end + diff --git a/src/lua.c b/src/lua.c index 74cd41d7..bd3413ec 100644 --- a/src/lua.c +++ b/src/lua.c @@ -31,8 +31,6 @@ #include #include -#include "lua_exports.c" - typedef struct lua_script_s { char *script_path; lua_State *lua_state; @@ -40,15 +38,86 @@ typedef struct lua_script_s { struct lua_script_s *next; } lua_script_t; +struct lua_c_functions_s +{ + char *name; + lua_CFunction func; +}; +typedef struct lua_c_functions_s lua_c_functions_t; + static char base_path[PATH_MAX + 1] = ""; static lua_script_t *scripts = NULL; +/* Cleans up the stack, pushes the return value as a number onto the stack and + * returns the number of values returned (1). */ +#define RETURN_LUA(l,status) do { \ + lua_State *_l_state = (l); \ + lua_settop (_l_state, 0); \ + lua_pushnumber (_l_state, (lua_Number) (status)); \ + return (1); \ +} while (0) + +/* + * Exported functions + */ +static int lua_cb_log (lua_State *l) /* {{{ */ +{ + int nargs = lua_gettop (l); /* number of arguments */ + int severity; + const char *msg; + + if (nargs != 2) + { + WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).", + nargs); + RETURN_LUA (l, -1); + } + + if (!lua_isnumber (l, 1)) + { + WARNING ("lua plugin: The first argument to collectd_log() must be a number."); + RETURN_LUA (l, -1); + } + + if (!lua_isstring (l, 2)) + { + WARNING ("lua plugin: The second argument to collectd_log() must be a string."); + RETURN_LUA (l, -1); + } + + severity = (int) lua_tonumber (l, /* stack pos = */ 1); + if ((severity != LOG_ERR) + && (severity != LOG_WARNING) + && (severity != LOG_NOTICE) + && (severity != LOG_INFO) + && (severity != LOG_DEBUG)) + severity = LOG_ERR; + + msg = lua_tostring (l, 2); + if (msg == NULL) + { + ERROR ("lua plugin: lua_tostring failed."); + RETURN_LUA (l, -1); + } + + plugin_log (severity, "%s", msg); + + RETURN_LUA (l, 0); +} /* }}} int lua_cb_log */ + +static lua_c_functions_t lua_c_functions[] = +{ + { "collectd_log", lua_cb_log } +}; + /* Declare the Lua libraries we wish to use. * Note: If you are opening and running a file containing Lua code using * 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries used in * that file here also. */ static const luaL_reg lua_load_libs[] = { + { LUA_COLIBNAME, luaopen_base }, + /* { "luaopen_loadlib", luaopen_loadlib }, */ #if COLLECT_DEBUG { LUA_DBLIBNAME, luaopen_debug }, #endif @@ -107,7 +176,9 @@ static int lua_script_init (lua_script_t *script) /* {{{ */ } /* Register all the functions we implement in C */ - register_exported_functions (script->lua_state); + for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++) + lua_register (script->lua_state, + lua_c_functions[i].name, lua_c_functions[i].func); return (0); } /* }}} int lua_script_init */ @@ -139,7 +210,7 @@ static int lua_script_load (const char *script_path) /* {{{ */ return (-1); } - status = luaL_loadfile (script->lua_state, script->script_path); + status = lua_dofile (script->lua_state, script->script_path); if (status != 0) { const char *errmsg; diff --git a/src/lua_exports.c b/src/lua_exports.c deleted file mode 100644 index a193f513..00000000 --- a/src/lua_exports.c +++ /dev/null @@ -1,40 +0,0 @@ -/** - * collectd - src/lua_exports.c - * Copyright (C) 2010 Julien Ammous - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; only version 2.1 of the License is - * applicable. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: - * Julien Ammous - **/ - - -/* this file contains the functions exported to lua scripts */ - -/* log_info(string) */ -static int log_info(lua_State *l) -{ - const char *message = lua_tostring(l, 1); - INFO("%s", message); - // return the number of values pushed on stack - return 0; -} - - -static int register_exported_functions(lua_State *l){ - lua_register(l, "log_info", log_info); - return 0; -} -