From a7d7520bc84269fd29ae265f6142897ccd58d5c6 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 30 Sep 2021 21:41:49 +0200 Subject: fix logging at correct level, also add :LspInstallLog command --- lua/nvim-lsp-installer/installers/context.lua | 4 ++-- lua/nvim-lsp-installer/log.lua | 33 +++++++++++++-------------- lua/nvim-lsp-installer/process.lua | 10 ++++---- lua/nvim-lsp-installer/settings.lua | 16 +++++-------- lua/nvim-lsp-installer/ui/display.lua | 4 ++-- lua/nvim-lsp-installer/ui/status-win/init.lua | 12 +++++----- 6 files changed, 37 insertions(+), 42 deletions(-) (limited to 'lua') diff --git a/lua/nvim-lsp-installer/installers/context.lua b/lua/nvim-lsp-installer/installers/context.lua index 057c3620..fba6683e 100644 --- a/lua/nvim-lsp-installer/installers/context.lua +++ b/lua/nvim-lsp-installer/installers/context.lua @@ -1,5 +1,5 @@ local Data = require "nvim-lsp-installer.data" -local Log = require "nvim-lsp-installer.log" +local log = require "nvim-lsp-installer.log" local process = require "nvim-lsp-installer.process" local platform = require "nvim-lsp-installer.platform" @@ -61,7 +61,7 @@ function M.github_release_file(repo, file) return callback(false) else local version = Data.json_decode(response).tag_name - Log.debug("Resolved latest version", server.name, version) + log.debug("Resolved latest version", server.name, version) context.requested_server_version = version context.github_release_file = get_download_url(version) callback(true) diff --git a/lua/nvim-lsp-installer/log.lua b/lua/nvim-lsp-installer/log.lua index 8aa2e16e..54aa00ea 100644 --- a/lua/nvim-lsp-installer/log.lua +++ b/lua/nvim-lsp-installer/log.lua @@ -16,25 +16,24 @@ local config = { -- Level configuration modes = { - { name = "trace", hl = "Comment" }, - { name = "debug", hl = "Comment" }, - { name = "info", hl = "None" }, - { name = "warn", hl = "WarningMsg" }, - { name = "error", hl = "ErrorMsg" }, - { name = "fatal", hl = "ErrorMsg" }, + { name = "trace", hl = "Comment", level = vim.log.levels.TRACE }, + { name = "debug", hl = "Comment", level = vim.log.levels.DEBUG }, + { name = "info", hl = "None", level = vim.log.levels.INFO }, + { name = "warn", hl = "WarningMsg", level = vim.log.levels.WARN }, + { name = "error", hl = "ErrorMsg", level = vim.log.levels.ERROR }, }, -- Can limit the number of decimals displayed for floats float_precision = 0.01, } -local log = {} +local log = { + outfile = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), config.name), +} local unpack = unpack or table.unpack do - local outfile = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), config.name) - local round = function(x, increment) increment = increment or 1 x = x / increment @@ -59,9 +58,9 @@ do return table.concat(t, " ") end - local log_at_level = function(level, level_config, message_maker, ...) + local log_at_level = function(level_config, message_maker, ...) -- Return early if we're below the current_log_level - if level < settings.current.log_level then + if level_config.level < settings.current.log_level then return end local nameupper = level_config.name:upper() @@ -102,22 +101,22 @@ do -- Output to log file if config.use_file then - local fp = assert(io.open(outfile, "a")) + local fp = assert(io.open(log.outfile, "a")) local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg) fp:write(str) fp:close() end end - for i, x in ipairs(config.modes) do + for _, x in ipairs(config.modes) do -- log.info("these", "are", "separated") log[x.name] = function(...) - return log_at_level(i, x, make_string, ...) + return log_at_level(x, make_string, ...) end -- log.fmt_info("These are %s strings", "formatted") log[("fmt_%s"):format(x.name)] = function(...) - return log_at_level(i, x, function(...) + return log_at_level(x, function(...) local passed = { ... } local fmt = table.remove(passed, 1) local inspected = {} @@ -130,7 +129,7 @@ do -- log.lazy_info(expensive_to_calculate) log[("lazy_%s"):format(x.name)] = function() - return log_at_level(i, x, function(f) + return log_at_level(x, function(f) return f() end) end @@ -140,7 +139,7 @@ do local original_console = config.use_console config.use_console = false config.info_level = override.info_level - log_at_level(i, x, make_string, unpack(vals)) + log_at_level(x, make_string, unpack(vals)) config.use_console = original_console config.info_level = nil end diff --git a/lua/nvim-lsp-installer/process.lua b/lua/nvim-lsp-installer/process.lua index 31523a05..9c6a8c50 100644 --- a/lua/nvim-lsp-installer/process.lua +++ b/lua/nvim-lsp-installer/process.lua @@ -1,4 +1,4 @@ -local Log = require "nvim-lsp-installer.log" +local log = require "nvim-lsp-installer.log" local platform = require "nvim-lsp-installer.platform" local uv = vim.loop @@ -7,7 +7,7 @@ local M = {} local function connect_sink(pipe, sink) return function(err, data) if err then - Log.error("Unexpected error when reading pipe.", err) + log.error("Unexpected error when reading pipe.", err) end if data ~= nil then sink(data) @@ -48,7 +48,7 @@ function M.spawn(cmd, opts, callback) local stdio = { stdin, stdout, stderr } - Log.debug("Spawning", cmd, opts) + log.debug("Spawning", cmd, opts) local spawn_opts = { env = opts.env, @@ -82,13 +82,13 @@ function M.spawn(cmd, opts, callback) end) if handle == nil then - Log.error("Failed to spawn process", cmd, pid) + log.error("Failed to spawn process", cmd, pid) opts.stdio_sink.stderr(("Failed to spawn process cmd=%s pid=%s"):format(cmd, pid)) callback(false) return nil, nil end - Log.debug("Spawned with pid", pid) + log.debug("Spawned with pid", pid) stdout:read_start(connect_sink(stdout, opts.stdio_sink.stdout)) stderr:read_start(connect_sink(stderr, opts.stdio_sink.stderr)) diff --git a/lua/nvim-lsp-installer/settings.lua b/lua/nvim-lsp-installer/settings.lua index ec7f622b..eb01c78c 100644 --- a/lua/nvim-lsp-installer/settings.lua +++ b/lua/nvim-lsp-installer/settings.lua @@ -10,18 +10,14 @@ local DEFAULT_SETTINGS = { }, }, - -- Controls to which degree logs are written to the log file. For example, it's useful to set this to - -- vim.log.levels.TRACE when debugging issues with server installations. + -- Controls to which degree logs are written to the log file. It's useful to set this to vim.log.levels.DEBUG when + -- debugging issues with server installations. log_level = vim.log.levels.WARN, - -- Whether to allow LSP servers to share the same installation directory. - -- For some servers, this effectively causes more than one server to be - -- installed (and uninstalled) when executing `:LspInstall` and - -- `:LspUninstall`. - - -- For example, installing `cssls` will also install both `jsonls` and `html` - -- (and the other ways around), as these all share the same underlying - -- package. + -- Whether to allow LSP servers to share the same installation directory. For some servers, this effectively causes + -- more than one server to be installed (and uninstalled) when executing `:LspInstall` and `:LspUninstall`. For + -- example, installing `cssls` will also install both `jsonls` and `html` (and the other ways around), as these all + -- share the same underlying package. allow_federated_servers = true, } diff --git a/lua/nvim-lsp-installer/ui/display.lua b/lua/nvim-lsp-installer/ui/display.lua index 3e0fa775..27aca167 100644 --- a/lua/nvim-lsp-installer/ui/display.lua +++ b/lua/nvim-lsp-installer/ui/display.lua @@ -199,8 +199,8 @@ function M.new_view_only_win(name) local draw = process.debounced(function(view) local win_valid = win_id ~= nil and vim.api.nvim_win_is_valid(win_id) local buf_valid = bufnr ~= nil and vim.api.nvim_buf_is_valid(bufnr) - log.fmt_debug("got bufnr=%s", bufnr) - log.fmt_debug("got win_id=%s", win_id) + log.fmt_trace("got bufnr=%s", bufnr) + log.fmt_trace("got win_id=%s", win_id) if not win_valid or not buf_valid then -- the window has been closed or the buffer is somehow no longer valid diff --git a/lua/nvim-lsp-installer/ui/status-win/init.lua b/lua/nvim-lsp-installer/ui/status-win/init.lua index 7ac5da64..57b215e4 100644 --- a/lua/nvim-lsp-installer/ui/status-win/init.lua +++ b/lua/nvim-lsp-installer/ui/status-win/init.lua @@ -1,7 +1,7 @@ local Ui = require "nvim-lsp-installer.ui" local fs = require "nvim-lsp-installer.fs" local settings = require "nvim-lsp-installer.settings" -local Log = require "nvim-lsp-installer.log" +local log = require "nvim-lsp-installer.log" local Data = require "nvim-lsp-installer.data" local display = require "nvim-lsp-installer.ui.display" @@ -275,7 +275,7 @@ local function init(all_servers) state.servers[server.name].installer.is_running = true end) - Log.debug("Starting install", server.name, requested_version) + log.debug("Starting install", server.name, requested_version) server:install_attached({ requested_server_version = requested_version, @@ -302,7 +302,7 @@ local function init(all_servers) }, function(success) mutate_state(function(state) -- can we log each line separately? - Log.debug("Installer output", server.name, state.servers[server.name].installer.tailed_output) + log.debug("Installer output", server.name, state.servers[server.name].installer.tailed_output) if success then -- release stdout/err output table.. hopefully ¯\_(ツ)_/¯ state.servers[server.name].installer.tailed_output = {} @@ -343,10 +343,10 @@ local function init(all_servers) return { open = open, install_server = function(server, version) - Log.debug("Installing server", server, version) + log.debug("Installing server", server, version) local server_state = get_state().servers[server.name] if server_state and (server_state.installer.is_running or server_state.installer.is_queued) then - Log.debug("Installer is already queued/running", server.name) + log.debug("Installer is already queued/running", server.name) return end mutate_state(function(state) @@ -359,7 +359,7 @@ local function init(all_servers) uninstall_server = function(server) local server_state = get_state().servers[server.name] if server_state and (server_state.installer.is_running or server_state.installer.is_queued) then - Log.debug("Installer is already queued/running", server.name) + log.debug("Installer is already queued/running", server.name) return end -- cgit v1.2.3-70-g09d2