Skip to content

Commit 02fdc26

Browse files
authored
fix(#1970): additional log function gating for efficiency when not logging (#1971)
* fix(#1970): additional log function gating for efficiency when not logging * fix(#1970): additional log function gating for efficiency when not logging
1 parent 59e65d8 commit 02fdc26

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

lua/nvim-tree.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,10 @@ function M.setup(conf)
880880
require("nvim-tree.notify").setup(opts)
881881
require("nvim-tree.log").setup(opts)
882882

883-
log.line("config", "default config + user")
884-
log.raw("config", "%s\n", vim.inspect(opts))
883+
if log.enabled "config" then
884+
log.line("config", "default config + user")
885+
log.raw("config", "%s\n", vim.inspect(opts))
886+
end
885887

886888
require("nvim-tree.actions").setup(opts)
887889
require("nvim-tree.keymap").setup(opts)

lua/nvim-tree/actions/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,10 @@ function M.setup(opts)
437437

438438
require("nvim-tree.actions.dispatch").setup(M.custom_keypress_funcs)
439439

440-
log.line("config", "active mappings")
441-
log.raw("config", "%s\n", vim.inspect(M.mappings))
440+
if log.enabled "config" then
441+
log.line("config", "active mappings")
442+
log.raw("config", "%s\n", vim.inspect(M.mappings))
443+
end
442444
end
443445

444446
return M

lua/nvim-tree/git/runner.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function Runner:_getopts(stdout_handle, stderr_handle)
5959
end
6060

6161
function Runner:_log_raw_output(output)
62-
if output and type(output) == "string" then
62+
if log.enabled "git" and output and type(output) == "string" then
6363
log.raw("git", "%s", output)
6464
log.line("git", "done")
6565
end

lua/nvim-tree/log.lua

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local M = {
88
--- @param fmt string for string.format
99
--- @vararg any arguments for string.format
1010
function M.raw(typ, fmt, ...)
11-
if not M.path or not M.config.types[typ] and not M.config.types.all then
11+
if not M.enabled(typ) then
1212
return
1313
end
1414

@@ -21,32 +21,48 @@ function M.raw(typ, fmt, ...)
2121
end
2222
end
2323

24-
--- Write to log file via M.line
24+
--- Write profile start to log file
2525
--- START is prefixed
26+
--- @param fmt string for string.format
27+
--- @vararg any arguments for string.format
2628
--- @return number nanos to pass to profile_end
2729
function M.profile_start(fmt, ...)
28-
if not M.path or not M.config.types.profile and not M.config.types.all then
30+
if M.enabled "profile" then
31+
M.line("profile", "START " .. (fmt or "???"), ...)
32+
return vim.loop.hrtime()
33+
else
2934
return 0
3035
end
31-
M.line("profile", "START " .. (fmt or "???"), ...)
32-
return vim.loop.hrtime()
3336
end
3437

35-
--- Write to log file via M.line
38+
--- Write profile end to log file
3639
--- END is prefixed and duration in seconds is suffixed
3740
--- @param start number nanos returned from profile_start
41+
--- @param fmt string for string.format
42+
--- @vararg any arguments for string.format
3843
function M.profile_end(start, fmt, ...)
39-
if not M.path or not M.config.types.profile and not M.config.types.all then
40-
return
44+
if M.enabled "profile" then
45+
local millis = start and math.modf((vim.loop.hrtime() - start) / 1000000) or -1
46+
M.line("profile", "END " .. (fmt or "???") .. " " .. millis .. "ms", ...)
4147
end
42-
local millis = start and math.modf((vim.loop.hrtime() - start) / 1000000) or -1
43-
M.line("profile", "END " .. (fmt or "???") .. " " .. millis .. "ms", ...)
4448
end
4549

46-
-- Write to log file via M.raw
47-
-- time and typ are prefixed and a trailing newline is added
50+
--- Write to log file
51+
--- time and typ are prefixed and a trailing newline is added
52+
--- @param typ string as per log.types config
53+
--- @param fmt string for string.format
54+
--- @vararg any arguments for string.format
4855
function M.line(typ, fmt, ...)
49-
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y-%m-%d %H:%M:%S", typ, fmt), ...)
56+
if M.enabled(typ) then
57+
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y-%m-%d %H:%M:%S", typ, fmt), ...)
58+
end
59+
end
60+
61+
--- Logging is enabled for typ or all
62+
--- @param typ string as per log.types config
63+
--- @return boolean
64+
function M.enabled(typ)
65+
return M.path ~= nil and (M.config.types[typ] or M.config.types.all)
5066
end
5167

5268
function M.setup(opts)

0 commit comments

Comments
 (0)