irongit

A Neovim plugin enabling you to solve LeetCode problems.

79 lines1.5 KBLua
1local config = require("leetcode.config")
2local t = require("leetcode.translator")
3local lvls = vim.log.levels
4
5---@class lc.Logger
6local logger = {}
7
8local function normalize(msg)
9 return type(msg) == "string" and t(msg) or vim.inspect(msg)
10end
11
12-- ---@private
13-- ---@param msg any
14-- ---@param lvl? integer
15-- ---@return any
16-- logger.log = vim.schedule_wrap(function(msg, lvl)
17-- end)
18
19function logger.log(msg, lvl)
20 if not config.user.logging then
21 return
22 end
23
24 local title = config.name
25 lvl = lvl or lvls.INFO
26 msg = normalize(msg)
27
28 if lvl == lvls.DEBUG then
29 msg = debug.traceback(msg .. "\n")
30 end
31
32 vim.schedule(function()
33 vim.notify(msg, lvl, { title = title })
34 end)
35end
36
37---@param msg any
38logger.info = function(msg)
39 logger.log(msg)
40end
41
42---@param msg any
43logger.warn = function(msg)
44 logger.log(msg, lvls.WARN)
45end
46
47---@param msg any
48logger.error = function(msg)
49 logger.log(msg, lvls.ERROR)
50 logger.debug(msg)
51end
52
53---@param err lc.err
54logger.err = function(err)
55 if not err then
56 return logger.error("error")
57 end
58
59 local msg = err.msg or ""
60 local lvl = err.lvl or lvls.ERROR
61
62 logger.log(msg, lvl)
63end
64
65---@param msg any
66---@param show? boolean
67---@return any
68logger.debug = function(msg, show)
69 if not config.debug then
70 return msg
71 end
72
73 local lvl = (show == nil or not show) and lvls.DEBUG or lvls.ERROR
74 logger.log(msg, lvl)
75
76 return msg
77end
78
79return logger