|
| 1 | +--[[ |
| 2 | +--FROM: https://github.com/tjdevries/vlog.nvim |
| 3 | +--]] |
| 4 | + |
| 5 | +-- log.lua |
| 6 | +-- |
| 7 | +-- Inspired by rxi/log.lua |
| 8 | +-- Modified by tjdevries and can be found at github.com/tjdevries/vlog.nvim |
| 9 | +-- |
| 10 | +-- This library is free software; you can redistribute it and/or modify it |
| 11 | +-- under the terms of the MIT license. See LICENSE for details. |
| 12 | + |
| 13 | +-- User configuration section |
| 14 | +local default_config = { |
| 15 | + -- Name of the plugin. Prepended to log messages |
| 16 | + plugin = 'nvim-java-refactor', |
| 17 | + |
| 18 | + -- Should print the output to neovim while running |
| 19 | + use_console = false, |
| 20 | + |
| 21 | + -- Should highlighting be used in console (using echohl) |
| 22 | + highlights = true, |
| 23 | + |
| 24 | + -- Should write to a file |
| 25 | + use_file = true, |
| 26 | + |
| 27 | + -- Any messages above this level will be logged. |
| 28 | + level = 'trace', |
| 29 | + |
| 30 | + -- Level configuration |
| 31 | + 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' }, |
| 38 | + }, |
| 39 | + |
| 40 | + -- Can limit the number of decimals displayed for floats |
| 41 | + float_precision = 0.01, |
| 42 | +} |
| 43 | + |
| 44 | +-- {{{ NO NEED TO CHANGE |
| 45 | +local log = {} |
| 46 | + |
| 47 | +local unpack = unpack or table.unpack |
| 48 | + |
| 49 | +log.new = function(config, standalone) |
| 50 | + config = vim.tbl_deep_extend('force', default_config, config) |
| 51 | + |
| 52 | + local outfile = string.format( |
| 53 | + '%s/%s.log', |
| 54 | + vim.api.nvim_call_function('stdpath', { 'data' }), |
| 55 | + config.plugin |
| 56 | + ) |
| 57 | + |
| 58 | + local obj |
| 59 | + if standalone then |
| 60 | + obj = log |
| 61 | + else |
| 62 | + obj = {} |
| 63 | + end |
| 64 | + |
| 65 | + local levels = {} |
| 66 | + for i, v in ipairs(config.modes) do |
| 67 | + levels[v.name] = i |
| 68 | + end |
| 69 | + |
| 70 | + local round = function(x, increment) |
| 71 | + increment = increment or 1 |
| 72 | + x = x / increment |
| 73 | + return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment |
| 74 | + end |
| 75 | + |
| 76 | + local make_string = function(...) |
| 77 | + local t = {} |
| 78 | + for i = 1, select('#', ...) do |
| 79 | + local x = select(i, ...) |
| 80 | + |
| 81 | + if type(x) == 'number' and config.float_precision then |
| 82 | + x = tostring(round(x, config.float_precision)) |
| 83 | + elseif type(x) == 'table' then |
| 84 | + x = vim.inspect(x) |
| 85 | + else |
| 86 | + x = tostring(x) |
| 87 | + end |
| 88 | + |
| 89 | + t[#t + 1] = x |
| 90 | + end |
| 91 | + return table.concat(t, ' ') |
| 92 | + end |
| 93 | + |
| 94 | + local log_at_level = function(level, level_config, message_maker, ...) |
| 95 | + -- Return early if we're below the config.level |
| 96 | + if level < levels[config.level] then |
| 97 | + return |
| 98 | + end |
| 99 | + local nameupper = level_config.name:upper() |
| 100 | + |
| 101 | + local msg = message_maker(...) |
| 102 | + local info = debug.getinfo(2, 'Sl') |
| 103 | + local lineinfo = info.short_src .. ':' .. info.currentline |
| 104 | + |
| 105 | + -- Output to console |
| 106 | + if config.use_console then |
| 107 | + local console_string = string.format( |
| 108 | + '[%-6s%s] %s: %s', |
| 109 | + nameupper, |
| 110 | + os.date('%H:%M:%S'), |
| 111 | + lineinfo, |
| 112 | + msg |
| 113 | + ) |
| 114 | + |
| 115 | + if config.highlights and level_config.hl then |
| 116 | + vim.cmd(string.format('echohl %s', level_config.hl)) |
| 117 | + end |
| 118 | + |
| 119 | + local split_console = vim.split(console_string, '\n') |
| 120 | + for _, v in ipairs(split_console) do |
| 121 | + vim.cmd( |
| 122 | + string.format( |
| 123 | + [[echom "[%s] %s"]], |
| 124 | + config.plugin, |
| 125 | + vim.fn.escape(v, '"') |
| 126 | + ) |
| 127 | + ) |
| 128 | + end |
| 129 | + |
| 130 | + if config.highlights and level_config.hl then |
| 131 | + vim.cmd('echohl NONE') |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + -- Output to log file |
| 136 | + if config.use_file then |
| 137 | + local fp = io.open(outfile, 'a') |
| 138 | + local str = |
| 139 | + string.format('[%-6s%s] %s: %s\n', nameupper, os.date(), lineinfo, msg) |
| 140 | + assert(fp, 'cannot open file: ' .. ' to write logs') |
| 141 | + fp:write(str) |
| 142 | + fp:close() |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + for i, x in ipairs(config.modes) do |
| 147 | + obj[x.name] = function(...) |
| 148 | + return log_at_level(i, x, make_string, ...) |
| 149 | + end |
| 150 | + |
| 151 | + obj[('fmt_%s'):format(x.name)] = function(...) |
| 152 | + local passed = { ... } |
| 153 | + return log_at_level(i, x, function() |
| 154 | + local fmt = table.remove(passed, 1) |
| 155 | + local inspected = {} |
| 156 | + for _, v in ipairs(passed) do |
| 157 | + table.insert(inspected, vim.inspect(v)) |
| 158 | + end |
| 159 | + return string.format(fmt, unpack(inspected)) |
| 160 | + end) |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + return obj |
| 165 | +end |
| 166 | + |
| 167 | +log.new(default_config, true) |
| 168 | + |
| 169 | +return log |
0 commit comments