1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-24 09:20:06 +08:00
SpaceVim/lua/spacevim/api/logger.lua

166 lines
4.1 KiB
Lua
Raw Normal View History

2022-01-04 23:15:17 +08:00
--=============================================================================
-- logger.lua --- logger api implemented in lua
-- Copyright (c) 2016-2019 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
local fn = vim.fn or require('spacevim').fn
2021-08-14 15:27:12 +08:00
local cmd = require('spacevim').cmd
local M = {
['name'] = '',
['silent'] = 1,
['level'] = 1,
['verbose'] = 1,
['file'] = '',
['temp'] = {},
}
2021-08-15 19:06:48 +08:00
-- 0 : log debug, info, warn, error messages
-- 1 : log info, warn, error messages
-- 2 : log warn, error messages
-- 3 : log error messages
M.levels = {'Info ', 'Warn ', 'Error', 'Debug'}
2021-11-13 21:22:47 +08:00
M.clock = fn.reltime()
2021-08-14 15:27:12 +08:00
function M.set_silent(sl)
M.silent = sl
end
function M.set_verbose(vb)
M.verbose = vb
end
function M.set_level(l)
M.level = l
end
2021-08-15 19:06:48 +08:00
function M._build_msg(msg, l)
msg = msg or ''
-- local time = fn.strftime('%H:%M:%S')
-- error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key))
-- local log = '[ ' .. M.name .. ' ] [' .. time .. '] [ ' .. M.levels[l] .. '] ' .. msg
-- change the format to
-- [ name ] [00:00:00:000] [level] msg
local clock = fn.reltimefloat(fn.reltime(M.clock))
local h = fn.float2nr(clock / 60 / 60)
local m = fn.float2nr(clock / 60)
local s = fn.float2nr(clock) % 60
local mic = string.format('%00.3f', clock - fn.float2nr(clock))
local c = string.format('%02d:%02d:%02d:%s', h, m, s, string.sub(mic, 3, -1))
local log = string.format('[ %s ] [%s] [ %s ] %s',
2021-11-13 21:22:47 +08:00
M.name,
c,
2021-11-13 21:22:47 +08:00
M.levels[l],
msg)
2021-08-15 19:06:48 +08:00
return log
end
function M.debug(msg)
if M.level <= 0 then
local log = M._build_msg(msg, 4)
if M.silent == 0 and M.verbose >= 4 then
cmd('echom "' .. log .. '"')
end
M.write(log)
end
end
function M.error(msg)
local log = M._build_msg(msg, 3)
2021-08-14 15:27:12 +08:00
if M.silent == 0 and M.verbose >= 1 then
cmd('echohl Error')
cmd('echom "' .. log .. '"')
cmd('echohl None')
end
M.write(log)
end
function M.write(msg)
table.insert(M.temp, msg)
if M.file ~= '' then
if fn.isdirectory(fn.fnamemodify(M.file, ':p:h')) == 0 then
fn.mkdir(fn.expand(fn.fnamemodify(M.file, ':p:h')), 'p')
end
local flags = ''
if fn.filereadable(M.file) == 1 then
flags = 'a'
end
fn.writefile({msg}, M.file, flags)
end
end
function M.warn(msg, ...)
if M.level <= 2 then
2021-08-15 19:06:48 +08:00
local log = M._build_msg(msg, 2)
2021-08-14 15:27:12 +08:00
if (M.silent == 0 and M.verbose >= 2) or select(1, ...) == 1 then
cmd('echohl WarningMsg')
cmd('echom "' .. log .. '"')
cmd('echohl None')
end
M.write(log)
end
end
function M.info(msg)
if M.level <= 1 then
2021-08-15 19:06:48 +08:00
local log = M._build_msg(msg, 1)
2021-08-14 15:27:12 +08:00
if M.silent == 0 and M.verbose >= 3 then
cmd('echom "' .. log .. '"')
end
M.write(log)
end
end
2021-08-14 20:51:00 +08:00
function M.view(l)
2021-08-15 19:06:48 +08:00
local info = ''
local logs = ''
if fn.filereadable(M.file) == 1 then
logs = fn.readfile(M.file, '')
info = info .. fn.join(fn.filter(logs, 'self._comp(v:val, a:l)'), "\n")
else
info = info .. '[ ' .. M.name .. ' ] : logger file ' .. M.file
.. ' does not exists, only log for current process will be shown!'
.. "\n"
for key, value in pairs(M.temp) do
if M._comp(value, l) == 1 then
info = info .. value .. "\n"
end
end
end
return info
2021-08-14 20:51:00 +08:00
end
2021-08-15 19:06:48 +08:00
function M._comp(msg, l)
-- if a:msg =~# '\[ ' . self.name . ' \] \[\d\d\:\d\d\:\d\d\] \[ '
if string.find(msg, M.levels[2]) ~= nil then
return 1
elseif string.find(msg, M.levels[1]) ~= nil then
if l > 2 then return 0 else return 1 end
else
if l > 1 then return 0 else return 1 end
end
2021-08-14 20:51:00 +08:00
end
2021-08-14 15:27:12 +08:00
function M.set_name(name)
M.name = name
end
function M.get_name()
return M.name
end
function M.set_file(file)
M.file = file
end
return M