From 34238fe5d53add4a325c60415edf0ba70407a498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 14 Feb 2019 15:27:11 +0800 Subject: =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=B7=AF=E5=BE=84=E5=90=8E=E6=8A=8A=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E6=97=A5=E5=BF=97=E5=86=85=E5=AE=B9=E8=BD=AC=E7=A7=BB?= =?UTF-8?q?=E8=BF=87=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/log.lua | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'server/src/log.lua') diff --git a/server/src/log.lua b/server/src/log.lua index c4fa9461..5c04cdb9 100644 --- a/server/src/log.lua +++ b/server/src/log.lua @@ -13,6 +13,22 @@ local function trim_src(src) return src end +local function init_log_file() + if not log.file then + log.file = io.open(log.path, 'w') + if not log.file then + return + end + log.file:write('') + log.file:close() + log.file = io.open(log.path, 'ab') + if not log.file then + return + end + log.file:setvbuf 'no' + end +end + local function push_log(level, ...) if not log.path then return @@ -28,18 +44,9 @@ local function push_log(level, ...) if level == 'error' then str = str .. '\n' .. debug.traceback(nil, 3) end + init_log_file() if not log.file then - log.file = io.open(log.path, 'w') - if not log.file then - return - end - log.file:write('') - log.file:close() - log.file = io.open(log.path, 'ab') - if not log.file then - return - end - log.file:setvbuf 'no' + return end local sec, ms = math.modf(log.start_time + os.clock()) local timestr = os.date('%Y-%m-%d %H:%M:%S', sec) @@ -79,13 +86,29 @@ function log.error(...) end function log.init(root, path) + local lastBuf + if log.file then + log.file:close() + log.file = nil + local file = io.open(log.path, 'rb') + if file then + lastBuf = file:read 'a' + file:close() + end + end log.path = path:string() log.prefix_len = #root:string() + 3 - log.file = nil log.size = 0 if not fs.exists(path:parent_path()) then fs.create_directories(path:parent_path()) end + if lastBuf then + init_log_file() + if log.file then + log.file:write(lastBuf) + log.size = log.size + #lastBuf + end + end end return log -- cgit v1.2.3