Skip to content

Commit 9130e58

Browse files
authored
fix(log): set log settings immediately, fix no-op logic (#1881)
1 parent 3dd783a commit 9130e58

File tree

4 files changed

+87
-34
lines changed

4 files changed

+87
-34
lines changed

lua/neo-tree.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ end
109109
M.setup = function(config)
110110
-- merging is deferred until ensure_config
111111
new_user_config = config
112+
if new_user_config.log_level ~= nil then
113+
M.set_log_level(new_user_config.log_level)
114+
end
115+
require("neo-tree.log").use_file(new_user_config.log_to_file, true)
112116
if vim.v.vim_did_enter == 0 then
113117
try_netrw_hijack(vim.fn.argv(0) --[[@as string]])
114118
end

lua/neo-tree/log.lua

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ local log_maker = {}
8080

8181
---@class (partial) neotree.Logger.PartialConfig : neotree.Logger.Config
8282
---@param config neotree.Logger.PartialConfig|neotree.Logger.Config
83-
---@param parent neotree.Logger?
8483
---@return neotree.Logger
85-
log_maker.new = function(config, parent)
84+
log_maker.new = function(config)
8685
---@class neotree.Logger
8786
local log = {}
8887
---@diagnostic disable-next-line: cast-local-type
@@ -187,7 +186,7 @@ log_maker.new = function(config, parent)
187186
---@param log_level vim.log.levels
188187
---@param message_maker fun(...):string
189188
local logfunc = function(log_level, message_maker)
190-
if log_level > log.minimum_level.file and log_level > log.minimum_level.console then
189+
if log_level < log.minimum_level.file and log_level < log.minimum_level.console then
191190
return function() end
192191
end
193192
local level_config = config.level_configs[log_level]
@@ -220,15 +219,17 @@ log_maker.new = function(config, parent)
220219
---@param lvl neotree.Log.Level
221220
---@return vim.log.levels
222221
local to_loglevel = function(lvl)
222+
if type(lvl) == "number" then
223+
return lvl
224+
end
225+
223226
if type(lvl) == "string" then
224227
local levelupper = lvl:upper()
225228
for name, level_num in pairs(Levels) do
226229
if levelupper == name then
227230
return level_num
228231
end
229232
end
230-
elseif type(lvl) == "number" then
231-
return lvl
232233
end
233234
notify("Couldn't resolve log level " .. lvl .. "defaulting to log level INFO", Levels.WARN)
234235
return Levels.INFO
@@ -287,33 +288,37 @@ log_maker.new = function(config, parent)
287288
---@return boolean using_file
288289
log.use_file = function(file, quiet)
289290
if file == false then
291+
config.use_file = false
290292
if not quiet then
291293
log.info("Logging to file disabled")
292294
end
295+
return config.use_file
296+
end
297+
log.outfile = type(file) == "string" and file or initial_filepath
298+
local fp, err = io.open(log.outfile, "a+")
299+
300+
if not fp then
293301
config.use_file = false
294-
else
295-
if type(file) == "string" then
296-
log.outfile = file
297-
else
298-
log.outfile = initial_filepath
299-
end
300-
local fp = io.open(log.outfile, "a+")
301-
if fp then
302-
local new_logfile_ino = assert(uv.fs_stat(log.outfile)).ino
303-
if new_logfile_ino ~= current_logfile_inode then
304-
-- the fp is pointing to a new/different file than previously
305-
log.file = fp
306-
log.file:setvbuf("line")
307-
current_logfile_inode = new_logfile_ino
308-
end
309-
config.use_file = true
310-
if not quiet then
311-
log.info("Logging to file:", log.outfile)
312-
end
313-
else
314-
config.use_file = false
315-
log.warn("Could not open log file:", log.outfile)
316-
end
302+
log.warn("Could not open log file:", log.outfile, err)
303+
return config.use_file
304+
end
305+
306+
local stat, stat_err = uv.fs_stat(log.outfile)
307+
if not stat then
308+
config.use_file = false
309+
log.warn("Could not stat log file:", log.outfile, stat_err)
310+
return config.use_file
311+
end
312+
313+
if stat.ino ~= current_logfile_inode then
314+
-- the fp is pointing to a different file
315+
log.file = fp
316+
log.file:setvbuf("line")
317+
current_logfile_inode = stat.ino
318+
end
319+
config.use_file = true
320+
if not quiet then
321+
log.info("Logging to file:", log.outfile)
317322
end
318323
return config.use_file
319324
end
@@ -353,8 +358,7 @@ log_maker.new = function(config, parent)
353358
"force",
354359
config,
355360
{ context = vim.list_extend({ new_context }, { context }) }
356-
),
357-
log
361+
)
358362
)
359363
end
360364

lua/neo-tree/setup/init.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,6 @@ M.merge_config = function(user_config)
428428
end, 50)
429429
end
430430

431-
if user_config.log_level ~= nil then
432-
M.set_log_level(user_config.log_level)
433-
end
434-
log.use_file(user_config.log_to_file, true)
435431
log.debug("setup")
436432

437433
if events_setup then

tests/neo-tree/log_spec.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local log = require("neo-tree.log")
2+
local debug_locals = function(lvl)
3+
local i = 1
4+
local results = {}
5+
while true do
6+
local name, value = debug.getlocal(lvl, i)
7+
if not name then
8+
return results
9+
end
10+
results[#results + 1] = { name = name, value = value }
11+
i = i + 1
12+
end
13+
end
14+
describe("File logging", function()
15+
it("should get correct debug info", function()
16+
require("neo-tree").setup({
17+
log_to_file = true,
18+
log_level = {
19+
console = vim.log.levels.OFF,
20+
file = vim.log.levels.INFO,
21+
},
22+
})
23+
local expected = debug.getinfo(1, "Sln")
24+
local check_getinfo_at_format = false
25+
local debug_getinfo_hook = function(...)
26+
local running_func = debug.getinfo(2, "Sln")
27+
28+
if running_func.name == "getinfo" then
29+
check_getinfo_at_format = true
30+
elseif check_getinfo_at_format and running_func.name == "format" then
31+
for _, localinfo in ipairs(debug_locals(4)) do
32+
local actual = localinfo.value
33+
if type(actual) == "table" then
34+
if actual.lastlinedefined then
35+
---@cast actual debuginfo
36+
assert(
37+
actual.lastlinedefined == expected.lastlinedefined
38+
and actual.source == expected.source
39+
)
40+
end
41+
end
42+
end
43+
debug.sethook()
44+
end
45+
end
46+
debug.sethook(debug_getinfo_hook, "cl")
47+
log.info("")
48+
end)
49+
end)

0 commit comments

Comments
 (0)