diff --git a/extend/lualog/lualog/lualog.cpp b/extend/lualog/lualog/lualog.cpp index a3beb03e..e1074307 100644 --- a/extend/lualog/lualog/lualog.cpp +++ b/extend/lualog/lualog/lualog.cpp @@ -33,15 +33,20 @@ namespace logger { return "unsuppert data type"; } + int zformat(lua_State* L, log_level lvl, vstring tag, vstring feature, vstring msg) { + get_logger()->output(lvl, msg, tag, feature); + if (lvl == log_level::LOG_LEVEL_FATAL) { + lua_pushlstring(L, msg.data(), msg.size()); + return 1; + } + return 0; + } + template int tformat(lua_State* L, log_level lvl, vstring tag, vstring feature, int flag, vstring vfmt, std::index_sequence&&) { try { auto msg = fmt::format(vfmt, read_args(L, flag, integers + 6)...); - get_logger()->output(lvl, msg, tag, feature); - if (lvl == log_level::LOG_LEVEL_FATAL) { - lua_pushlstring(L, msg.c_str(), msg.size()); - return 1; - } + return zformat(L, lvl, tag, feature, msg); } catch (const exception& e) { luaL_error(L, "log format failed: %s!", e.what()); } @@ -69,7 +74,7 @@ namespace logger { vstring vfmt = lua_to_native(L, 5); int arg_num = lua_gettop(L) - 5; switch (arg_num) { - case 0: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<0>{}); + case 0: return zformat(L, lvl, tag, feature, vfmt); case 1: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<1>{}); case 2: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<2>{}); case 3: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<3>{}); @@ -78,7 +83,7 @@ namespace logger { case 6: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<6>{}); case 7: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<7>{}); case 8: return tformat(L, lvl, tag, feature, flag, vfmt, make_index_sequence<8>{}); - default: luaL_error(L, "test args is more than 8!"); break; + default: luaL_error(L, "log format args is more than 8!"); break; } return 0; }); diff --git a/script/sandbox.lua b/script/sandbox.lua index c7d29975..89faa1d2 100644 --- a/script/sandbox.lua +++ b/script/sandbox.lua @@ -6,23 +6,24 @@ local loadfile = loadfile local iopen = io.open local mabs = math.abs local ogetenv = os.getenv -local log_info = log.info -local log_err = log.error +local lprint = log.print local sformat = string.format local traceback = debug.traceback local file_time = stdfs.last_write_time +local LOG_LEVEL = log.LOG_LEVEL + local FEATURE = "devops" local TITLE = quanta.title local load_status = "success" local log_error = function(content) load_status = "failed" - log_err(content, TITLE, FEATURE) + lprint(LOG_LEVEL.ERROR, 0, TITLE, FEATURE, content) end local log_output = function(content) - log_info(content, TITLE) + lprint(LOG_LEVEL.INFO, 0, TITLE, FEATURE, content) end local function ssplit(str, token) @@ -112,9 +113,9 @@ function quanta.report(type) local divider = "----------------------------------------------------------------------------------------" local fmt = '{"type":"%s","pid":"%s","state":"%s","time":%s,"service":"%s"}' local str = sformat(fmt, type, quanta.pid, load_status, os.time(), quanta.name) - log_info(divider, TITLE, FEATURE) - log_info(str, TITLE, FEATURE) - log_info(divider, TITLE, FEATURE) + log_output(divider) + log_output(str) + log_output(divider) end function quanta.reload()