From 0223ac68b17a2bb5ab562840f158b86e0de6b015 Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Wed, 20 Sep 2023 18:28:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/lualog/lualog/lualog.cpp | 57 +++++++++++++++++++++++++++++++++ server/test.lua | 2 +- server/test/log_test.lua | 3 ++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/extend/lualog/lualog/lualog.cpp b/extend/lualog/lualog/lualog.cpp index eafebae5..f360386d 100644 --- a/extend/lualog/lualog/lualog.cpp +++ b/extend/lualog/lualog/lualog.cpp @@ -1,8 +1,45 @@ #define LUA_LIB +#include #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 + string vvformat(vstring vfmt, Args... args) { + return fmt::format(vfmt, args...); + } + + + template + void tformat(lua_State* L, string& msg, vstring vfmt, std::index_sequence&&) { + 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(); @@ -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); }); diff --git a/server/test.lua b/server/test.lua index 3ce0c88c..8e936581 100644 --- a/server/test.lua +++ b/server/test.lua @@ -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) diff --git a/server/test/log_test.lua b/server/test/log_test.lua index 721eba79..1299d601 100644 --- a/server/test/log_test.lua +++ b/server/test/log_test.lua @@ -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()