1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:40:05 +08:00

feat(log): improve logger api

This commit is contained in:
Eric Wong 2023-09-16 15:21:40 +08:00 committed by GitHub
parent 70b949a35d
commit 2425ef85e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 103 deletions

View File

@ -15,10 +15,11 @@ if has('nvim-0.5.0')
\ )
endfunction
""
" write warning message to spacevim runtime log.
" by default, the warning message will not be printed in cmdline, if
" `silent` is set, and is `0`, the warning message will be printed to
" cmdline.
" write warning {msg} to spacevim runtime log.
" The `msg` must be string. the second argument is optional, It can a
" boolean or `0/1`. By default, the 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.
function! SpaceVim#logger#warn(msg, ...) abort
let issilent = get(a:000, 0, 1)
lua require("spacevim.logger").warn(

View File

@ -1683,9 +1683,10 @@ SpaceVim#logger#info({msg}) *SpaceVim#logger#info()*
write message to SpaceVim runtime log with `info` level.
SpaceVim#logger#warn({msg}) *SpaceVim#logger#warn()*
write warning message to spacevim runtime log. by default, the warning
message will not be printed in cmdline, if `silent` is set, and is `0`, the
warning message will be printed to cmdline.
write warning {msg} to spacevim runtime log. The `msg` must be string. the
second argument is optional, It can a boolean or `0/1`. By default, the
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()*
write error message to spacevim runtime log.

View File

@ -22,7 +22,7 @@ description: "logger API provides some basic functions for log message when crea
| name | description |
| --------------------- | --------------------------------- |
| `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_level(number)` | set the logger level |
| `error(string)` | log error message |

View File

@ -8,29 +8,43 @@
local fn = vim.fn or require('spacevim').fn
local cmd = require('spacevim').cmd
local nt = require('spacevim.api.notify')
local rtplog = {}
local M = {
['name'] = '',
['silent'] = 1,
['level'] = 1,
['verbose'] = 1,
['file'] = '',
['temp'] = {},
name = '',
silent = 1,
level = 1,
verbose = 1,
file = '',
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.clock = fn.reltime()
function M.set_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
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
end
@ -47,30 +61,27 @@ end
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
-- 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 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)
return log
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)
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
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
@ -78,34 +89,30 @@ end
function M.error(msg)
local log = M._build_msg(msg, 3)
if M.silent == 0 and M.verbose >= 1 then
cmd('echohl Error')
cmd('echom "' .. log .. '"')
cmd('echohl None')
nt.notify(msg, 'Error')
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
local log = M._build_msg(msg, 2)
if (M.silent == 0 and M.verbose >= 2) or select(1, ...) == 0 then
cmd('echohl WarningMsg')
cmd('echom "' .. log .. '"')
cmd('echohl None')
local issilent = select(1, ...)
if type(issilent) == 'number' then
if issilent == 1 then
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
M.write(log)
end
@ -114,65 +121,33 @@ end
function M.info(msg)
if M.level <= 1 then
local log = M._build_msg(msg, 1)
if M.silent == 0 and M.verbose >= 3 then
cmd('echom "' .. log .. '"')
end
M.write(log)
end
end
function M.view_all()
local info = table.concat(rtplog, '\n')
return info
end
function M.view(l)
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
for _, log in ipairs(M.temp) do
if log.level >= l then
info = info .. log.str .. '\n'
end
end
return info
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)
if type(name) == 'string' then
M.name = name
end
end
function M.get_name()
return M.name
end
function M.set_file(file)
M.file = file
end
return M

View File

@ -49,7 +49,8 @@ function M.setOutput(file)
end
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('setl nobuflisted')
cmd('nnoremap <buffer><silent> q :tabclose!<CR>')