Skip to content

Commit

Permalink
日志模块优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Sep 21, 2023
1 parent 90c4bfb commit f872a89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
19 changes: 12 additions & 7 deletions extend/lualog/lualog/lualog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t... integers>
int tformat(lua_State* L, log_level lvl, vstring tag, vstring feature, int flag, vstring vfmt, std::index_sequence<integers...>&&) {
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());
}
Expand Down Expand Up @@ -69,7 +74,7 @@ namespace logger {
vstring vfmt = lua_to_native<vstring>(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>{});
Expand All @@ -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;
});
Expand Down
15 changes: 8 additions & 7 deletions script/sandbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f872a89

Please sign in to comment.