Skip to content

Commit 31c4dc6

Browse files
committed
fix assert logging
1 parent d82fda3 commit 31c4dc6

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

lua/neo-tree/log.lua

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ log.new = function(config, standalone)
5858
string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "data" }), config.plugin)
5959

6060
local obj
61-
if standalone then
61+
if not standalone then
6262
obj = log
6363
else
6464
obj = {}
@@ -68,7 +68,7 @@ log.new = function(config, standalone)
6868
obj.use_file = function(file, quiet)
6969
if file == false then
7070
if not quiet then
71-
obj.info("[neo-tree] Logging to file disabled")
71+
obj.info("Logging to file disabled")
7272
end
7373
config.use_file = false
7474
else
@@ -79,7 +79,7 @@ log.new = function(config, standalone)
7979
end
8080
config.use_file = true
8181
if not quiet then
82-
obj.info("[neo-tree] Logging to file: " .. obj.outfile)
82+
obj.info("Logging to file: " .. obj.outfile)
8383
end
8484
end
8585
end
@@ -126,6 +126,21 @@ log.new = function(config, standalone)
126126
return table.concat(t, " ")
127127
end
128128

129+
---@param name string
130+
---@param msg string
131+
local log_to_file = function(name, msg)
132+
local info = debug.getinfo(2, "Sl")
133+
local lineinfo = info.short_src .. ":" .. info.currentline
134+
local str = string.format("[%-6s%s] %s: %s\n", name, os.date(), lineinfo, msg)
135+
local fp = io.open(obj.outfile, "a")
136+
if fp then
137+
fp:write(str)
138+
fp:close()
139+
else
140+
print("[neo-tree] Could not open log file: " .. obj.outfile)
141+
end
142+
end
143+
129144
local log_at_level = function(level, level_config, message_maker, ...)
130145
-- Return early if we're below the config.level
131146
if level < levels[config.level] then
@@ -135,22 +150,13 @@ log.new = function(config, standalone)
135150
if vim.v.dying > 0 or vim.v.exiting ~= vim.NIL then
136151
return
137152
end
138-
local nameupper = level_config.name:upper()
139153

140154
local msg = message_maker(...)
141-
local info = debug.getinfo(2, "Sl")
142-
local lineinfo = info.short_src .. ":" .. info.currentline
143155

144156
-- Output to log file
145157
if config.use_file then
146-
local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
147-
local fp = io.open(obj.outfile, "a")
148-
if fp then
149-
fp:write(str)
150-
fp:close()
151-
else
152-
print("[neo-tree] Could not open log file: " .. obj.outfile)
153-
end
158+
local nameupper = level_config.name:upper()
159+
log_to_file(nameupper, msg)
154160
end
155161

156162
-- Output to console
@@ -178,16 +184,19 @@ log.new = function(config, standalone)
178184
end)
179185
end
180186
end
187+
181188
obj.assert = function(v, ...)
182-
vim.print(v)
183189
if v then
184190
return v, ...
185191
end
186-
obj.error(...)
192+
if config.use_file then
193+
log_to_file("ERROR", table.concat({ ... }, " "))
194+
end
195+
error(...)
187196
end
188197
end
189198

190-
log.new(default_config, true)
199+
log.new(default_config, false)
191200
-- }}}
192201

193202
return log

0 commit comments

Comments
 (0)