diff options
Diffstat (limited to 'server-beta')
-rw-r--r-- | server-beta/src/brave/log.lua | 6 | ||||
-rw-r--r-- | server-beta/src/log.lua | 128 |
2 files changed, 75 insertions, 59 deletions
diff --git a/server-beta/src/brave/log.lua b/server-beta/src/brave/log.lua index d86be162..cd27cd55 100644 --- a/server-beta/src/brave/log.lua +++ b/server-beta/src/brave/log.lua @@ -19,8 +19,10 @@ local function pushLog(level, ...) end local info = debugGetInfo(3, 'Sl') brave.push('log', { - msg = str, - info = info, + level = level, + msg = str, + src = info.source, + line = info.currentline, }) return str end diff --git a/server-beta/src/log.lua b/server-beta/src/log.lua index cd62df97..9d0889be 100644 --- a/server-beta/src/log.lua +++ b/server-beta/src/log.lua @@ -1,116 +1,130 @@ local fs = require 'bee.filesystem' -local log = {} +local osTime = os.time +local osClock = os.clock +local osDate = os.date +local ioOpen = io.open +local tablePack = table.pack +local tableConcat = table.concat +local tostring = tostring +local debugTraceBack = debug.traceback +local mathModf = math.modf +local debugGetInfo = debug.getinfo +local ioStdErr = io.stderr -log.file = nil -log.start_time = os.time() - os.clock() -log.size = 0 -log.max_size = 100 * 1024 * 1024 +_ENV = nil -local function trim_src(src) - src = src:sub(log.prefix_len + 3, -5) +local m = {} + +m.file = nil +m.startTime = osTime() - osClock() +m.size = 0 +m.maxSize = 100 * 1024 * 1024 + +local function trimSrc(src) + src = src:sub(m.prefixLen + 3, -5) src = src:gsub('^[/\\]+', '') src = src:gsub('[\\/]+', '.') 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 + if not m.file then + m.file = ioOpen(m.path, 'w') + if not m.file then return end - log.file:write('') - log.file:close() - log.file = io.open(log.path, 'ab') - if not log.file then + m.file:write('') + m.file:close() + m.file = ioOpen(m.path, 'ab') + if not m.file then return end - log.file:setvbuf 'no' + m.file:setvbuf 'no' end end local function pushLog(level, ...) - if not log.path then + if not m.path then return end - if log.size > log.max_size then + if m.size > m.maxSize then return end - local t = table.pack(...) + local t = tablePack(...) for i = 1, t.n do t[i] = tostring(t[i]) end - local str = table.concat(t, '\t', 1, t.n) + local str = tableConcat(t, '\t', 1, t.n) if level == 'error' then - str = str .. '\n' .. debug.traceback(nil, 3) - io.stderr:write(str .. '\n') - end - init_log_file() - if not log.file then - return - end - local sec, ms = math.modf(log.start_time + os.clock()) - local timestr = os.date('%Y-%m-%d %H:%M:%S', sec) - local info = debug.getinfo(3, 'Sl') - local buf - if info and info.currentline > 0 then - buf = ('[%s.%03.f][%s]: [%s:%s]%s\n'):format(timestr, ms * 1000, level, trim_src(info.source), info.currentline, str) - else - buf = ('[%s.%03.f][%s]: %s\n'):format(timestr, ms * 1000, level, str) - end - log.file:write(buf) - log.size = log.size + #buf - if log.size > log.max_size then - log.file:write('[REACH MAX SIZE]') + str = str .. '\n' .. debugTraceBack(nil, 3) + ioStdErr:write(str .. '\n') end - return str + local info = debugGetInfo(3, 'Sl') + return m.raw(level, str, info.source, info.currentline) end -function log.info(...) +function m.info(...) pushLog('info', ...) end -function log.debug(...) +function m.debug(...) pushLog('debug', ...) end -function log.trace(...) +function m.trace(...) pushLog('trace', ...) end -function log.warn(...) +function m.warn(...) pushLog('warn', ...) end -function log.error(...) +function m.error(...) pushLog('error', ...) end -function log.init(root, path) +function m.raw(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) + m.file:write(buf) + m.size = m.size + #buf + if m.size > m.maxSize then + m.file:write('[REACH MAX SIZE]') + end + return +end + +function m.init(root, path) local lastBuf - if log.file then - log.file:close() - log.file = nil - local file = io.open(log.path, 'rb') + if m.file then + m.file:close() + m.file = nil + local file = ioOpen(m.path, 'rb') if file then lastBuf = file:read 'a' file:close() end end - log.path = path:string() - log.prefix_len = #root:string() - log.size = 0 + m.path = path:string() + m.prefixLen = #root:string() + m.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 + if m.file then + m.file:write(lastBuf) + m.size = m.size + #lastBuf end end end -return log +return m |