diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-02-14 15:27:11 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-02-14 15:27:11 +0800 |
commit | 34238fe5d53add4a325c60415edf0ba70407a498 (patch) | |
tree | 2180a2c03a2ae0db1a711276483a30b283b561bb /server/src/log.lua | |
parent | caec2f97984f72d0ecb5f0c35174c682e749ac8a (diff) | |
download | lua-language-server-34238fe5d53add4a325c60415edf0ba70407a498.zip |
重新初始化日志路径后把之前的日志内容转移过去
Diffstat (limited to 'server/src/log.lua')
-rw-r--r-- | server/src/log.lua | 47 |
1 files changed, 35 insertions, 12 deletions
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 |