Skip to content

Instantly share code, notes, and snippets.

@xjdrew
Created August 24, 2017 08:50
Show Gist options
  • Save xjdrew/7fa6660b029cd190e24af9859e540d5b to your computer and use it in GitHub Desktop.
Save xjdrew/7fa6660b029cd190e24af9859e540d5b to your computer and use it in GitHub Desktop.
lsyslog.c
#include <syslog.h>
#include <lua.h>
#include <lauxlib.h>
#define LSYSLOG_FCOUNT 8
static int facilitys[LSYSLOG_FCOUNT] = {
LOG_LOCAL0,
LOG_LOCAL1,
LOG_LOCAL2,
LOG_LOCAL3,
LOG_LOCAL4,
LOG_LOCAL5,
LOG_LOCAL6,
LOG_LOCAL7,
};
/*
* args: appname, facility, to_screen
*/
static int
lopenlog(lua_State *L) {
const char* appname = luaL_checkstring(L, 1);
int findex = luaL_checkinteger(L, 2);
int to_screen = luaL_checkinteger(L, 3);
int facility = facilitys[findex%LSYSLOG_FCOUNT];
if(to_screen) openlog(appname, LOG_ODELAY | LOG_PERROR, facility);
else openlog(appname, LOG_ODELAY, facility);
return 0;
}
static int
lcloselog(lua_State *L) {
closelog();
return 0;
}
/*
* args: priority, msg, facility(optional)
*/
static int
lsyslog(lua_State *L) {
int priority = LOG_PRI(luaL_checkinteger(L, 1));
const char* msg = luaL_checkstring(L, 2);
int facility = 0;
if (lua_gettop(L) >= 3) {
int findex = luaL_checkinteger(L,3);
facility = facilitys[findex%LSYSLOG_FCOUNT];
}
syslog(facility|priority, "%s", msg);
return 0;
}
int luaopen_syslog(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{"openlog", lopenlog},
{"syslog", lsyslog},
{"closelog", lcloselog},
{NULL, NULL},
};
luaL_newlib(L, l);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment