diff options
author | Christian Pointner <equinox@anylike.org> | 2009-12-27 14:54:08 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anylike.org> | 2009-12-27 14:54:08 +0000 |
commit | f50813574d97529a81c2f7afb962a06846c9167f (patch) | |
tree | 4fb9bc4bc3bed7d8722beb543f220bb1dbe2474b | |
parent | fixed strip target (diff) |
implemented log.printf as replacement for log.print
-rw-r--r-- | src/echo_server.lua | 14 | ||||
-rw-r--r-- | src/l_log.c | 22 | ||||
-rw-r--r-- | src/main_loop.lua | 4 |
3 files changed, 27 insertions, 13 deletions
diff --git a/src/echo_server.lua b/src/echo_server.lua index ee23cb1..8ebfcf0 100644 --- a/src/echo_server.lua +++ b/src/echo_server.lua @@ -29,34 +29,34 @@ function echo_server() local host, port = "localhost", 4500 local ip, err = socket.dns.toip(host) if(ip == nil) then - log.print("ERROR", "can't resolve " .. host .. ": " .. err) + log.printf("ERROR", "can't resolve %s: %s", host, err) return -1 end local udp, err = socket.udp() if(udp == nil) then - log.print("ERROR", "can't create udp socket") + log.printf("ERROR", "can't create udp socket") return -1 end local ret, err = udp:setsockname(ip, port) if(ret == nil) then - log.print("ERROR", "setsockname(" .. ip .. "," .. port .. ") failed: " .. err) + log.printf("ERROR", "setsockname(%s,%s) failed: %s", ip, port, err) return -1 end - log.print("NOTICE", "echo server listening on " .. ip .. ":" .. port); + log.printf("NOTICE", "echo server listening on %s:%s", ip, port); while true do local dgrm, from_ip, from_port = udp:receivefrom() - if(ret == nil) then - log.print("ERROR", "receivefrom(" .. ip .. "," .. port .. ") failed: " .. from_ip) + if(dgrm == nil) then + log.printf("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.print("ERROR", "sendto(" .. ip .. "," .. port .. ") failed: " .. err) + log.printf("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 45dcac4..9fd8988 100644 --- a/src/l_log.c +++ b/src/l_log.c @@ -62,8 +62,21 @@ static int l_log_add_target(lua_State *L) } /* void log_printf(log_prio_t prio, const char* fmt, ...); */ -static int l_log_print(lua_State *L) +static int l_log_printf(lua_State *L) { + int numargs = lua_gettop(L); + if(numargs < 2) + return luaL_error(L, "log.printf too few arguments"); + + if(numargs > 2) { + lua_getglobal(L, "string"); + lua_pushliteral(L, "format"); + lua_gettable(L, -2); + lua_insert(L, 2); + lua_remove(L, -1); + lua_call(L, numargs - 1, 1); + } + const char* prio_str = luaL_checkstring(L,1); log_prio_t prio; if(prio_str) { @@ -78,22 +91,23 @@ static int l_log_print(lua_State *L) else if(!strncmp("DEBUG", prio_str, 6)) prio = DEBUG; else - return luaL_error(L, "invalid log priority"); + return luaL_error(L, "log.printf invalid log priority"); } else - return luaL_error(L, "invalid log priority"); + return luaL_error(L, "log.printf invalid log priority"); log_printf(prio, "%s", luaL_checkstring(L, 2)); return 0; } + /* void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len); */ static const struct luaL_reg log_funcs [] = { { "init", l_log_init }, { "close", l_log_close }, { "add_target", l_log_add_target }, - { "print", l_log_print }, + { "printf", l_log_printf }, { NULL, NULL } }; diff --git a/src/main_loop.lua b/src/main_loop.lua index 76691f7..8124298 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -24,7 +24,7 @@ -- function main_loop(opt) - log.print("NOTICE", "main_loop started") - + log.printf("NOTICE", "main_loop started") + return echo_server() end |