lua plugin: Be more verbose when loading a script fails.
[collectd.git] / src / lua.c
index b79e0b1..58c96ad 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
  *   Florian Forster <octo at collectd.org>
  **/
 
+/* <lua5.1/luaconf.h> defines a macro using "sprintf". Although not used here,
+ * GCC will complain about the macro definition. */
+#define DONT_POISON_SPRINTF_YET
+
 #include "collectd.h"
 #include "plugin.h"
 #include "common.h"
 #include <lauxlib.h>
 #include <lualib.h>
 
+#if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
+# undef sprintf
+# pragma GCC poison sprintf
+#endif
+
 typedef struct lua_script_s {
   char          *script_path;
   lua_State     *lua_state;
@@ -222,23 +231,6 @@ static lua_c_functions_t lua_c_functions[] =
   { "collectd_register_read", lua_cb_register_read }
 };
 
-/* 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
-  { LUA_TABLIBNAME,  luaopen_table  },
-  { LUA_IOLIBNAME,   luaopen_io     },
-  { LUA_STRLIBNAME,  luaopen_string },
-  { LUA_MATHLIBNAME, luaopen_math   }
-};
-
 static void lua_script_free (lua_script_t *script) /* {{{ */
 {
   lua_script_t *next;
@@ -276,16 +268,8 @@ static int lua_script_init (lua_script_t *script) /* {{{ */
     return (-1);
   }
 
-  /* Open up all the Lua libraries declared above. */
-  for (i = 0; i < STATIC_ARRAY_SIZE (lua_load_libs); i++)
-  {
-    int status;
-
-    status = (*lua_load_libs[i].func) (script->lua_state);
-    if (status != 0)
-      WARNING ("lua plugin: Loading library \"%s\" failed.",
-          lua_load_libs[i].name);
-  }
+  /* Open up all the standard Lua libraries. */
+  luaL_openlibs (script->lua_state);
 
   /* Register all the functions we implement in C */
   for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
@@ -322,21 +306,31 @@ static int lua_script_load (const char *script_path) /* {{{ */
     return (-1);
   }
 
-  status = lua_dofile (script->lua_state, script->script_path);
+  status = luaL_loadfile (script->lua_state, script->script_path);
+  if (status != 0)
+  {
+    ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
+    lua_script_free (script);
+    return (-1);
+  }
+
+  status = lua_pcall (script->lua_state,
+      /* nargs = */    0,
+      /* nresults = */ LUA_MULTRET,
+      /* errfunc = */  0);
   if (status != 0)
   {
     const char *errmsg;
 
-    switch (status)
-    {
-      case LUA_ERRSYNTAX: errmsg = "Syntax error"; break;
-      case LUA_ERRFILE:   errmsg = "File I/O error"; break;
-      case LUA_ERRMEM:    errmsg = "Memory allocation error"; break;
-      default:            errmsg = "Unexpected error";
-    }
+    errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
 
-    ERROR ("lua plugin: Loading script \"%s\" failed: %s",
-        script->script_path, errmsg);
+    if (errmsg == NULL)
+      ERROR ("lua plugin: lua_pcall failed with status %i. "
+          "In addition, no error message could be retrieved from the stack.",
+          status);
+    else
+      ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
+          script->script_path, errmsg);
 
     lua_script_free (script);
     return (-1);