local skynet = require"skynet" local skynet_util = require"skynet_util" local file_util = require"file_util" local time_util = require"time_util" require"skynet.manager"
localos = os localio = io localerror = error localassert = assert localprint = print localstring = string local math_floor = math.floor localtype = type localtable = table
local SELF_ADDRESS = skynet.self()
local file = nil local file_path = skynet.getenv('logpath') local file_name = skynet.getenv('logfilename') local daemon = skynet.getenv('daemon') local hook_hander_list = {}
localfunctionopen_file() ifnot daemon then return end if file then file:close() end print(file_path,file_name) os.execute('mkdir -p ' .. file_path) ifnotos.execute("mkdir -p " .. file_path) then error("create dir err") end local file_p = file_util.path_join(file_path,file_name) file = io.open(file_p, 'a+') assert(file, "can`t open file " .. file_p) end
skynet.register_protocol { name = "text", id = skynet.PTYPE_TEXT, unpack = skynet.tostring, dispatch = function(_, address, msg) local cur_time = time_util.skynet_int_time() local second,m = math_floor(cur_time / 100), cur_time % 100 local time_date = os.date('[%Y%m%d:%H:%M:%S ',second) local log_str = time_date .. m .. ']' .. msg if file then file:write(log_str .. '\n') file:flush() else print(log_str) end
if address ~= SELF_ADDRESS then for i = 1,#hook_hander_list do hook_hander_list[i](msg) end end end }
skynet.register_protocol { name = "SYSTEM", id = skynet.PTYPE_SYSTEM, unpack = function(...)return ... end, dispatch = function() -- reopen signal open_file() end }
local CMD = {}
functionCMD.add_hook(file_name) local func = require(file_name) assert(type(func) == 'function', "err file " .. file_name) table.insert(hook_hander_list, func) returntrue end