Skip to content

Commit db3e425

Browse files
authored
feat(log): Allow user-customizable log messages. (#486)
Justification: - The default adds 100 characters only for `[TRACE date] filepath:line`: * [TRACE Mi 26 Apr 2023 23:06:02 CEST] * /home/username/projects/libbuf.nvim/lua/libbuf/init.lua:133 - Typical tracing logs contain 40-50 chars for fn name and some variable * `currentBuffersWithPropertis(): bufprops[1] ={` - Assumptions or purpose of the plenary.log are not documented.
1 parent 060a332 commit db3e425

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

lua/plenary/log.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- log.lua
2+
-- Does only support logging source files.
23
--
34
-- Inspired by rxi/log.lua
45
-- Modified by tjdevries and can be found at github.com/tjdevries/vlog.nvim
@@ -49,6 +50,23 @@ local default_config = {
4950

5051
-- Can limit the number of decimals displayed for floats.
5152
float_precision = 0.01,
53+
54+
-- Adjust content as needed, but must keep function parameters to be filled
55+
-- by library code.
56+
---@param is_console boolean If output is for console or log file.
57+
---@param mode_name string Level configuration 'modes' field 'name'
58+
---@param src_path string Path to source file given by debug.info.source
59+
---@param src_line integer Line into source file given by debug.info.currentline
60+
---@param msg string Message, which is later on escaped, if needed.
61+
fmt_msg = function(is_console, mode_name, src_path, src_line, msg)
62+
local nameupper = mode_name:upper()
63+
local lineinfo = src_path .. ":" .. src_line
64+
if is_console then
65+
return string.format("[%-6s%s] %s: %s", nameupper, os.date "%H:%M:%S", lineinfo, msg)
66+
else
67+
return string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
68+
end
69+
end,
5270
}
5371

5472
-- {{{ NO NEED TO CHANGE
@@ -105,16 +123,14 @@ log.new = function(config, standalone)
105123
if level < levels[config.level] then
106124
return
107125
end
108-
local nameupper = level_config.name:upper()
109-
110126
local msg = message_maker(...)
111127
local info = debug.getinfo(config.info_level or 2, "Sl")
112-
local lineinfo = info.short_src .. ":" .. info.currentline
113-
128+
local src_path = info.source:sub(2)
129+
local src_line = info.currentline
114130
-- Output to console
115131
if config.use_console then
116132
local log_to_console = function()
117-
local console_string = string.format("[%-6s%s] %s: %s", nameupper, os.date "%H:%M:%S", lineinfo, msg)
133+
local console_string = config.fmt_msg(true, level_config.name, src_path, src_line, msg)
118134

119135
if config.highlights and level_config.hl then
120136
vim.cmd(string.format("echohl %s", level_config.hl))
@@ -148,13 +164,14 @@ log.new = function(config, standalone)
148164
outfile_parent_path:mkdir { parents = true }
149165
end
150166
local fp = assert(io.open(outfile, "a"))
151-
local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
167+
local str = config.fmt_msg(false, level_config.name, src_path, src_line, msg)
152168
fp:write(str)
153169
fp:close()
154170
end
155171

156172
-- Output to quickfix
157173
if config.use_quickfix then
174+
local nameupper = level_config.name:upper()
158175
local formatted_msg = string.format("[%s] %s", nameupper, msg)
159176
local qf_entry = {
160177
-- remove the @ getinfo adds to the file path

0 commit comments

Comments
 (0)