-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
binghe001
committed
Feb 9, 2025
1 parent
44406e6
commit ab86224
Showing
1 changed file
with
47 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,51 @@ | ||
-- main.lua | ||
|
||
local logger = require("logger") | ||
|
||
-- 配置表 | ||
local config = { | ||
max_task_queue_size = 2000, | ||
max_workers = 10, | ||
buffer_size = 8192, | ||
write_interval = 1000000, | ||
retry_times = 3, | ||
retry_delay = 200, | ||
max_memory_pool_blocks = 200, | ||
max_reconnect_attempts = 3, | ||
reconnect_delay = 2000000, | ||
-- logger.lua | ||
|
||
local logger = {} | ||
|
||
-- 日志记录器metatable | ||
local logger_metatable = { | ||
__gc = function(self) | ||
local logger_ptr = self.__ptr | ||
-- 销毁日志记录器 | ||
destroy_logger(logger_ptr) | ||
end, | ||
} | ||
|
||
-- 创建日志记录器实例 | ||
local log = logger.new(config) | ||
-- 创建新的日志记录器实例 | ||
function logger.new(config) | ||
local L = luaL_newstate() | ||
luaL_openlibs(L) | ||
|
||
-- 将配置表压入栈顶 | ||
lua_pushvalue(L, config) | ||
|
||
-- 调用luaopen_logger函数 | ||
local status, result = pcall(luaopen_logger, L) | ||
|
||
if not status then | ||
error("Failed to initialize logger: " .. result) | ||
end | ||
|
||
-- 检查结果是否为userdata | ||
if type(result) ~= 'userdata' then | ||
error("Logger initialization failed") | ||
end | ||
|
||
-- 设置metatable | ||
setmetatable(result, logger_metatable) | ||
|
||
return result | ||
end | ||
|
||
-- 记录日志方法 | ||
function logger:record(message) | ||
local logger_ptr = self.__ptr | ||
-- 在这里实现具体的日志记录逻辑 | ||
end | ||
|
||
-- 记录日志 | ||
log:record("Test message") | ||
-- 销毁日志记录器方法 | ||
function logger:destroy() | ||
local logger_ptr = self.__ptr | ||
end | ||
|
||
-- 销毁日志记录器 | ||
log:destroy() | ||
return logger |