mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
feature: Add logger for debugging (#18)
This commit is contained in:
@@ -111,11 +111,10 @@ EOF
|
|||||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||||
debounce_delay = 1000, -- delay after which a pending save is executed
|
debounce_delay = 1000, -- delay after which a pending save is executed
|
||||||
callbacks = { -- functions to be executed at different intervals
|
callbacks = { -- functions to be executed at different intervals
|
||||||
enabling = nil, -- ran when enabling auto-save
|
|
||||||
disabling = nil, -- ran when disabling auto-save
|
|
||||||
before_saving = nil, -- ran before doing the actual save
|
before_saving = nil, -- ran before doing the actual save
|
||||||
after_saving = nil -- ran after doing the actual save
|
},
|
||||||
}
|
-- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable
|
||||||
|
debug = false,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -22,11 +22,10 @@ Config = {
|
|||||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||||
debounce_delay = 1000, -- delay after which a pending save is executed
|
debounce_delay = 1000, -- delay after which a pending save is executed
|
||||||
callbacks = { -- functions to be executed at different intervals
|
callbacks = { -- functions to be executed at different intervals
|
||||||
enabling = nil, -- ran when enabling auto-save
|
|
||||||
disabling = nil, -- ran when disabling auto-save
|
|
||||||
before_saving = nil, -- ran before doing the actual save
|
before_saving = nil, -- ran before doing the actual save
|
||||||
after_saving = nil, -- ran after doing the actual save
|
|
||||||
},
|
},
|
||||||
|
-- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable
|
||||||
|
debug = false, -- print debug messages, set to `true` to enable
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ local cnf = require("auto-save.config")
|
|||||||
local callback = require("auto-save.utils.data").do_callback
|
local callback = require("auto-save.utils.data").do_callback
|
||||||
local colors = require("auto-save.utils.colors")
|
local colors = require("auto-save.utils.colors")
|
||||||
local echo = require("auto-save.utils.echo")
|
local echo = require("auto-save.utils.echo")
|
||||||
|
local logger
|
||||||
local autosave_running
|
local autosave_running
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
local g = vim.g
|
local g = vim.g
|
||||||
@@ -25,6 +26,8 @@ local function cancel_timer(buf)
|
|||||||
if timer ~= nil then
|
if timer ~= nil then
|
||||||
timer:close()
|
timer:close()
|
||||||
timers_by_buffer[buf] = nil
|
timers_by_buffer[buf] = nil
|
||||||
|
|
||||||
|
logger.log(buf, "Timer canceled")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -37,6 +40,8 @@ local function debounce(lfn, duration)
|
|||||||
timers_by_buffer[buf] = nil
|
timers_by_buffer[buf] = nil
|
||||||
end, duration)
|
end, duration)
|
||||||
timers_by_buffer[buf] = timer
|
timers_by_buffer[buf] = timer
|
||||||
|
|
||||||
|
logger.log(buf, "Timer started")
|
||||||
end
|
end
|
||||||
return inner_debounce
|
return inner_debounce
|
||||||
end
|
end
|
||||||
@@ -64,11 +69,15 @@ local function should_be_saved(buf)
|
|||||||
return cnf.opts.condition(buf)
|
return cnf.opts.condition(buf)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
logger.log(buf, "Should save buffer")
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function save(buf)
|
local function save(buf)
|
||||||
if not api.nvim_buf_get_option(buf, "modified") then
|
if not api.nvim_buf_get_option(buf, "modified") then
|
||||||
|
logger.log(buf, "Abort saving buffer")
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -88,7 +97,7 @@ local function save(buf)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
callback("after_saving")
|
logger.log(buf, "Saved buffer")
|
||||||
|
|
||||||
if cnf.opts.execution_message.enabled == true then
|
if cnf.opts.execution_message.enabled == true then
|
||||||
echo_execution_message()
|
echo_execution_message()
|
||||||
@@ -169,7 +178,6 @@ function M.on()
|
|||||||
group = "AutoSave",
|
group = "AutoSave",
|
||||||
})
|
})
|
||||||
|
|
||||||
callback("enabling")
|
|
||||||
autosave_running = true
|
autosave_running = true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -178,7 +186,6 @@ function M.off()
|
|||||||
clear = true,
|
clear = true,
|
||||||
})
|
})
|
||||||
|
|
||||||
callback("disabling")
|
|
||||||
autosave_running = false
|
autosave_running = false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -194,6 +201,7 @@ end
|
|||||||
|
|
||||||
function M.setup(custom_opts)
|
function M.setup(custom_opts)
|
||||||
cnf:set_options(custom_opts)
|
cnf:set_options(custom_opts)
|
||||||
|
logger = require("auto-save.utils.logging").new(cnf:get_options())
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
42
lua/auto-save/utils/logging.lua
Normal file
42
lua/auto-save/utils/logging.lua
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
-- inspired from https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/log.lua
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local outfile = string.format("%s/auto-save.log", vim.api.nvim_call_function("stdpath", { "cache" }))
|
||||||
|
|
||||||
|
-- it could be that the directory of the file does not exist
|
||||||
|
-- this would require further checks, see https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/log.lua#L138
|
||||||
|
--- @param message string
|
||||||
|
local write_to_outfile = function(message)
|
||||||
|
local f = assert(io.open(outfile, "a"))
|
||||||
|
f:write(message)
|
||||||
|
f:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
M.new = function(options)
|
||||||
|
local enabled = options.debug
|
||||||
|
|
||||||
|
--- @param buf number | nil
|
||||||
|
--- @param message string
|
||||||
|
local log = function(buf, message)
|
||||||
|
if not enabled then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local log_message
|
||||||
|
if buf ~= nil then
|
||||||
|
local filename = vim.api.nvim_buf_get_name(buf)
|
||||||
|
log_message = string.format("[%s] [%s] - %s\n", os.date(), filename, message)
|
||||||
|
else
|
||||||
|
log_message = string.format("[%s] - %s\n", os.date(), message)
|
||||||
|
end
|
||||||
|
|
||||||
|
write_to_outfile(log_message)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
log = log,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
Reference in New Issue
Block a user