From e1964b7417431b86e99136a276bd13cad30ef99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 20 Sep 2019 10:22:13 +0800 Subject: =?UTF-8?q?=E6=94=AF=E6=8C=81=20log=20=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-beta/src/log.lua | 11 ++-- server-beta/src/pub/init.lua | 118 ++-------------------------------------- server-beta/src/pub/pub.lua | 127 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 117 deletions(-) create mode 100644 server-beta/src/pub/pub.lua (limited to 'server-beta/src') diff --git a/server-beta/src/log.lua b/server-beta/src/log.lua index 9d0889be..98dacd07 100644 --- a/server-beta/src/log.lua +++ b/server-beta/src/log.lua @@ -61,7 +61,7 @@ local function pushLog(level, ...) ioStdErr:write(str .. '\n') end local info = debugGetInfo(3, 'Sl') - return m.raw(level, str, info.source, info.currentline) + return m.raw(0, level, str, info.source, info.currentline) end function m.info(...) @@ -84,15 +84,18 @@ function m.error(...) pushLog('error', ...) end -function m.raw(level, msg, source, currentline) +function m.raw(thd, level, msg, source, currentline) init_log_file() if not m.file then return end local sec, ms = mathModf(m.startTime + osClock()) local timestr = osDate('%Y-%m-%d %H:%M:%S', sec) - local buf - buf = ('[%s.%03.f][%s]: [%s:%s]%s\n'):format(timestr, ms * 1000, level, trimSrc(source), currentline, msg) + local agl = '' + if #level < 5 then + agl = (' '):rep(5 - #level) + end + local buf = ('[%s.%03.f][%s]: %s[#%d:%s:%s]%s\n'):format(timestr, ms * 1000, level, agl, thd, trimSrc(source), currentline, msg) m.file:write(buf) m.size = m.size + #buf if m.size > m.maxSize then diff --git a/server-beta/src/pub/init.lua b/server-beta/src/pub/init.lua index a1bac3a8..f86a42b6 100644 --- a/server-beta/src/pub/init.lua +++ b/server-beta/src/pub/init.lua @@ -1,115 +1,7 @@ -local thread = require 'bee.thread' -local utility = require 'utility' -local task = require 'task' +local pub = require 'pub.pub' -local errLog = thread.channel 'errlog' -local type = type +pub.on('log', function (params, brave) + log.raw(brave.id, params.level, params.msg, params.src, params.line) +end) -local braveTemplate = [[ -package.path = %q -package.cpath = %q - -log = require 'brave.log' -local brave = require 'brave' -brave.register(%d) -]] - ----@class pub -local m = {} -m.type = 'pub' -m.braves = {} - ---- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务 ----@param num integer -function m.recruitBraves(num) - for _ = 1, num do - local id = #m.braves + 1 - log.info('Create brave:', id) - thread.newchannel('taskpad' .. id) - thread.newchannel('waiter' .. id) - m.braves[id] = { - id = id, - taskpad = thread.channel('taskpad' .. id), - waiter = thread.channel('waiter' .. id), - thread = thread.thread(braveTemplate:format( - package.path, - package.cpath, - id - )), - taskList = {}, - counter = utility.counter(), - currentTask = nil, - } - end -end - ---- 勇者是否有空 -function m.isIdle(brave) - return next(brave.taskList) == nil -end - ---- 给勇者推送任务 -function m.pushTask(brave, name, params) - local taskID = brave.counter() - brave.taskpad:push(name, taskID, params) - return task.wait(function (waker) - brave.taskList[taskID] = waker - end) -end - ---- 从勇者处接收任务反馈 -function m.popTask(brave, id, result) - local waker = brave.taskList[id] - if not waker then - log.warn(('Brave pushed unknown task result: [%d] => [%d]'):format(brave.id, id)) - return - end - brave.taskList[id] = nil - waker(result) -end - ---- 从勇者处接收报告 -function m.popReport(brave, name, params) -end - ---- 发布任务 ----@parma name string -function m.task(name, params) - for _, brave in ipairs(m.braves) do - if m.isIdle(brave) then - return m.pushTask(brave, name, params) - end - end -end - ---- 接收反馈 ----|返回接收到的反馈数量 ----@return integer -function m.recieve() - for _, brave in ipairs(m.braves) do - local suc, id, result = brave.waiter:pop() - if not suc then - goto CONTINUE - end - if type(id) == 'string' then - m.popReport(brave, id, result) - else - m.popTask(brave, id, result) - end - task.sleep(0) - ::CONTINUE:: - end -end - ---- 检查伤亡情况 -function m.checkDead() - while true do - local suc, err = errLog:pop() - if not suc then - break - end - log.error('Brave is dead!: ' .. err) - end -end - -return m +return pub diff --git a/server-beta/src/pub/pub.lua b/server-beta/src/pub/pub.lua new file mode 100644 index 00000000..9a1a6049 --- /dev/null +++ b/server-beta/src/pub/pub.lua @@ -0,0 +1,127 @@ +local thread = require 'bee.thread' +local utility = require 'utility' +local task = require 'task' + +local errLog = thread.channel 'errlog' +local type = type + +local braveTemplate = [[ +package.path = %q +package.cpath = %q + +log = require 'brave.log' +local brave = require 'brave' +brave.register(%d) +]] + +---@class pub +local m = {} +m.type = 'pub' +m.braves = {} +m.ability = {} + +--- 注册酒馆的功能 +function m.on(name, callback) + m.ability[name] = callback +end + +--- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务 +---@param num integer +function m.recruitBraves(num) + for _ = 1, num do + local id = #m.braves + 1 + log.info('Create brave:', id) + thread.newchannel('taskpad' .. id) + thread.newchannel('waiter' .. id) + m.braves[id] = { + id = id, + taskpad = thread.channel('taskpad' .. id), + waiter = thread.channel('waiter' .. id), + thread = thread.thread(braveTemplate:format( + package.path, + package.cpath, + id + )), + taskList = {}, + counter = utility.counter(), + currentTask = nil, + } + end +end + +--- 勇者是否有空 +function m.isIdle(brave) + return next(brave.taskList) == nil +end + +--- 给勇者推送任务 +function m.pushTask(brave, name, params) + local taskID = brave.counter() + brave.taskpad:push(name, taskID, params) + return task.wait(function (waker) + brave.taskList[taskID] = waker + end) +end + +--- 从勇者处接收任务反馈 +function m.popTask(brave, id, result) + local waker = brave.taskList[id] + if not waker then + log.warn(('Brave pushed unknown task result: # %d => [%d]'):format(brave.id, id)) + return + end + brave.taskList[id] = nil + waker(result) +end + +--- 从勇者处接收报告 +function m.popReport(brave, name, params) + local abil = m.ability[name] + if not abil then + log.warn(('Brave pushed unknown report: # %d => %q'):format(brave.id, name)) + return + end + abil(params, brave) +end + +--- 发布任务 +---@parma name string +function m.task(name, params) + for _, brave in ipairs(m.braves) do + if m.isIdle(brave) then + return m.pushTask(brave, name, params) + end + end +end + +--- 接收反馈 +---|返回接收到的反馈数量 +---@return integer +function m.recieve() + for _, brave in ipairs(m.braves) do + local suc, id, result = brave.waiter:pop() + if not suc then + goto CONTINUE + end + if type(id) == 'string' then + m.popReport(brave, id, result) + else + m.popTask(brave, id, result) + end + task.sleep(0) + ::CONTINUE:: + end +end + +--- 检查伤亡情况 +function m.checkDead() + while true do + local suc, err = errLog:pop() + if not suc then + break + end + log.error('Brave is dead!: ' .. err) + end +end + +return m -- cgit v1.2.3