Skip to content

Commit

Permalink
日志模块优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Sep 20, 2023
1 parent 567c7e9 commit 0223ac6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
57 changes: 57 additions & 0 deletions extend/lualog/lualog/lualog.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
#define LUA_LIB
#include <variant>
#include "logger.h"

using namespace std;
using namespace luakit;

namespace logger {

thread_local luabuf buf;
string read_args(lua_State* L, int index) {
switch (lua_type(L, index)) {
case LUA_TNIL: return "nil";
case LUA_TTHREAD: return "thread";
case LUA_TFUNCTION: return "function";
case LUA_TUSERDATA: return "userdata";
case LUA_TLIGHTUSERDATA: return "userdata";
case LUA_TBOOLEAN: return (lua_tointeger(L, index) == 1) ? "true" : "false";
case LUA_TSTRING: return lua_tostring(L, index);
case LUA_TTABLE: {
buf.clean();
serialize_one(L, &buf, index, 1, false);
return (char*)buf.head();
}
case LUA_TNUMBER: {
return lua_isinteger(L, index) ? to_string(lua_tointeger(L, index)) : to_string(lua_tonumber(L, index));
}
}
return "unsuppert data type";
}

template<typename... Args>
string vvformat(vstring vfmt, Args... args) {
return fmt::format(vfmt, args...);
}


template<size_t... integers>
void tformat(lua_State* L, string& msg, vstring vfmt, std::index_sequence<integers...>&&) {
msg = vvformat(vfmt, read_args(L, integers + 3)...);
}

luakit::lua_table open_lualog(lua_State* L) {
luakit::kit_state kit_state(L);
auto lualog = kit_state.new_table();
Expand All @@ -14,6 +51,26 @@ namespace logger {
"ERROR", log_level::LOG_LEVEL_ERROR,
"FATAL", log_level::LOG_LEVEL_FATAL
);

lualog.set_function("test", [](lua_State* L, vstring vfmt, bool perty) {
std::string msg;
int arg_num = lua_gettop(L) - 2;
switch (arg_num) {
case 0: tformat(L, msg, vfmt, make_index_sequence<0>{}); break;
case 1: tformat(L, msg, vfmt, make_index_sequence<1>{}); break;
case 2: tformat(L, msg, vfmt, make_index_sequence<2>{}); break;
case 3: tformat(L, msg, vfmt, make_index_sequence<3>{}); break;
case 4: tformat(L, msg, vfmt, make_index_sequence<4>{}); break;
case 5: tformat(L, msg, vfmt, make_index_sequence<5>{}); break;
case 6: tformat(L, msg, vfmt, make_index_sequence<6>{}); break;
case 7: tformat(L, msg, vfmt, make_index_sequence<7>{}); break;
case 8: tformat(L, msg, vfmt, make_index_sequence<8>{}); break;
default: luaL_error(L, "test args is more than 8!"); break;
}
lua_pushlstring(L, msg.c_str(), msg.size());
return 1;
});

lualog.set_function("daemon", [](bool status) { get_logger()->daemon(status); });
lualog.set_function("set_max_line", [](size_t line) { get_logger()->set_max_line(line); });
lualog.set_function("set_clean_time", [](size_t time) { get_logger()->set_clean_time(time); });
Expand Down
2 changes: 1 addition & 1 deletion server/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ quanta.startup(function()--初始化test
import("test/detour_test.lua")
import("test/bitarray_test.lua")
]]
import("test/ws_test.lua")
import("test/log_test.lua")
end)
3 changes: 3 additions & 0 deletions server/test/log_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ for _, cycle in ipairs(params2) do
log_dump(string.format("logger_test: cycle %d use time %s ms!", cycle, time))
end

local tt = log.test("abc-{}-{}", false, 1, 3)
log_info("logger test :%s", tt)

log_info("logger test end")

--os.exit()

0 comments on commit 0223ac6

Please sign in to comment.