Add a loggging utility

adds a function to the global namespace, log() as well as constants
from syslog(3) for logging messages.
This commit is contained in:
Robin Malley 2020-05-17 13:16:29 -04:00
parent 13883cccd7
commit 1863e14b34
1 changed files with 31 additions and 0 deletions

View File

@ -9,6 +9,9 @@
#include <lualib.h>
//#include <inet/in.h>//linux only I guess
#include "libkore.h"
#include <syslog.h>
#define LUA_PUSH_CONST(L,a) lua_pushnumber(L,a); lua_setfield(L,-2,#a);
struct http_request*
luaL_checkrequest(lua_State *L, int pos){
@ -252,6 +255,18 @@ lhttp_file_get(lua_State *L){
return 1;
}
/*
log(priority,string) //formating must be done before calling
*/
int
lkore_log(lua_State *L){
const char *str = luaL_checkstring(L,-1);
int prio = luaL_checkint(L,-2);
lua_pop(L,2);
kore_log(prio,"%s",str);
return 0;
}
static const luaL_Reg kore_funcs[] = {
{"http_response", lhttp_response},
{"http_response_header", lhttp_response_header},
@ -267,6 +282,7 @@ static const luaL_Reg kore_funcs[] = {
{"http_populate_cookies",lhttp_populate_cookies},
{"http_populate_multipart_form",lhttp_populate_multipart_form},
{"http_file_get",lhttp_file_get},
{"log",lkore_log},
{NULL,NULL}
};
@ -284,5 +300,20 @@ load_kore_libs(lua_State *L){
lua_getglobal(L,"_G");
luaL_register(L,NULL,kore_funcs);
//Push priority constants for use with log()
LUA_PUSH_CONST(L,LOG_EMERG);
LUA_PUSH_CONST(L,LOG_ALERT);
LUA_PUSH_CONST(L,LOG_CRIT);
LUA_PUSH_CONST(L,LOG_ERR);
LUA_PUSH_CONST(L,LOG_WARNING);
LUA_PUSH_CONST(L,LOG_NOTICE);
LUA_PUSH_CONST(L,LOG_INFO);
LUA_PUSH_CONST(L,LOG_DEBUG);
#ifdef BUILD_PROD
lua_pushboolean(1);
#else
lua_pushboolean(0);
#endif
lua_setfield(L,-2,"PRODUCTION");
lua_pop(L,1);
}