#include <sys/types.h>
#include <errno.h>
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
int luaopen_syslog(lua_State *);
static int
syslog_openlog(lua_State *L)
{
const char *ident;
int option;
int facility;
ident = luaL_checkstring(L, 1);
option = luaL_checkinteger(L, 2);
facility = luaL_checkinteger(L, 3);
openlog(ident, option, facility);
return 0;
}
static int
syslog_syslog(lua_State *L)
{
syslog((int) luaL_checkinteger(L, 1), "%s", luaL_checkstring(L, 2));
return 0;
}
static int
syslog_closelog(lua_State *L)
{
closelog();
return 0;
}
static int
syslog_setlogmask(lua_State *L)
{
lua_pushinteger(L, setlogmask((int) luaL_checkinteger(L, 1)));
return 1;
}
static void
syslog_set_info(lua_State *L)
{
lua_pushliteral(L, "_COPYRIGHT");
lua_pushliteral(L, "Copyright (C) 2013 by "
"Marc Balmer <marc@msys.ch>");
lua_settable(L, -3);
lua_pushliteral(L, "_DESCRIPTION");
lua_pushliteral(L, "syslog binding for Lua");
lua_settable(L, -3);
lua_pushliteral(L, "_VERSION");
lua_pushliteral(L, "syslog 1.0.0");
lua_settable(L, -3);
}
struct constant {
const char *name;
int value;
};
#define CONSTANT(NAME) { #NAME, NAME }
static struct constant syslog_constant[] = {
CONSTANT(LOG_CONS),
CONSTANT(LOG_NDELAY),
CONSTANT(LOG_NOWAIT),
CONSTANT(LOG_ODELAY),
CONSTANT(LOG_PERROR),
CONSTANT(LOG_PID),
CONSTANT(LOG_AUTH),
CONSTANT(LOG_AUTHPRIV),
CONSTANT(LOG_CRON),
CONSTANT(LOG_DAEMON),
CONSTANT(LOG_FTP),
CONSTANT(LOG_KERN),
CONSTANT(LOG_LOCAL0),
CONSTANT(LOG_LOCAL1),
CONSTANT(LOG_LOCAL2),
CONSTANT(LOG_LOCAL3),
CONSTANT(LOG_LOCAL4),
CONSTANT(LOG_LOCAL5),
CONSTANT(LOG_LOCAL6),
CONSTANT(LOG_LOCAL7),
CONSTANT(LOG_LPR),
CONSTANT(LOG_MAIL),
CONSTANT(LOG_NEWS),
CONSTANT(LOG_SYSLOG),
CONSTANT(LOG_USER),
CONSTANT(LOG_UUCP),
CONSTANT(LOG_EMERG),
CONSTANT(LOG_ALERT),
CONSTANT(LOG_CRIT),
CONSTANT(LOG_ERR),
CONSTANT(LOG_WARNING),
CONSTANT(LOG_NOTICE),
CONSTANT(LOG_INFO),
CONSTANT(LOG_DEBUG),
{ NULL, 0 }
};
int
luaopen_syslog(lua_State *L)
{
int n;
struct luaL_Reg luasyslog[] = {
{ "openlog", syslog_openlog },
{ "syslog", syslog_syslog },
{ "closelog", syslog_closelog },
{ "setlogmask", syslog_setlogmask },
{ NULL, NULL }
};
#if LUA_VERSION_NUM >= 502
luaL_newlib(L, luasyslog);
#else
luaL_register(L, "syslog", luasyslog);
#endif
syslog_set_info(L);
for (n = 0; syslog_constant[n].name != NULL; n++) {
lua_pushinteger(L, syslog_constant[n].value);
lua_setfield(L, -2, syslog_constant[n].name);
};
return 1;
}