diff --git a/docs/api/logger.md b/docs/api/logger.md index aecdb1dcb..f37392e4f 100644 --- a/docs/api/logger.md +++ b/docs/api/logger.md @@ -8,7 +8,8 @@ description: "logger API provides some basic functions for log message when crea - [Intro](#intro) -- [functions](#functions) +- [Functions](#functions) +- [Usage](#usage) @@ -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. -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`. @@ -56,8 +69,18 @@ function! foo#log#setOutput(file) abort endfunction ``` -## functions +**lua script:** -| name | description | -| --------------- | ------------------------------ | -| `set_name(str)` | set the name of current logger | +```lua +local logger = require('spacevim.api').import('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 + +``` diff --git a/lua/spacevim.lua b/lua/spacevim.lua index 070cda58b..2990abb4f 100644 --- a/lua/spacevim.lua +++ b/lua/spacevim.lua @@ -21,6 +21,16 @@ function M.eval(l) 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 local function build_argv(...) diff --git a/lua/spacevim/api/logger.lua b/lua/spacevim/api/logger.lua new file mode 100644 index 000000000..44fc4e51b --- /dev/null +++ b/lua/spacevim/api/logger.lua @@ -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 diff --git a/test/lua/logger.vader b/test/lua/logger.vader new file mode 100644 index 000000000..36bff2787 --- /dev/null +++ b/test/lua/logger.vader @@ -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