robin/lylla.nvim

a tiny, customizable statusline for neovim

git clone https://git.robinwobin.dev/robin/lylla.nvim

robinfix: diagnosticsa25438a

main
1.7 KiB70 linesraw
1---@module 'lylla.log'
2
3local log = {}
4
5log.stack = {}
6
7local function getdebuginfo()
8  local i = 3
9  local info = debug.getinfo(i, "nSf")
10  local nextinfo = debug.getinfo(i + 1, "n")
11  while nextinfo and info and info.name == nil do
12    info = nextinfo
13    i = i + 1
14    nextinfo = debug.getinfo(i + 1, "n")
15  end
16  return info
17end
18
19--- any message will not be displayed unless `'debug'` is set to `msg`.
20--- this is a purposeful design decisison made to avoid flooding the user with errors.
21--- this design decision also means that modules can silently fail.
22---@param msg string
23---@param level integer
24function log.notify(msg, level, debuginfo)
25  debuginfo = debuginfo or getdebuginfo()
26  local info = string.format(
27    "%s %s at %s",
28    #debuginfo.namewhat > 0 and debuginfo.namewhat or "chunk",
29    debuginfo.name or "main",
30    debuginfo.short_src or "main loop"
31  )
32  log.stack[#log.stack + 1] = { level, info, msg }
33  msg = string.format("in %s:\n\t%s", info, msg)
34  if vim.o.debug == "" then
35    return
36  end
37  if vim.o.debug == "throw" and level >= vim.log.levels.ERROR then
38    error(msg, level)
39    return
40  end
41  vim.notify_once(msg, level)
42  return level
43end
44
45---@param msg string
46function log.trace(msg)
47  return log.notify(msg, vim.log.levels.TRACE, getdebuginfo())
48end
49
50---@param msg string
51function log.debug(msg)
52  return log.notify(msg, vim.log.levels.DEBUG, getdebuginfo())
53end
54
55---@param msg string
56function log.info(msg)
57  return log.notify(msg, vim.log.levels.INFO, getdebuginfo())
58end
59
60---@param msg string
61function log.warn(msg)
62  return log.notify(msg, vim.log.levels.WARN, getdebuginfo())
63end
64
65---@param msg string
66function log.error(msg)
67  return log.notify(msg, vim.log.levels.ERROR, getdebuginfo())
68end
69
70return log