mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-22 17:05:42 +08:00
Add view log func
This commit is contained in:
parent
7ca8174d4d
commit
cfcb8e5d2e
@ -3,48 +3,55 @@ let s:self = {
|
|||||||
\ 'name' : '',
|
\ 'name' : '',
|
||||||
\ 'silent' : 1,
|
\ 'silent' : 1,
|
||||||
\ 'file' : '',
|
\ 'file' : '',
|
||||||
|
\ 'temp' : [],
|
||||||
\ }
|
\ }
|
||||||
|
|
||||||
|
let s:levels = ['Info', 'Warn', 'Error']
|
||||||
|
|
||||||
function! SpaceVim#api#logger#get() abort
|
function! SpaceVim#api#logger#get() abort
|
||||||
return deepcopy(s:self)
|
return deepcopy(s:self)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! s:self.error(msg) abort
|
function! s:self.error(msg) abort
|
||||||
let time = strftime('%H:%M:%S')
|
let time = strftime('%H:%M:%S')
|
||||||
let log = '[ ' . self.name . ' ] [' . time . '] [ Error ] ' . a:msg
|
let log = '[ ' . self.name . ' ] [' . time . '] [ Error ] ' . a:msg
|
||||||
if !self.silent
|
if !self.silent
|
||||||
echoerr log
|
echoerr log
|
||||||
endif
|
endif
|
||||||
call self.write(log)
|
call self.write(log)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:self.write(msg) abort
|
function! s:self.write(msg) abort
|
||||||
if !isdirectory(fnamemodify(self.file, ':p:h'))
|
if empty(self.file)
|
||||||
call mkdir(expand(fnamemodify(self.file, ':p:h')), 'p')
|
call add(self.temp, a:msg)
|
||||||
endif
|
return
|
||||||
let flags = filewritable(self.file) ? 'a' : ''
|
endif
|
||||||
call writefile([a:msg], self.file, flags)
|
if !isdirectory(fnamemodify(self.file, ':p:h'))
|
||||||
|
call mkdir(expand(fnamemodify(self.file, ':p:h')), 'p')
|
||||||
|
endif
|
||||||
|
let flags = filewritable(self.file) ? 'a' : ''
|
||||||
|
call writefile([a:msg], self.file, flags)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:self.warn(msg) abort
|
function! s:self.warn(msg) abort
|
||||||
let time = strftime('%H:%M:%S')
|
let time = strftime('%H:%M:%S')
|
||||||
let log = '[ ' . self.name . ' ] [' . time . '] [ Warn ] ' . a:msg
|
let log = '[ ' . self.name . ' ] [' . time . '] [ Warn ] ' . a:msg
|
||||||
if !self.silent
|
if !self.silent
|
||||||
echohl WarningMsg
|
echohl WarningMsg
|
||||||
echom log
|
echom log
|
||||||
echohl None
|
echohl None
|
||||||
endif
|
endif
|
||||||
call self.write(log)
|
call self.write(log)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:self.info(msg) abort
|
function! s:self.info(msg) abort
|
||||||
let time = strftime('%H:%M:%S')
|
let time = strftime('%H:%M:%S')
|
||||||
let log = '[ ' . self.name . ' ] [' . time . '] [ Info ] ' . a:msg
|
let log = '[ ' . self.name . ' ] [' . time . '] [ Info ] ' . a:msg
|
||||||
if !self.silent
|
if !self.silent
|
||||||
echom log
|
echom log
|
||||||
endif
|
endif
|
||||||
call self.write(log)
|
call self.write(log)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:self.set_name(name) abort
|
function! s:self.set_name(name) abort
|
||||||
@ -58,3 +65,20 @@ endfunction
|
|||||||
function! s:self.set_file(file) abort
|
function! s:self.set_file(file) abort
|
||||||
let self.file = a:file
|
let self.file = a:file
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.view(l) abort
|
||||||
|
let info = ''
|
||||||
|
if filereadable(self.file)
|
||||||
|
let logs = readfile(self.file, '')
|
||||||
|
let info .= join(filter(logs,
|
||||||
|
\ "v:val =~# '\[ " . self.name . ' \] \[\d\d\:\d\d\:\d\d\] \['
|
||||||
|
\ . s:levels[a:l] . "\]'"), "\n")
|
||||||
|
else
|
||||||
|
let info .= '[ ' . self.name . ' ] : logger file ' . self.file
|
||||||
|
\ . ' does not exists, only log for current process will be shown!'
|
||||||
|
let info .= join(filter(self.temp,
|
||||||
|
\ "v:val =~# '\[ SpaceVim \] \[\d\d\:\d\d\:\d\d\] \["
|
||||||
|
\ . s:levels[a:l] . "\]'"), "\n")
|
||||||
|
endif
|
||||||
|
return info
|
||||||
|
endfunction
|
||||||
|
Loading…
x
Reference in New Issue
Block a user