|
1 | 1 | -- log.lua
|
| 2 | +-- Does only support logging source files. |
2 | 3 | --
|
3 | 4 | -- Inspired by rxi/log.lua
|
4 | 5 | -- Modified by tjdevries and can be found at github.com/tjdevries/vlog.nvim
|
@@ -49,6 +50,23 @@ local default_config = {
|
49 | 50 |
|
50 | 51 | -- Can limit the number of decimals displayed for floats.
|
51 | 52 | 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, |
52 | 70 | }
|
53 | 71 |
|
54 | 72 | -- {{{ NO NEED TO CHANGE
|
@@ -105,16 +123,14 @@ log.new = function(config, standalone)
|
105 | 123 | if level < levels[config.level] then
|
106 | 124 | return
|
107 | 125 | end
|
108 |
| - local nameupper = level_config.name:upper() |
109 |
| - |
110 | 126 | local msg = message_maker(...)
|
111 | 127 | 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 |
114 | 130 | -- Output to console
|
115 | 131 | if config.use_console then
|
116 | 132 | 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) |
118 | 134 |
|
119 | 135 | if config.highlights and level_config.hl then
|
120 | 136 | vim.cmd(string.format("echohl %s", level_config.hl))
|
@@ -148,13 +164,14 @@ log.new = function(config, standalone)
|
148 | 164 | outfile_parent_path:mkdir { parents = true }
|
149 | 165 | end
|
150 | 166 | 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) |
152 | 168 | fp:write(str)
|
153 | 169 | fp:close()
|
154 | 170 | end
|
155 | 171 |
|
156 | 172 | -- Output to quickfix
|
157 | 173 | if config.use_quickfix then
|
| 174 | + local nameupper = level_config.name:upper() |
158 | 175 | local formatted_msg = string.format("[%s] %s", nameupper, msg)
|
159 | 176 | local qf_entry = {
|
160 | 177 | -- remove the @ getinfo adds to the file path
|
|
0 commit comments