mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-25 03:02:21 +08:00
Merge branch 'log_api' into dev
This commit is contained in:
commit
53e44e3be2
@ -9,12 +9,17 @@ env:
|
|||||||
- LINT=vimlint
|
- LINT=vimlint
|
||||||
- LINT=vint-errors
|
- LINT=vint-errors
|
||||||
- LINT=vint
|
- LINT=vint
|
||||||
|
- LINT=vader
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- env: LINT=vimlint
|
- env: LINT=vimlint
|
||||||
- env: LINT=vint
|
- env: LINT=vint
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- sudo apt-get -qq update
|
||||||
|
- sudo apt-get install -y vim-gtk
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- |
|
- |
|
||||||
if [ "${LINT#vimlint}" != "$LINT" ]; then
|
if [ "${LINT#vimlint}" != "$LINT" ]; then
|
||||||
@ -22,6 +27,8 @@ install:
|
|||||||
git clone --depth=1 https://github.com/ynkdir/vim-vimlparser /tmp/vimlparser
|
git clone --depth=1 https://github.com/ynkdir/vim-vimlparser /tmp/vimlparser
|
||||||
elif [ "${LINT#vint}" != "$LINT" ]; then
|
elif [ "${LINT#vint}" != "$LINT" ]; then
|
||||||
virtualenv /tmp/vint && source /tmp/vint/bin/activate && pip install vim-vint
|
virtualenv /tmp/vint && source /tmp/vint/bin/activate && pip install vim-vint
|
||||||
|
elif [ "${LINT#vader}" != "$LINT" ]; then
|
||||||
|
git clone https://github.com/junegunn/vader.vim.git /tmp/vader
|
||||||
fi
|
fi
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
@ -33,4 +40,6 @@ script:
|
|||||||
vint .
|
vint .
|
||||||
elif [ "$LINT" = "vint-errors" ]; then
|
elif [ "$LINT" = "vint-errors" ]; then
|
||||||
vint --error .
|
vint --error .
|
||||||
|
elif [ "$LINT" = "vader" ]; then
|
||||||
|
vim -Nu test/test.vim -c 'Vader! test/*'
|
||||||
fi
|
fi
|
||||||
|
114
autoload/SpaceVim/api/logger.vim
Normal file
114
autoload/SpaceVim/api/logger.vim
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
|
||||||
|
let s:self = {
|
||||||
|
\ 'name' : '',
|
||||||
|
\ 'silent' : 1,
|
||||||
|
\ 'level' : 1,
|
||||||
|
\ 'file' : '',
|
||||||
|
\ 'temp' : [],
|
||||||
|
\ }
|
||||||
|
|
||||||
|
"1 : log all messages
|
||||||
|
"2 : log warning and error messages
|
||||||
|
"3 : log error messages only
|
||||||
|
let s:levels = ['Info', 'Warn', 'Error']
|
||||||
|
|
||||||
|
function! SpaceVim#api#logger#get() abort
|
||||||
|
return deepcopy(s:self)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.set_level(l) abort
|
||||||
|
let self.level = a:l
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.error(msg) abort
|
||||||
|
let time = strftime('%H:%M:%S')
|
||||||
|
let log = '[ ' . self.name . ' ] [' . time . '] [ ' . s:levels[2] . ' ] ' . a:msg
|
||||||
|
if !self.silent
|
||||||
|
echoerr log
|
||||||
|
endif
|
||||||
|
call self.write(log)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.write(msg) abort
|
||||||
|
if empty(self.file)
|
||||||
|
call add(self.temp, a:msg)
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
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
|
||||||
|
|
||||||
|
function! s:self.warn(msg) abort
|
||||||
|
if self.level > 2
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
let time = strftime('%H:%M:%S')
|
||||||
|
let log = '[ ' . self.name . ' ] [' . time . '] [ ' . s:levels[1] . ' ] ' . a:msg
|
||||||
|
if !self.silent
|
||||||
|
echohl WarningMsg
|
||||||
|
echom log
|
||||||
|
echohl None
|
||||||
|
endif
|
||||||
|
call self.write(log)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.info(msg) abort
|
||||||
|
if self.level > 1
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
let time = strftime('%H:%M:%S')
|
||||||
|
let log = '[ ' . self.name . ' ] [' . time . '] [ ' . s:levels[0] . ' ] ' . a:msg
|
||||||
|
if !self.silent
|
||||||
|
echom log
|
||||||
|
endif
|
||||||
|
call self.write(log)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.set_name(name) abort
|
||||||
|
let self.name = a:name
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.get_name() abort
|
||||||
|
return self.name
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.set_file(file) abort
|
||||||
|
let self.file = a:file
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.view(l) abort
|
||||||
|
let info = ''
|
||||||
|
if filereadable(self.file)
|
||||||
|
let logs = readfile(self.file, '')
|
||||||
|
let info .= join(filter(logs, 'self._comp(v:val, 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 .= "\n"
|
||||||
|
let info .= join(filter(deepcopy(self.temp), 'self._comp(v:val, a:l)'), "\n")
|
||||||
|
endif
|
||||||
|
return info
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:self._comp(msg, l) abort
|
||||||
|
if a:msg =~# '\[ ' . self.name . ' \] \[\d\d\:\d\d\:\d\d\] \[ '
|
||||||
|
\ . s:levels[2] . ' \]'
|
||||||
|
return 1
|
||||||
|
elseif a:msg =~# '\[ ' . self.name . ' \] \[\d\d\:\d\d\:\d\d\] \[ '
|
||||||
|
\ . s:levels[1] . ' \]'
|
||||||
|
if a:l > 2
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
if a:l > 1
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endfunction
|
22
test/api/logger.vader
Normal file
22
test/api/logger.vader
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Execute ( SpaceVim api: logger ):
|
||||||
|
new
|
||||||
|
let log = SpaceVim#api#import('logger')
|
||||||
|
call log.set_name('TestLog')
|
||||||
|
AssertEqual log.name, 'TestLog'
|
||||||
|
call log.info('info test')
|
||||||
|
call log.warn('info test')
|
||||||
|
call log.error('info test')
|
||||||
|
AssertEqual len(log.temp), 3
|
||||||
|
call log.set_level(2)
|
||||||
|
call log.info('info test')
|
||||||
|
call log.warn('info test')
|
||||||
|
call log.error('info test')
|
||||||
|
AssertEqual len(log.temp), 5
|
||||||
|
call log.set_level(3)
|
||||||
|
call log.info('info test')
|
||||||
|
call log.warn('info test')
|
||||||
|
call log.error('info test')
|
||||||
|
AssertEqual len(log.temp), 6
|
||||||
|
AssertEqual len(split(log.view(1), "\n")), 7
|
||||||
|
AssertEqual len(split(log.view(2), "\n")), 6
|
||||||
|
AssertEqual len(split(log.view(3), "\n")), 4
|
21
test/test.vader
Normal file
21
test/test.vader
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
Execute (Clear search history):
|
||||||
|
for _ in range(&history)
|
||||||
|
call histdel('/', -1)
|
||||||
|
endfor
|
||||||
|
|
||||||
|
Given (Search and destroy):
|
||||||
|
I'm a street walking cheetah with a heart full of napalm
|
||||||
|
I'm a runaway son of the nuclear A-bomb
|
||||||
|
I'm a world's forgotten boy
|
||||||
|
The one who searches and destroys
|
||||||
|
|
||||||
|
Do (Searches):
|
||||||
|
/street\<CR>
|
||||||
|
/walking\<CR>
|
||||||
|
/cheetah\<CR>
|
||||||
|
/runaway\<CR>
|
||||||
|
/search\<CR>
|
||||||
|
|
||||||
|
Execute (Assertions):
|
||||||
|
Log string(map(range(1, &history), 'histget("/", - v:val)'))
|
||||||
|
AssertEqual 'search', histget('/', -1)
|
6
test/test.vim
Normal file
6
test/test.vim
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
filetype off
|
||||||
|
set rtp+=/tmp/vader
|
||||||
|
set rtp+=.
|
||||||
|
set rtp+=after
|
||||||
|
filetype plugin indent on
|
||||||
|
syntax enable
|
Loading…
x
Reference in New Issue
Block a user