summaryrefslogtreecommitdiff
path: root/server/src/log.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/log.lua')
-rw-r--r--server/src/log.lua47
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