From 1863e14b348e7e09c421ae4ccd2cdf2204d46f0f Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Sun, 17 May 2020 13:16:29 -0400 Subject: [PATCH] Add a loggging utility adds a function to the global namespace, log() as well as constants from syslog(3) for logging messages. --- src/libkore.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/libkore.c b/src/libkore.c index 350e315..5c270f3 100644 --- a/src/libkore.c +++ b/src/libkore.c @@ -9,6 +9,9 @@ #include //#include //linux only I guess #include "libkore.h" +#include + +#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); }