From 217c6848a6656c5e187d66a2b3caed9a9676d448 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sat, 18 May 2024 11:19:45 +0200 Subject: [PATCH] feat(util): better debug log --- lua/noice/util/init.lua | 13 ++++++++++--- lua/noice/view/init.lua | 10 +++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lua/noice/util/init.lua b/lua/noice/util/init.lua index b1cf8ba8..72069eb4 100644 --- a/lua/noice/util/init.lua +++ b/lua/noice/util/init.lua @@ -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 diff --git a/lua/noice/view/init.lua b/lua/noice/view/init.lua index 3b5504c4..0bbccc26 100644 --- a/lua/noice/view/init.lua +++ b/lua/noice/view/init.lua @@ -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 @@ -148,7 +155,8 @@ function View:display() self:show() self._errors = 0 end, { - catch = function() + catch = function(err) + self:debug(err) self:destroy() end, })()