2022-01-04 23:15:17 +08:00
|
|
|
--=============================================================================
|
|
|
|
-- logger.lua --- logger api implemented in lua
|
2023-03-26 13:50:22 +08:00
|
|
|
-- Copyright (c) 2016-2023 Wang Shidong & Contributors
|
2022-01-04 23:15:17 +08:00
|
|
|
-- Author: Wang Shidong < wsdjeg@outlook.com >
|
|
|
|
-- URL: https://spacevim.org
|
|
|
|
-- License: GPLv3
|
|
|
|
--=============================================================================
|
|
|
|
|
|
|
|
local fn = vim.fn or require('spacevim').fn
|
2021-08-14 15:27:12 +08:00
|
|
|
|
2023-09-16 15:21:40 +08:00
|
|
|
local nt = require('spacevim.api.notify')
|
|
|
|
|
|
|
|
local rtplog = {}
|
2021-08-14 15:27:12 +08:00
|
|
|
|
|
|
|
local M = {
|
2023-09-16 15:21:40 +08:00
|
|
|
name = '',
|
|
|
|
silent = 1,
|
|
|
|
level = 1,
|
|
|
|
verbose = 1,
|
|
|
|
file = '',
|
|
|
|
temp = {},
|
2021-08-14 15:27:12 +08:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:46:38 +08:00
|
|
|
M.levels = { 'Info ', 'Warn ', 'Error', 'Debug' }
|
2021-11-13 21:22:47 +08:00
|
|
|
M.clock = fn.reltime()
|
2021-08-14 15:27:12 +08:00
|
|
|
|
|
|
|
function M.set_silent(sl)
|
2023-09-16 15:21:40 +08:00
|
|
|
if type(sl) == 'boolean' then
|
|
|
|
M.silent = sl
|
|
|
|
elseif type(sl) == 'number' then
|
|
|
|
-- this is for backward compatibility.
|
|
|
|
if sl == 1 then
|
|
|
|
M.silent = true
|
|
|
|
elseif sl == 0 then
|
|
|
|
M.silent = false
|
|
|
|
end
|
|
|
|
end
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.set_verbose(vb)
|
2023-09-16 15:21:40 +08:00
|
|
|
-- verbose should be 1 - 4
|
|
|
|
-- message type: log debug, info, warn, error
|
|
|
|
-- info and debug should not be print to screen.
|
|
|
|
-- the default verbose is 1
|
|
|
|
-- 1: notify nothing
|
|
|
|
-- 2: notify only error
|
|
|
|
-- 3: notify warn and error
|
2023-04-03 12:46:38 +08:00
|
|
|
M.verbose = vb
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.set_level(l)
|
2023-04-03 12:46:38 +08:00
|
|
|
-- the level only can be:
|
|
|
|
-- 0 : log debug, info, warn, error messages
|
|
|
|
-- 1 : log info, warn, error messages
|
|
|
|
-- 2 : log warn, error messages
|
|
|
|
-- 3 : log error messages
|
|
|
|
if l == 0 or l == 1 or l == 2 or l == 3 then
|
2021-08-14 15:27:12 +08:00
|
|
|
M.level = l
|
2023-04-03 12:46:38 +08:00
|
|
|
end
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
2021-08-15 19:06:48 +08:00
|
|
|
function M._build_msg(msg, l)
|
2023-04-03 12:46:38 +08:00
|
|
|
msg = msg or ''
|
2023-06-19 20:12:21 +08:00
|
|
|
local _, mic = vim.loop.gettimeofday()
|
2023-09-16 15:21:40 +08:00
|
|
|
local c = string.format('%s:%03d', os.date('%H:%M:%S'), mic / 1000)
|
|
|
|
-- local log = string.format('[ %s ] [%s] [ %s ] %s', M.name, c, M.levels[l], msg)
|
|
|
|
|
|
|
|
return {
|
|
|
|
name = M.name,
|
|
|
|
time = c,
|
|
|
|
msg = msg,
|
|
|
|
level = l,
|
|
|
|
str = string.format('[ %s ] [%s] [ %s ] %s', M.name, c, M.levels[l], msg),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.write(log)
|
|
|
|
table.insert(M.temp, log)
|
|
|
|
table.insert(rtplog, log.str)
|
2021-08-15 19:06:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.debug(msg)
|
2023-04-03 12:46:38 +08:00
|
|
|
if M.level <= 0 then
|
|
|
|
local log = M._build_msg(msg, 4)
|
|
|
|
M.write(log)
|
|
|
|
end
|
2021-08-15 19:06:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.error(msg)
|
2023-04-03 12:46:38 +08:00
|
|
|
local log = M._build_msg(msg, 3)
|
|
|
|
if M.silent == 0 and M.verbose >= 1 then
|
2023-09-16 15:21:40 +08:00
|
|
|
nt.notify(msg, 'Error')
|
2023-04-03 12:46:38 +08:00
|
|
|
end
|
|
|
|
M.write(log)
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.warn(msg, ...)
|
2023-04-03 12:46:38 +08:00
|
|
|
if M.level <= 2 then
|
|
|
|
local log = M._build_msg(msg, 2)
|
2023-09-16 15:21:40 +08:00
|
|
|
local issilent = select(1, ...)
|
|
|
|
if type(issilent) == 'number' then
|
|
|
|
if issilent == 1 then
|
|
|
|
issilent = true
|
|
|
|
else
|
|
|
|
issilent = false
|
|
|
|
end
|
|
|
|
elseif type(issilent) ~= 'boolean' then
|
|
|
|
issilent = false
|
|
|
|
end
|
|
|
|
if not issilent then
|
|
|
|
nt.notify(msg, 'WarningMsg')
|
|
|
|
else
|
|
|
|
if not M.silent and M.verbose >= 2 then
|
|
|
|
nt.notify(msg, 'WarningMsg')
|
|
|
|
end
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
2023-04-03 12:46:38 +08:00
|
|
|
M.write(log)
|
|
|
|
end
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.info(msg)
|
2023-04-03 12:46:38 +08:00
|
|
|
if M.level <= 1 then
|
|
|
|
local log = M._build_msg(msg, 1)
|
|
|
|
M.write(log)
|
|
|
|
end
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
2023-09-16 15:21:40 +08:00
|
|
|
function M.view_all()
|
|
|
|
local info = table.concat(rtplog, '\n')
|
2023-04-03 12:46:38 +08:00
|
|
|
return info
|
2021-08-14 20:51:00 +08:00
|
|
|
end
|
|
|
|
|
2023-09-16 15:21:40 +08:00
|
|
|
function M.view(l)
|
|
|
|
local info = ''
|
|
|
|
for _, log in ipairs(M.temp) do
|
|
|
|
if log.level >= l then
|
|
|
|
info = info .. log.str .. '\n'
|
2023-04-03 12:46:38 +08:00
|
|
|
end
|
|
|
|
end
|
2023-09-16 15:21:40 +08:00
|
|
|
return info
|
2021-08-14 20:51:00 +08:00
|
|
|
end
|
|
|
|
|
2021-08-14 15:27:12 +08:00
|
|
|
function M.set_name(name)
|
2023-09-16 15:21:40 +08:00
|
|
|
if type(name) == 'string' then
|
|
|
|
M.name = name
|
|
|
|
end
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.get_name()
|
2023-04-03 12:46:38 +08:00
|
|
|
return M.name
|
2021-08-14 15:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|