mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-23 01:09:56 +08:00
feat(log): improve logger api
This commit is contained in:
parent
70b949a35d
commit
2425ef85e2
@ -15,10 +15,11 @@ if has('nvim-0.5.0')
|
|||||||
\ )
|
\ )
|
||||||
endfunction
|
endfunction
|
||||||
""
|
""
|
||||||
" write warning message to spacevim runtime log.
|
" write warning {msg} to spacevim runtime log.
|
||||||
" by default, the warning message will not be printed in cmdline, if
|
" The `msg` must be string. the second argument is optional, It can a
|
||||||
" `silent` is set, and is `0`, the warning message will be printed to
|
" boolean or `0/1`. By default, the warning message will not be printed,
|
||||||
" cmdline.
|
" if the second argument is given, and is `0` or false, the warning msg
|
||||||
|
" will be printed to screen.
|
||||||
function! SpaceVim#logger#warn(msg, ...) abort
|
function! SpaceVim#logger#warn(msg, ...) abort
|
||||||
let issilent = get(a:000, 0, 1)
|
let issilent = get(a:000, 0, 1)
|
||||||
lua require("spacevim.logger").warn(
|
lua require("spacevim.logger").warn(
|
||||||
|
@ -1683,9 +1683,10 @@ SpaceVim#logger#info({msg}) *SpaceVim#logger#info()*
|
|||||||
write message to SpaceVim runtime log with `info` level.
|
write message to SpaceVim runtime log with `info` level.
|
||||||
|
|
||||||
SpaceVim#logger#warn({msg}) *SpaceVim#logger#warn()*
|
SpaceVim#logger#warn({msg}) *SpaceVim#logger#warn()*
|
||||||
write warning message to spacevim runtime log. by default, the warning
|
write warning {msg} to spacevim runtime log. The `msg` must be string. the
|
||||||
message will not be printed in cmdline, if `silent` is set, and is `0`, the
|
second argument is optional, It can a boolean or `0/1`. By default, the
|
||||||
warning message will be printed to cmdline.
|
warning message will not be printed, if the second argument is given, and is
|
||||||
|
`0` or false, the warning msg will be printed to screen.
|
||||||
|
|
||||||
SpaceVim#logger#error({msg}) *SpaceVim#logger#error()*
|
SpaceVim#logger#error({msg}) *SpaceVim#logger#error()*
|
||||||
write error message to spacevim runtime log.
|
write error message to spacevim runtime log.
|
||||||
|
@ -22,7 +22,7 @@ description: "logger API provides some basic functions for log message when crea
|
|||||||
| name | description |
|
| name | description |
|
||||||
| --------------------- | --------------------------------- |
|
| --------------------- | --------------------------------- |
|
||||||
| `set_name(string)` | set the name of current logger |
|
| `set_name(string)` | set the name of current logger |
|
||||||
| `set_silent(0 or 1)` | enable/disable silent mode |
|
| `set_silent(silent)` | enable/disable silent mode, `silent` should be boolean or 0/1 |
|
||||||
| `set_verbose(number)` | set the verbose level |
|
| `set_verbose(number)` | set the verbose level |
|
||||||
| `set_level(number)` | set the logger level |
|
| `set_level(number)` | set the logger level |
|
||||||
| `error(string)` | log error message |
|
| `error(string)` | log error message |
|
||||||
|
@ -8,29 +8,43 @@
|
|||||||
|
|
||||||
local fn = vim.fn or require('spacevim').fn
|
local fn = vim.fn or require('spacevim').fn
|
||||||
|
|
||||||
local cmd = require('spacevim').cmd
|
local nt = require('spacevim.api.notify')
|
||||||
|
|
||||||
|
local rtplog = {}
|
||||||
|
|
||||||
local M = {
|
local M = {
|
||||||
['name'] = '',
|
name = '',
|
||||||
['silent'] = 1,
|
silent = 1,
|
||||||
['level'] = 1,
|
level = 1,
|
||||||
['verbose'] = 1,
|
verbose = 1,
|
||||||
['file'] = '',
|
file = '',
|
||||||
['temp'] = {},
|
temp = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
-- 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' }
|
M.levels = { 'Info ', 'Warn ', 'Error', 'Debug' }
|
||||||
M.clock = fn.reltime()
|
M.clock = fn.reltime()
|
||||||
|
|
||||||
function M.set_silent(sl)
|
function M.set_silent(sl)
|
||||||
M.silent = sl
|
if type(sl) == 'boolean' then
|
||||||
|
M.silent = sl
|
||||||
|
elseif type(sl) == 'number' then
|
||||||
|
-- this is for backward compatibility.
|
||||||
|
if sl == 1 then
|
||||||
|
M.silent = true
|
||||||
|
elseif sl == 0 then
|
||||||
|
M.silent = false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.set_verbose(vb)
|
function M.set_verbose(vb)
|
||||||
|
-- verbose should be 1 - 4
|
||||||
|
-- message type: log debug, info, warn, error
|
||||||
|
-- info and debug should not be print to screen.
|
||||||
|
-- the default verbose is 1
|
||||||
|
-- 1: notify nothing
|
||||||
|
-- 2: notify only error
|
||||||
|
-- 3: notify warn and error
|
||||||
M.verbose = vb
|
M.verbose = vb
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -47,30 +61,27 @@ end
|
|||||||
|
|
||||||
function M._build_msg(msg, l)
|
function M._build_msg(msg, l)
|
||||||
msg = msg or ''
|
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
|
|
||||||
-- https://github.com/neovim/neovim/issues/4433
|
|
||||||
-- string.format("%s:%03d", os.date("%H:%M:%S"), vim.loop.now() % 1000)
|
|
||||||
-- 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 _, mic = vim.loop.gettimeofday()
|
local _, mic = vim.loop.gettimeofday()
|
||||||
local c = string.format("%s:%03d", os.date("%H:%M:%S"), mic / 1000)
|
local c = string.format('%s:%03d', os.date('%H:%M:%S'), mic / 1000)
|
||||||
local log = string.format('[ %s ] [%s] [ %s ] %s', M.name, c, M.levels[l], msg)
|
-- local log = string.format('[ %s ] [%s] [ %s ] %s', M.name, c, M.levels[l], msg)
|
||||||
return log
|
|
||||||
|
return {
|
||||||
|
name = M.name,
|
||||||
|
time = c,
|
||||||
|
msg = msg,
|
||||||
|
level = l,
|
||||||
|
str = string.format('[ %s ] [%s] [ %s ] %s', M.name, c, M.levels[l], msg),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.write(log)
|
||||||
|
table.insert(M.temp, log)
|
||||||
|
table.insert(rtplog, log.str)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.debug(msg)
|
function M.debug(msg)
|
||||||
if M.level <= 0 then
|
if M.level <= 0 then
|
||||||
local log = M._build_msg(msg, 4)
|
local log = M._build_msg(msg, 4)
|
||||||
if M.silent == 0 and M.verbose >= 4 then
|
|
||||||
cmd('echom "' .. log .. '"')
|
|
||||||
end
|
|
||||||
M.write(log)
|
M.write(log)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -78,34 +89,30 @@ end
|
|||||||
function M.error(msg)
|
function M.error(msg)
|
||||||
local log = M._build_msg(msg, 3)
|
local log = M._build_msg(msg, 3)
|
||||||
if M.silent == 0 and M.verbose >= 1 then
|
if M.silent == 0 and M.verbose >= 1 then
|
||||||
cmd('echohl Error')
|
nt.notify(msg, 'Error')
|
||||||
cmd('echom "' .. log .. '"')
|
|
||||||
cmd('echohl None')
|
|
||||||
end
|
end
|
||||||
M.write(log)
|
M.write(log)
|
||||||
end
|
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, ...)
|
function M.warn(msg, ...)
|
||||||
if M.level <= 2 then
|
if M.level <= 2 then
|
||||||
local log = M._build_msg(msg, 2)
|
local log = M._build_msg(msg, 2)
|
||||||
if (M.silent == 0 and M.verbose >= 2) or select(1, ...) == 0 then
|
local issilent = select(1, ...)
|
||||||
cmd('echohl WarningMsg')
|
if type(issilent) == 'number' then
|
||||||
cmd('echom "' .. log .. '"')
|
if issilent == 1 then
|
||||||
cmd('echohl None')
|
issilent = true
|
||||||
|
else
|
||||||
|
issilent = false
|
||||||
|
end
|
||||||
|
elseif type(issilent) ~= 'boolean' then
|
||||||
|
issilent = false
|
||||||
|
end
|
||||||
|
if not issilent then
|
||||||
|
nt.notify(msg, 'WarningMsg')
|
||||||
|
else
|
||||||
|
if not M.silent and M.verbose >= 2 then
|
||||||
|
nt.notify(msg, 'WarningMsg')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
M.write(log)
|
M.write(log)
|
||||||
end
|
end
|
||||||
@ -114,65 +121,33 @@ end
|
|||||||
function M.info(msg)
|
function M.info(msg)
|
||||||
if M.level <= 1 then
|
if M.level <= 1 then
|
||||||
local log = M._build_msg(msg, 1)
|
local log = M._build_msg(msg, 1)
|
||||||
if M.silent == 0 and M.verbose >= 3 then
|
|
||||||
cmd('echom "' .. log .. '"')
|
|
||||||
end
|
|
||||||
M.write(log)
|
M.write(log)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.view_all()
|
||||||
|
local info = table.concat(rtplog, '\n')
|
||||||
|
return info
|
||||||
|
end
|
||||||
|
|
||||||
function M.view(l)
|
function M.view(l)
|
||||||
local info = ''
|
local info = ''
|
||||||
local logs = ''
|
for _, log in ipairs(M.temp) do
|
||||||
if fn.filereadable(M.file) == 1 then
|
if log.level >= l then
|
||||||
logs = fn.readfile(M.file, '')
|
info = info .. log.str .. '\n'
|
||||||
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
|
||||||
end
|
end
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.set_name(name)
|
function M.set_name(name)
|
||||||
M.name = name
|
if type(name) == 'string' then
|
||||||
|
M.name = name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_name()
|
function M.get_name()
|
||||||
return M.name
|
return M.name
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.set_file(file)
|
|
||||||
M.file = file
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -49,7 +49,8 @@ function M.setOutput(file)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.viewRuntimeLog()
|
function M.viewRuntimeLog()
|
||||||
local info = '### SpaceVim runtime log :\n\n' .. logger.view(logger.level)
|
-- this function should be more faster, and view runtime log without filter
|
||||||
|
local info = '### SpaceVim runtime log :\n\n' .. logger.view_all()
|
||||||
cmd('tabnew')
|
cmd('tabnew')
|
||||||
cmd('setl nobuflisted')
|
cmd('setl nobuflisted')
|
||||||
cmd('nnoremap <buffer><silent> q :tabclose!<CR>')
|
cmd('nnoremap <buffer><silent> q :tabclose!<CR>')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user