1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-10 12:25:49 +08:00
SpaceVim/autoload/SpaceVim/logger.vim

116 lines
2.9 KiB
VimL
Raw Normal View History

2017-01-08 23:02:49 +08:00
let s:logger_level = g:spacevim_debug_level
2017-01-08 16:27:18 +08:00
let s:levels = ['Info', 'Warn', 'Error']
let s:logger_file = expand('~/.SpaceVim/.SpaceVim.log')
2017-01-24 00:23:59 +08:00
let s:log_temp = []
2017-01-08 18:38:45 +08:00
2017-01-08 16:27:18 +08:00
""
" @public
" Set debug level of SpaceVim. Default is 1.
2017-01-08 16:27:18 +08:00
"
" 1 : log all messages
2017-01-08 16:27:18 +08:00
"
" 2 : log warning and error messages
2017-01-08 16:27:18 +08:00
"
" 3 : log error messages only
2017-01-08 16:27:18 +08:00
function! SpaceVim#logger#setLevel(level) abort
2017-03-06 23:26:26 +08:00
let s:logger_level = a:level
2017-01-08 16:27:18 +08:00
endfunction
2017-01-08 18:38:45 +08:00
function! SpaceVim#logger#info(msg) abort
2017-03-06 23:26:26 +08:00
if g:spacevim_enable_debug && s:logger_level <= 1
call s:wite(s:warpMsg(a:msg, 1))
else
call add(s:log_temp,s:warpMsg(a:msg,1))
endif
2017-01-08 18:38:45 +08:00
endfunction
function! SpaceVim#logger#warn(msg) abort
2017-03-06 23:26:26 +08:00
if g:spacevim_enable_debug && s:logger_level <= 2
call s:wite(s:warpMsg(a:msg, 2))
else
call add(s:log_temp,s:warpMsg(a:msg,2))
endif
2017-01-08 18:38:45 +08:00
endfunction
function! SpaceVim#logger#error(msg) abort
2017-03-06 23:26:26 +08:00
if g:spacevim_enable_debug && s:logger_level <= 3
call s:wite(s:warpMsg(a:msg, 3))
else
call add(s:log_temp,s:warpMsg(a:msg,3))
endif
2017-01-08 18:38:45 +08:00
endfunction
2017-01-08 16:27:18 +08:00
function! s:wite(msg) abort
2017-03-06 23:26:26 +08:00
let flags = filewritable(s:logger_file) ? 'a' : ''
call writefile([a:msg], s:logger_file, flags)
2017-01-08 16:27:18 +08:00
endfunction
function! SpaceVim#logger#viewLog(...) abort
2017-03-06 23:26:26 +08:00
let info = "### SpaceVim Options :\n\n"
let info .= "```viml\n"
let info .= join(SpaceVim#options#list(), "\n")
let info .= "\n```\n"
let info .= "\n\n"
2017-02-02 14:48:36 +08:00
2017-03-06 23:26:26 +08:00
let info .= "### SpaceVim Health checking :\n\n"
let info .= SpaceVim#health#report()
let info .= "\n\n"
2017-02-02 14:48:36 +08:00
2017-03-06 23:26:26 +08:00
let info .= "### SpaceVim runtime log :\n\n"
let info .= "```log\n"
2017-01-24 00:16:35 +08:00
2017-03-06 23:26:26 +08:00
let l = a:0 > 0 ? a:1 : 1
if filereadable(s:logger_file)
let logs = readfile(s:logger_file, '')
2017-03-07 22:11:41 +08:00
let info .= join(filter(logs,
\ "v:val =~# '\[ SpaceVim \] \[\d\d\:\d\d\:\d\d\] \["
\ . s:levels[l] . "\]'"), "\n")
2017-03-06 23:26:26 +08:00
else
2017-03-07 22:11:41 +08:00
let info .= '[ SpaceVim ] : logger file ' . s:logger_file
\ . ' does not exists, only log for current process will be shown!'
let info .= join(filter(s:log_temp,
\ "v:val =~# '\[ SpaceVim \] \[\d\d\:\d\d\:\d\d\] \["
\ . s:levels[l] . "\]'"), "\n")
2017-03-06 23:26:26 +08:00
endif
let info .= "\n```\n"
2017-03-07 23:31:51 +08:00
if a:0 > 0
if a:1 == 1
tabnew +setl\ nobuflisted
nnoremap <buffer><silent> q :bd!<CR>
for msg in split(info, "\n")
call append(line('$'), msg)
endfor
normal! "_dd
setl nomodifiable
setl buftype=nofile
2017-03-08 20:39:25 +08:00
setl filetype=markdown
2017-03-07 23:31:51 +08:00
else
2017-03-19 15:36:51 +08:00
echo info
2017-03-07 23:31:51 +08:00
endif
else
return info
endif
2017-01-08 16:27:18 +08:00
endfunction
""
" @public
" Set the log output file of SpaceVim. Default is `~/.SpaceVim/.SpaceVim.log`.
2017-01-08 16:27:18 +08:00
function! SpaceVim#logger#setOutput(file) abort
2017-03-06 23:26:26 +08:00
let s:logger_file = a:file
2017-01-08 16:27:18 +08:00
endfunction
function! s:warpMsg(msg,l) abort
2017-03-06 23:26:26 +08:00
let time = strftime('%H:%M:%S')
let log = '[ SpaceVim ] [' . time . '] [' . s:levels[a:l - 1] . '] ' . a:msg
return log
2017-01-08 16:27:18 +08:00
endfunction
function! SpaceVim#logger#echoWarn(msg) abort
2017-03-06 23:26:26 +08:00
echohl WarningMsg
echom s:warpMsg(a:msg, 1)
echohl None
2017-01-08 16:27:18 +08:00
endfunction
2017-03-06 23:26:26 +08:00
2017-03-07 22:11:41 +08:00
" vim:set et sw=2 cc=80: