Skip to content

Commit

Permalink
feat(log): Allow user-customizable log messages. (#486)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matu3ba committed May 24, 2023
1 parent 060a332 commit db3e425
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lua/plenary/log.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- log.lua
-- Does only support logging source files.
--
-- Inspired by rxi/log.lua
-- Modified by tjdevries and can be found at github.com/tjdevries/vlog.nvim
Expand Down Expand Up @@ -49,6 +50,23 @@ local default_config = {

-- Can limit the number of decimals displayed for floats.
float_precision = 0.01,

-- Adjust content as needed, but must keep function parameters to be filled
-- by library code.
---@param is_console boolean If output is for console or log file.
---@param mode_name string Level configuration 'modes' field 'name'
---@param src_path string Path to source file given by debug.info.source
---@param src_line integer Line into source file given by debug.info.currentline
---@param msg string Message, which is later on escaped, if needed.
fmt_msg = function(is_console, mode_name, src_path, src_line, msg)
local nameupper = mode_name:upper()
local lineinfo = src_path .. ":" .. src_line
if is_console then
return string.format("[%-6s%s] %s: %s", nameupper, os.date "%H:%M:%S", lineinfo, msg)
else
return string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
end
end,
}

-- {{{ NO NEED TO CHANGE
Expand Down Expand Up @@ -105,16 +123,14 @@ log.new = function(config, standalone)
if level < levels[config.level] then
return
end
local nameupper = level_config.name:upper()

local msg = message_maker(...)
local info = debug.getinfo(config.info_level or 2, "Sl")
local lineinfo = info.short_src .. ":" .. info.currentline

local src_path = info.source:sub(2)
local src_line = info.currentline
-- Output to console
if config.use_console then
local log_to_console = function()
local console_string = string.format("[%-6s%s] %s: %s", nameupper, os.date "%H:%M:%S", lineinfo, msg)
local console_string = config.fmt_msg(true, level_config.name, src_path, src_line, msg)

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

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

0 comments on commit db3e425

Please sign in to comment.