Skip to content

Commit

Permalink
feat(util): better debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 18, 2024
1 parent 6a3721b commit 217c684
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lua/noice/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,22 @@ function M.info(msg, ...)
M.notify(msg, vim.log.levels.INFO, ...)
end

---@param data any
function M.debug(data)
if not Config.options.debug then
return
end
if type(data) == "function" then
data = data()
end
if type(data) ~= "string" then
data = vim.inspect(data)
end
local file = "./noice.log"
local fd = io.open(file, "a+")
if not fd then
error(("Could not open file %s for writing"):format(file))
end
if type(data) ~= "string" then
data = vim.inspect(data)
end
fd:write(data .. "\n")
fd:close()
end
Expand Down
10 changes: 9 additions & 1 deletion lua/noice/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ function View:set(messages, opts)
self:push(messages, opts)
end

function View:debug(msg)
if Config.options.debug then
Util.debug(("[%s] %s"):format(self._opts.view, vim.inspect(msg)))
Util.debug(debug.traceback())
end
end

-- Safely destroys any create windows and buffers.
-- This is needed to properly re-create views in case of E565 errors
function View:destroy() end
Expand All @@ -148,7 +155,8 @@ function View:display()
self:show()
self._errors = 0
end, {
catch = function()
catch = function(err)
self:debug(err)
self:destroy()
end,
})()
Expand Down

0 comments on commit 217c684

Please sign in to comment.