mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-03 04:50:04 +08:00
Add lua logger api (#4395)
This commit is contained in:
parent
5fe3d09bb2
commit
384964526b
@ -8,7 +8,8 @@ description: "logger API provides some basic functions for log message when crea
|
|||||||
<!-- vim-markdown-toc GFM -->
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
- [Intro](#intro)
|
- [Intro](#intro)
|
||||||
- [functions](#functions)
|
- [Functions](#functions)
|
||||||
|
- [Usage](#usage)
|
||||||
|
|
||||||
<!-- vim-markdown-toc -->
|
<!-- vim-markdown-toc -->
|
||||||
|
|
||||||
@ -16,7 +17,19 @@ description: "logger API provides some basic functions for log message when crea
|
|||||||
|
|
||||||
`logger` API provides some functions to create logger for plugin.
|
`logger` API provides some functions to create logger for plugin.
|
||||||
|
|
||||||
In vim script, loading the API before using it. You can also config the options of the API.
|
|
||||||
|
## Functions
|
||||||
|
|
||||||
|
| name | description |
|
||||||
|
| --------------- | ------------------------------ |
|
||||||
|
| `set_name(str)` | set the name of current logger |
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
The `logger` api provides two versions, the vim script and lua:
|
||||||
|
|
||||||
|
**vim script:**
|
||||||
|
|
||||||
here is an example for creatting logger for plugin `foo.vim`.
|
here is an example for creatting logger for plugin `foo.vim`.
|
||||||
|
|
||||||
@ -56,8 +69,18 @@ function! foo#log#setOutput(file) abort
|
|||||||
endfunction
|
endfunction
|
||||||
```
|
```
|
||||||
|
|
||||||
## functions
|
**lua script:**
|
||||||
|
|
||||||
| name | description |
|
```lua
|
||||||
| --------------- | ------------------------------ |
|
local logger = require('spacevim.api').import('logger')
|
||||||
| `set_name(str)` | set the name of current logger |
|
|
||||||
|
logger.set_name('foo')
|
||||||
|
logger.set_level(1)
|
||||||
|
logger.set_silent(1)
|
||||||
|
logger.set_verbose(1)
|
||||||
|
|
||||||
|
local function warn(msg)
|
||||||
|
logger.warn(msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
```
|
||||||
|
@ -21,6 +21,16 @@ function M.eval(l)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if vim.command ~= nil then
|
||||||
|
function M.cmd(command)
|
||||||
|
return vim.command(command)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
function M.cmd(command)
|
||||||
|
return vim.api.nvim_command(command)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- there is no want to call viml function in old vim and neovim
|
-- there is no want to call viml function in old vim and neovim
|
||||||
|
|
||||||
local function build_argv(...)
|
local function build_argv(...)
|
||||||
|
98
lua/spacevim/api/logger.lua
Normal file
98
lua/spacevim/api/logger.lua
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
local fn = nil
|
||||||
|
if vim.fn == nil then
|
||||||
|
fn = require('spacevim').fn
|
||||||
|
else
|
||||||
|
fn = vim.fn
|
||||||
|
end
|
||||||
|
|
||||||
|
local cmd = require('spacevim').cmd
|
||||||
|
|
||||||
|
local M = {
|
||||||
|
['name'] = '',
|
||||||
|
['silent'] = 1,
|
||||||
|
['level'] = 1,
|
||||||
|
['verbose'] = 1,
|
||||||
|
['file'] = '',
|
||||||
|
['temp'] = {},
|
||||||
|
}
|
||||||
|
|
||||||
|
local levels = {'Info', 'Warn', 'Error'}
|
||||||
|
|
||||||
|
function M.set_silent(sl)
|
||||||
|
M.silent = sl
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.set_verbose(vb)
|
||||||
|
M.verbose = vb
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.set_level(l)
|
||||||
|
M.level = l
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.error(msg)
|
||||||
|
local time = fn.strftime('%H:%M:%S')
|
||||||
|
local log = '[ ' .. M.name .. ' ] [' .. time .. '] [ ' .. levels[1] .. ' ] ' .. msg
|
||||||
|
if M.silent == 0 and M.verbose >= 1 then
|
||||||
|
cmd('echohl Error')
|
||||||
|
cmd('echom "' .. log .. '"')
|
||||||
|
cmd('echohl None')
|
||||||
|
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 time = fn.strftime('%H:%M:%S')
|
||||||
|
local log = '[ ' .. M.name .. ' ] [' .. time .. '] [ ' .. levels[1] .. ' ] ' .. msg
|
||||||
|
if (M.silent == 0 and M.verbose >= 2) or select(1, ...) == 1 then
|
||||||
|
cmd('echohl WarningMsg')
|
||||||
|
cmd('echom "' .. log .. '"')
|
||||||
|
cmd('echohl None')
|
||||||
|
end
|
||||||
|
M.write(log)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.info(msg)
|
||||||
|
if M.level <= 1 then
|
||||||
|
local time = fn.strftime('%H:%M:%S')
|
||||||
|
local log = '[ ' .. M.name .. ' ] [' .. time .. '] [ ' .. levels[1] .. ' ] ' .. msg
|
||||||
|
if M.silent == 0 and M.verbose >= 3 then
|
||||||
|
cmd('echom "' .. log .. '"')
|
||||||
|
end
|
||||||
|
M.write(log)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.set_name(name)
|
||||||
|
M.name = name
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.get_name()
|
||||||
|
return M.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.set_file(file)
|
||||||
|
M.file = file
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return M
|
19
test/lua/logger.vader
Normal file
19
test/lua/logger.vader
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
Execute ( SpaceVim lua api: logger ):
|
||||||
|
let cmp = SpaceVim#api#import('vim#compatible')
|
||||||
|
lua spacevim_logger = require('spacevim.api').import('logger')
|
||||||
|
lua spacevim_logger.set_name('TestLog')
|
||||||
|
AssertEqual cmp.luaeval('spacevim_logger.name'), 'TestLog'
|
||||||
|
lua spacevim_logger.info('info test')
|
||||||
|
lua spacevim_logger.warn('info test')
|
||||||
|
lua spacevim_logger.error('info test')
|
||||||
|
AssertEqual cmp.luaeval('#spacevim_logger.temp'), 3
|
||||||
|
lua spacevim_logger.set_level(2)
|
||||||
|
lua spacevim_logger.info('info test')
|
||||||
|
lua spacevim_logger.warn('info test')
|
||||||
|
lua spacevim_logger.error('info test')
|
||||||
|
AssertEqual cmp.luaeval('#spacevim_logger.temp'), 5
|
||||||
|
lua spacevim_logger.set_level(3)
|
||||||
|
lua spacevim_logger.info('info test')
|
||||||
|
lua spacevim_logger.warn('info test')
|
||||||
|
lua spacevim_logger.error('info test')
|
||||||
|
AssertEqual cmp.luaeval('#spacevim_logger.temp'), 6
|
Loading…
Reference in New Issue
Block a user