From 82c9e8b4f4ed0168ca439544f60166c81bd10778 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 29 Dec 2009 00:20:18 +0000 Subject: switched to constants at log table --- src/echo_server.lua | 12 ++++++------ src/l_log.c | 35 ++++++++++++++++------------------- src/main_loop.lua | 4 ++-- 3 files changed, 24 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/echo_server.lua b/src/echo_server.lua index 3c35337..da43185 100644 --- a/src/echo_server.lua +++ b/src/echo_server.lua @@ -28,23 +28,23 @@ socket = require("socket") function echo_server_init(host, port) local ip, err = socket.dns.toip(host) if(ip == nil) then - log.printf("ERROR", "can't resolve %s: %s", host, err) + log.printf(log.ERROR, "can't resolve %s: %s", host, err) return nil end local udp, err = socket.udp() if(udp == nil) then - log.printf("ERROR", "can't create udp socket") + log.printf(log.ERROR, "can't create udp socket") return nil end local ret, err = udp:setsockname(ip, port) if(ret == nil) then - log.printf("ERROR", "setsockname(%s,%s) failed: %s", ip, port, err) + log.printf(log.ERROR, "setsockname(%s,%s) failed: %s", ip, port, err) return nil end - log.printf("NOTICE", "echo server listening on %s:%s", ip, port); + log.printf(log.NOTICE, "echo server listening on %s:%s", ip, port); return udp end @@ -52,13 +52,13 @@ end function echo_server_recv(udp) local dgrm, from_ip, from_port = udp:receivefrom() if(dgrm == nil) then - log.printf("ERROR", "receivefrom(%s,%s) failed: %s", ip, port, from_ip) + log.printf(log.ERROR, "receivefrom(%s,%s) failed: %s", ip, port, from_ip) return -1 end local ret, err = udp:sendto(dgrm, from_ip, from_port) if(ret == nil) then - log.printf("ERROR", "sendto(%s,%s) failed: %s", ip, port, err) + log.printf(log.ERROR, "sendto(%s,%s) failed: %s", ip, port, err) return -1 end diff --git a/src/l_log.c b/src/l_log.c index 7cac3b5..890bddb 100644 --- a/src/l_log.c +++ b/src/l_log.c @@ -76,25 +76,7 @@ static int l_log_printf(lua_State *L) lua_call(L, numargs - 1, 1); } - const char* prio_str = luaL_checkstring(L,1); - log_prio_t prio; - if(prio_str) { - if(!strncmp("ERROR", prio_str, 6)) - prio = ERROR; - else if(!strncmp("WARNING", prio_str, 6)) - prio = WARNING; - else if(!strncmp("NOTICE", prio_str, 6)) - prio = NOTICE; - else if(!strncmp("INFO", prio_str, 6)) - prio = INFO; - else if(!strncmp("DEBUG", prio_str, 6)) - prio = DEBUG; - else - return luaL_error(L, "log.printf invalid log priority"); - } - else - return luaL_error(L, "log.printf invalid log priority"); - + log_prio_t prio = luaL_checkint(L,1); log_printf(prio, "%s", luaL_checkstring(L, 2)); return 0; } @@ -111,5 +93,20 @@ static const struct luaL_reg log_funcs [] = { LUALIB_API int luaopen_log(lua_State *L) { luaL_register(L, LUA_LOGLIBNAME, log_funcs); + lua_pushliteral(L, "ERROR"); + lua_pushinteger(L, ERROR); + lua_settable(L, -3); + lua_pushliteral(L, "WARNING"); + lua_pushinteger(L, WARNING); + lua_settable(L, -3); + lua_pushliteral(L, "NOTICE"); + lua_pushinteger(L, NOTICE); + lua_settable(L, -3); + lua_pushliteral(L, "INFO"); + lua_pushinteger(L, INFO); + lua_settable(L, -3); + lua_pushliteral(L, "DEBUG"); + lua_pushinteger(L, DEBUG); + lua_settable(L, -3); return 1; } diff --git a/src/main_loop.lua b/src/main_loop.lua index dd1289b..5fbea2f 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -26,7 +26,7 @@ socket = require("socket") function main_loop(opt) - log.printf("NOTICE", "main_loop started") + log.printf(log.NOTICE, "main_loop started") local sig = signal.init() local sock = {} @@ -40,7 +40,7 @@ function main_loop(opt) while return_value == 0 do local readable, _, err = socket.select({ sig , sock[1], sock[2] }, nil) if(err) then - log.printf("ERROR", "select returned with error: %s", err) + log.printf(log.ERROR, "select returned with error: %s", err) return_value = -1 else for _, input in ipairs(readable) do -- cgit v1.2.3