Lua binding: New API: `set_log_threshold` and `log`

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-07-21 13:26:02 +08:00
parent 9349adea75
commit 4ae06b8150
2 changed files with 39 additions and 2 deletions

View File

@ -22,18 +22,24 @@
local uloop = require "uloop"
local uh = require "uhttpd"
local verbose = true
local port = 8914
-- LOG_DEBUG LOG_INFO LOG_ERR
if not verbose then
uh.set_log_threshold(uh.LOG_ERR)
end
uloop.init()
print("uhttpd version:", uh.VERSION)
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
local srv = uh.new(port)
-- srv:set_options({docroot = "/home/zjh/www", index = "lua.html"})
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
print("Listen on:", port)
uh.log(uh.LOG_INFO, "Listen on:" .. port)
srv:set_error404_cb(function(cl, opt)
uh.send_header(cl, 200, "OK", -1)

View File

@ -19,6 +19,7 @@
#include <string.h>
#include "log.h"
#include "uhttpd.h"
#include "uhttpd-lua.h"
@ -336,6 +337,25 @@ static int lua_uh_request_done(lua_State *L)
return 0;
}
static int lua_uh_log(lua_State *L)
{
int priority = lua_tointeger(L, 1);
const char *msg = lua_tostring(L, 2);
luaL_where(L, 1);
ulog(priority, "%s%s\n", lua_tostring(L, -1), msg);
return 0;
}
static int lua_uh_set_log_threshold(lua_State *L)
{
ulog_threshold(lua_tointeger(L, 1));
return 0;
}
static const luaL_Reg uhttpd_fun[] = {
{"new", lua_uh_new},
{"send_header", lua_uh_send_header},
@ -346,6 +366,8 @@ static const luaL_Reg uhttpd_fun[] = {
{"send_error", lua_uh_send_error},
{"redirect", lua_uh_redirect},
{"request_done", lua_uh_request_done},
{"log", lua_uh_log},
{"set_log_threshold", lua_uh_set_log_threshold},
{NULL, NULL}
};
@ -370,5 +392,14 @@ int luaopen_uhttpd(lua_State *L)
#endif
lua_setfield(L, -2, "SSL_SUPPORTED");
lua_pushinteger(L, LOG_DEBUG);
lua_setfield(L, -2, "LOG_DEBUG");
lua_pushinteger(L, LOG_INFO);
lua_setfield(L, -2, "LOG_INFO");
lua_pushinteger(L, LOG_ERR);
lua_setfield(L, -2, "LOG_ERR");
return 1;
}