Skip to content

Commit dc90162

Browse files
committed
Fix log level and error handling
1 parent bee227c commit dc90162

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

lua/devcontainer-cli/log.lua

+19-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local global_config = require("devcontainer-cli.config")
1111
-- User configuration section
1212
local default_config = {
1313
-- Name of the plugin. Prepended to log messages
14-
plugin = 'decontainer-cli',
14+
plugin = "devcontainer-cli",
1515

1616
-- Should print the output to neovim while running
1717
use_console = true,
@@ -29,12 +29,12 @@ local default_config = {
2929

3030
-- Level configuration
3131
modes = {
32-
{ name = "trace", hl = "Comment", },
33-
{ name = "debug", hl = "Comment", },
34-
{ name = "info", hl = "None", },
35-
{ name = "warn", hl = "WarningMsg", },
36-
{ name = "error", hl = "ErrorMsg", },
37-
{ name = "fatal", hl = "ErrorMsg", },
32+
{ name = "trace", hl = "Comment" },
33+
{ name = "debug", hl = "Comment" },
34+
{ name = "info", hl = "None" },
35+
{ name = "warn", hl = "WarningMsg" },
36+
{ name = "error", hl = "ErrorMsg" },
37+
{ name = "fatal", hl = "ErrorMsg" },
3838
},
3939

4040
-- Can limit the number of decimals displayed for floats
@@ -49,7 +49,7 @@ local unpack = unpack or table.unpack
4949
log.new = function(config, standalone)
5050
config = vim.tbl_deep_extend("force", default_config, config)
5151

52-
local outfile = string.format('%s/%s.log', vim.fn.stdpath("cache"), config.plugin)
52+
local outfile = string.format("%s/%s.log", vim.fn.stdpath("cache"), config.plugin)
5353

5454
local obj
5555
if standalone then
@@ -66,12 +66,12 @@ log.new = function(config, standalone)
6666
local round = function(x, increment)
6767
increment = increment or 1
6868
x = x / increment
69-
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
69+
return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment
7070
end
7171

7272
local make_string = function(...)
7373
local t = {}
74-
for i = 1, select('#', ...) do
74+
for i = 1, select("#", ...) do
7575
local x = select(i, ...)
7676

7777
if type(x) == "number" and config.float_precision then
@@ -87,7 +87,6 @@ log.new = function(config, standalone)
8787
return table.concat(t, " ")
8888
end
8989

90-
9190
local log_at_level = function(level, level_config, message_maker, ...)
9291
local nameupper = level_config.name:upper()
9392

@@ -120,13 +119,15 @@ log.new = function(config, standalone)
120119
end
121120

122121
-- Output to log file
123-
if config.use_file and level >= levels[config.log_level] then
124-
local fp = io.open(outfile, "a")
125-
local str = string.format("[%-6s%s] %s: %s\n",
126-
nameupper, os.date(), lineinfo, msg)
127-
if fp ~= nil then
122+
if config.use_file and level < levels[config.log_level] then
123+
local ok, err = pcall(function()
124+
local fp = assert(io.open(outfile, "a"))
125+
local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
128126
fp:write(str)
129127
fp:close()
128+
end)
129+
if not ok then
130+
vim.notify(string.format("Failed to write to log file: %s", err), vim.log.levels.ERROR)
130131
end
131132
end
132133
end
@@ -136,9 +137,9 @@ log.new = function(config, standalone)
136137
return log_at_level(i, x, make_string, ...)
137138
end
138139

139-
obj[("fmt_%s" ):format(x.name)] = function()
140+
obj[("fmt_%s"):format(x.name)] = function()
140141
return log_at_level(i, x, function(...)
141-
local passed = {...}
142+
local passed = { ... }
142143
local fmt = table.remove(passed, 1)
143144
local inspected = {}
144145
for _, v in ipairs(passed) do

0 commit comments

Comments
 (0)