+/* 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 }
+};
+