Skip to content

Commit 5cbb891

Browse files
committed
feat(core): improve logging
1 parent 51f8b32 commit 5cbb891

File tree

10 files changed

+430
-175
lines changed

10 files changed

+430
-175
lines changed

lua/image/backends/kitty/helpers.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local codes = require("image/backends/kitty/codes")
22
local utils = require("image/utils")
3+
local log = require("image/utils/logger").within("backend.kitty")
34

45
local uv = vim.uv
56
-- Allow for loop to be used on older versions
@@ -28,7 +29,7 @@ local write = function(data, tty, escape)
2829

2930
local payload = data
3031
if escape and utils.tmux.is_tmux then payload = utils.tmux.escape(data) end
31-
-- utils.debug("write:", vim.inspect(payload), tty)
32+
log.debug("write", { payload_len = #payload, tty = tty })
3233
if tty then
3334
local handle = io.open(tty, "w")
3435
if not handle then error("failed to open tty") end
@@ -70,7 +71,7 @@ end
7071
local write_graphics = function(config, data)
7172
local control_payload = ""
7273

73-
-- utils.debug("kitty.write_graphics()", config, data)
74+
log.debug("write_graphics", { config = config, has_data = data ~= nil })
7475

7576
for k, v in pairs(config) do
7677
if v ~= nil then
@@ -112,7 +113,7 @@ local write_graphics = function(config, data)
112113
uv.sleep(1)
113114
end
114115
else
115-
-- utils.debug("kitty control payload:", control_payload)
116+
log.debug("control payload", { payload = control_payload })
116117
write("\x1b_G" .. control_payload .. "\x1b\\", config.tty, true)
117118
end
118119
end

lua/image/backends/kitty/init.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local codes = require("image/backends/kitty/codes")
22
local helpers = require("image/backends/kitty/helpers")
33
local utils = require("image/utils")
4+
local log = require("image/utils/logger").within("backend.kitty")
45

56
local editor_tty = utils.term.get_tty()
67
local is_SSH = (vim.env.SSH_CLIENT ~= nil) or (vim.env.SSH_TTY ~= nil)
@@ -58,7 +59,7 @@ backend.render = function(image, x, y, width, height)
5859
}
5960
if with_virtual_placeholders then transmit_payload.display_virtual_placeholder = 1 end
6061
helpers.write_graphics(transmit_payload, image.cropped_path)
61-
-- utils.debug("[kitty] transmitted image " .. image.id .. " (" .. image.internal_id .. ")")
62+
log.debug("transmitted image " .. image.id .. " (" .. image.internal_id .. ")")
6263
end
6364
if backend.features.crop then
6465
local preprocessing_hash = ("%s-%s"):format(image.id, image.resize_hash)
@@ -156,7 +157,7 @@ backend.render = function(image, x, y, width, height)
156157
backend.state.images[image.id] = image
157158
image.is_rendered = true
158159

159-
-- utils.debug("path:", image.cropped_path, display_payload)
160+
log.debug("path:", { path = image.cropped_path, payload = display_payload })
160161
end
161162

162163
local get_clear_tty_override = function()
@@ -188,7 +189,7 @@ backend.clear = function(image_id, shallow)
188189
backend.state.images[image_id] = nil
189190
transmitted_images[image.id] = nil
190191
end
191-
-- utils.debug("[kitty] cleared image", image.id, "(" .. image.internal_id .. ")", "shallow:", shallow)
192+
log.debug("cleared image", { id = image.id, internal_id = image.internal_id, shallow = shallow })
192193
return
193194
end
194195

@@ -200,14 +201,14 @@ backend.clear = function(image_id, shallow)
200201
tty = get_clear_tty_override(),
201202
})
202203

203-
-- utils.debug("[kitty] cleared all")
204+
log.debug("cleared all")
204205
for id, image in pairs(backend.state.images) do
205206
image.is_rendered = false
206207
if not shallow then
207208
backend.state.images[id] = nil
208209
transmitted_images[image.id] = nil
209210
end
210-
-- utils.debug("[kitty] cleared image (all)", image.id, "shallow:", shallow)
211+
log.debug("cleared image (all)", { id = image.id, shallow = shallow })
211212
end
212213
end
213214

lua/image/backends/ueberzug.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local utils = require("image/utils")
2+
local log = require("image/utils/logger").within("backend.ueberzug")
23

34
local child = nil
45
local should_be_alive = false
@@ -27,7 +28,7 @@ local spawn = function()
2728

2829
local write = function(data)
2930
local serialized = vim.fn.json_encode(data)
30-
-- utils.debug("ueberzug:stdin", serialized)
31+
log.debug("stdin", { data = serialized })
3132
vim.loop.write(stdin, serialized .. "\n")
3233
end
3334

lua/image/image.lua

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local renderer = require("image/renderer")
22
local utils = require("image/utils")
3+
local log = require("image/utils/logger").within("image")
34

45
-- { ["buf:row:col"]: { id, height } }
56
---@type table<string, { id: number, height: number }>
@@ -55,7 +56,7 @@ function Image:render(geometry)
5556

5657
-- track last_modified and wipe cache
5758
local current_last_modified = vim.fn.getftime(self.original_path)
58-
-- utils.debug(("timestamp: %s, last_modified: %s"):format(current_last_modified, self.last_modified))
59+
log.debug(("timestamp: %s, last_modified: %s"):format(current_last_modified, self.last_modified))
5960
if self.last_modified ~= current_last_modified then
6061
self.last_modified = current_last_modified
6162
self.resize_hash = nil
@@ -77,18 +78,18 @@ function Image:render(geometry)
7778
renderer.clear_cache_for_path(self.original_path)
7879
end
7980

80-
-- utils.debug(("---------------- %s ----------------"):format(self.id))
81+
log.debug(("---------------- %s ----------------"):format(self.id))
8182
local was_rendered = renderer.render(self)
8283

83-
-- utils.debug(
84-
-- ("[image] success: %s x: %s, y: %s, width: %s, height: %s"):format(
85-
-- was_rendered,
86-
-- self.geometry.x,
87-
-- self.geometry.y,
88-
-- self.geometry.width,
89-
-- self.geometry.height
90-
-- )
91-
-- )
84+
log.debug(
85+
("success: %s x: %s, y: %s, width: %s, height: %s"):format(
86+
was_rendered,
87+
self.geometry.x,
88+
self.geometry.y,
89+
self.geometry.width,
90+
self.geometry.height
91+
)
92+
)
9293

9394
-- clear if already rendered but rendering this should be prevented
9495
if self.is_rendered and not was_rendered then
@@ -112,7 +113,7 @@ function Image:render(geometry)
112113

113114
if not has_up_to_date_extmark then
114115
if previous_extmark ~= nil then
115-
-- utils.debug(("(image.render) clearing extmark %s"):format(previous_extmark.id))
116+
log.debug(("clearing extmark %s"):format(previous_extmark.id))
116117
vim.api.nvim_buf_del_extmark(self.buffer, self.global_state.extmarks_namespace, previous_extmark.id)
117118
buf_extmark_map[extmark_key] = nil
118119
end
@@ -128,7 +129,7 @@ function Image:render(geometry)
128129
extmark_opts.virt_lines = filler
129130
end
130131

131-
-- utils.debug(("(image.render) creating extmark %s"):format(self.internal_id))
132+
log.debug(("creating extmark %s"):format(self.internal_id))
132133
local extmark_row = math.max(row or 0, 0)
133134
local extmark_col = math.max(col or 0, 0)
134135
local ok, extmark_id = pcall(
@@ -165,7 +166,7 @@ end
165166

166167
---@param shallow? boolean
167168
function Image:clear(shallow)
168-
-- utils.debug(("[image] clear %s, shallow: %s"):format(self.id, shallow))
169+
log.debug(("clear %s, shallow: %s"):format(self.id, shallow))
169170
self.global_state.backend.clear(self.id, shallow or false)
170171

171172
self.rendered_geometry = {
@@ -262,7 +263,7 @@ local from_file = function(path, options, state)
262263

263264
-- bail if not an image
264265
if not utils.magic.is_image(absolute_original_path) then
265-
-- utils.debug(("image.nvim: not an image: %s"):format(absolute_original_path))
266+
log.info(("not an image: %s"):format(absolute_original_path))
266267
return nil
267268
end
268269

@@ -302,7 +303,7 @@ local from_file = function(path, options, state)
302303
namespace = opts.namespace or nil,
303304
last_modified = vim.fn.getftime(absolute_original_path),
304305
}, state)
305-
-- utils.debug(("image.nvim: cloned image %s from %s"):format(clone.id, instance.id))
306+
log.debug(("cloned image %s from %s"):format(clone.id, instance.id))
306307
return clone
307308
end
308309
end
@@ -386,7 +387,7 @@ local from_url = function(url, options, callback, state)
386387
}, function(code, signal)
387388
if code ~= 0 then
388389
if state.options.ignore_download_error then
389-
utils.debug("image: curl errored while downloading " .. url, {
390+
log.error("curl errored while downloading " .. url, {
390391
code = code,
391392
signal = signal,
392393
})
@@ -402,7 +403,7 @@ local from_url = function(url, options, callback, state)
402403

403404
vim.loop.read_start(stdout, function(err, data)
404405
assert(not err, err)
405-
if not data then utils.debug("image: downloaded " .. url .. " to " .. tmp_path) end
406+
if not data then log.info("downloaded " .. url .. " to " .. tmp_path) end
406407
state.remote_cache[url] = tmp_path
407408

408409
vim.defer_fn(function()

lua/image/init.lua

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
local utils = require("image/utils")
22
local processors = require("image/processors")
33
local report = require("image/report")
4+
local logger = require("image/utils/logger")
5+
local log = logger.within("core")
46

57
---@type Options
68
local default_options = {
79
-- backend = "ueberzug",
810
backend = "kitty",
911
processor = "magick_cli",
12+
debug = {
13+
enabled = false,
14+
level = "debug",
15+
file_path = "/tmp/image.nvim.log",
16+
format = "compact",
17+
},
1018
integrations = {
1119
markdown = {
1220
enabled = true,
@@ -69,6 +77,11 @@ api.setup = function(options)
6977
local opts = vim.tbl_deep_extend("force", default_options, options or {})
7078
state.options = opts
7179

80+
-- setup logger with debug configuration
81+
if opts.debug then
82+
logger.setup(opts.debug)
83+
end
84+
7285
vim.schedule(function()
7386
if opts.processor == "magick_rock" then
7487
local magick = require("image/magick")
@@ -94,8 +107,14 @@ api.setup = function(options)
94107
-- load integrations
95108
for integration_name, integration_options in pairs(opts.integrations) do
96109
if integration_options.enabled then
97-
local integration = require("image/integrations/" .. integration_name)
98-
if type(integration.setup) == "function" then integration.setup(api, integration_options, state) end
110+
log.debug("Loading integration: " .. integration_name)
111+
local ok, integration = pcall(require, "image/integrations/" .. integration_name)
112+
if not ok then
113+
log.error("Failed to load integration: " .. integration_name, integration)
114+
elseif type(integration.setup) == "function" then
115+
integration.setup(api, integration_options, state)
116+
log.debug("Setup integration: " .. integration_name)
117+
end
99118
end
100119
end
101120

@@ -192,7 +211,7 @@ api.setup = function(options)
192211
{ topline = topline, botline = botline, bufnr = bufnr, height = height, folded_lines = folded_lines }
193212

194213
-- execute deferred clear / rerender
195-
-- utils.debug("needs_clear", needs_clear, "needs_rerender", needs_rerender)
214+
log.debug("needs_clear", { needs_clear = needs_clear, needs_rerender = needs_rerender })
196215
vim.schedule(function()
197216
if needs_clear then
198217
for _, curr in ipairs(api.get_images({ window = winid, buffer = prev.bufnr })) do
@@ -331,7 +350,7 @@ api.setup = function(options)
331350
if not state.enabled then return end
332351

333352
vim.schedule(function()
334-
-- utils.debug("FocusLost")
353+
log.debug("FocusLost")
335354
if
336355
state.options.editor_only_render_when_focused
337356
or (utils.tmux.is_tmux and utils.tmux.get_window_id() ~= initial_tmux_window_id)
@@ -357,7 +376,7 @@ api.setup = function(options)
357376
-- bail if not enabled
358377
if not state.enabled then return end
359378

360-
-- utils.debug("FocusGained")
379+
log.debug("FocusGained")
361380

362381
state.disable_decorator_handling = false
363382

lua/image/integrations/markdown.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local document = require("image/utils/document")
22

33
return document.create_document_integration({
44
name = "markdown",
5-
-- debug = true,
5+
debug = true,
66
default_options = {
77
clear_in_insert_mode = false,
88
download_remote_images = true,

0 commit comments

Comments
 (0)