Lua binding: New API: `set_log_threshold` and `log`
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>main
parent
9349adea75
commit
4ae06b8150
|
@ -22,18 +22,24 @@
|
||||||
local uloop = require "uloop"
|
local uloop = require "uloop"
|
||||||
local uh = require "uhttpd"
|
local uh = require "uhttpd"
|
||||||
|
|
||||||
|
local verbose = true
|
||||||
local port = 8914
|
local port = 8914
|
||||||
|
|
||||||
|
-- LOG_DEBUG LOG_INFO LOG_ERR
|
||||||
|
if not verbose then
|
||||||
|
uh.set_log_threshold(uh.LOG_ERR)
|
||||||
|
end
|
||||||
|
|
||||||
uloop.init()
|
uloop.init()
|
||||||
|
|
||||||
print("uhttpd version:", uh.VERSION)
|
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
|
||||||
|
|
||||||
local srv = uh.new(port)
|
local srv = uh.new(port)
|
||||||
|
|
||||||
-- srv:set_options({docroot = "/home/zjh/www", index = "lua.html"})
|
-- srv:set_options({docroot = "/home/zjh/www", index = "lua.html"})
|
||||||
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
|
-- 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)
|
srv:set_error404_cb(function(cl, opt)
|
||||||
uh.send_header(cl, 200, "OK", -1)
|
uh.send_header(cl, 200, "OK", -1)
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
#include "uhttpd.h"
|
#include "uhttpd.h"
|
||||||
#include "uhttpd-lua.h"
|
#include "uhttpd-lua.h"
|
||||||
|
|
||||||
|
@ -336,6 +337,25 @@ static int lua_uh_request_done(lua_State *L)
|
||||||
return 0;
|
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[] = {
|
static const luaL_Reg uhttpd_fun[] = {
|
||||||
{"new", lua_uh_new},
|
{"new", lua_uh_new},
|
||||||
{"send_header", lua_uh_send_header},
|
{"send_header", lua_uh_send_header},
|
||||||
|
@ -346,6 +366,8 @@ static const luaL_Reg uhttpd_fun[] = {
|
||||||
{"send_error", lua_uh_send_error},
|
{"send_error", lua_uh_send_error},
|
||||||
{"redirect", lua_uh_redirect},
|
{"redirect", lua_uh_redirect},
|
||||||
{"request_done", lua_uh_request_done},
|
{"request_done", lua_uh_request_done},
|
||||||
|
{"log", lua_uh_log},
|
||||||
|
{"set_log_threshold", lua_uh_set_log_threshold},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -370,5 +392,14 @@ int luaopen_uhttpd(lua_State *L)
|
||||||
#endif
|
#endif
|
||||||
lua_setfield(L, -2, "SSL_SUPPORTED");
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue