fix logging when plenary is not available (#1390)

This commit is contained in:
kylo252 2021-08-26 12:49:29 +02:00 committed by GitHub
parent cfefddde9e
commit 5b94e3cee2
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 61 additions and 35 deletions

View file

@ -24,6 +24,10 @@ config:load()
local plugins = require "plugins" local plugins = require "plugins"
local plugin_loader = require("plugin-loader").init() local plugin_loader = require("plugin-loader").init()
plugin_loader:load { plugins, lvim.plugins } plugin_loader:load { plugins, lvim.plugins }
local Log = require("core.log").new_default()
Log:info "Starting LunarVim"
vim.g.colors_name = lvim.colorscheme -- Colorscheme must get called after plugins are loaded or it will break new installs. vim.g.colors_name = lvim.colorscheme -- Colorscheme must get called after plugins are loaded or it will break new installs.
vim.cmd("colorscheme " .. lvim.colorscheme) vim.cmd("colorscheme " .. lvim.colorscheme)

View file

@ -4,26 +4,57 @@ local Log = {}
---@param opts these are passed verbatim to Plenary.log ---@param opts these are passed verbatim to Plenary.log
---@return log handle ---@return log handle
function Log:new(opts) function Log:new(opts)
local status_ok, _ = pcall(require, "plenary.log") local status_ok, handle = pcall(require, "plenary.log")
if not status_ok then if not status_ok then
return nil vim.notify("Plenary.log is not available. Logging to console only", vim.log.levels.DEBUG)
end end
local obj = require("plenary.log").new(opts) self.__handle = handle
local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin) local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin)
obj.get_path = function() self.get_path = function()
return path return path
end end
return obj setmetatable({}, Log)
return self
end
function Log:add_entry(msg, level)
local status_ok, _ = pcall(require, "plenary.log")
if not status_ok then
return vim.notify(msg, vim.log.levels[level])
end
-- plenary uses lower-case log levels
return self.__handle[level:lower()](msg)
end end
--- Creates or retrieves a log handle for the default logfile --- Creates or retrieves a log handle for the default logfile
--- based on Plenary.log --- based on Plenary.log
---@return log handle ---@return log handle
function Log:get_default() function Log:new_default()
return Log:new { plugin = "lunarvim", level = lvim.log.level } return Log:new { plugin = "lunarvim", level = lvim.log.level }
end end
function Log:trace(msg)
self:add_entry(msg, "TRACE")
end
function Log:debug(msg)
self:add_entry(msg, "DEBUG")
end
function Log:info(msg)
self:add_entry(msg, "INFO")
end
function Log:warn(msg)
self:add_entry(msg, "TRACE")
end
function Log:error(msg)
self:add_entry(msg, "TRACE")
end
return Log return Log

View file

@ -112,14 +112,13 @@ function M.get_style(style)
local style_keys = vim.tbl_keys(styles) local style_keys = vim.tbl_keys(styles)
if not vim.tbl_contains(style_keys, style) then if not vim.tbl_contains(style_keys, style) then
local Log = require "core.log" local Log = require "core.log"
local logger = Log:get_default() Log:error(
logger.error(
"Invalid lualine style", "Invalid lualine style",
string.format('"%s"', style), string.format('"%s"', style),
"options are: ", "options are: ",
string.format('"%s"', table.concat(style_keys, '", "')) string.format('"%s"', table.concat(style_keys, '", "'))
) )
logger.info '"lvim" style is applied.' Log:info '"lvim" style is applied.'
style = "lvim" style = "lvim"
end end

View file

@ -52,7 +52,7 @@ end
function M.setup() function M.setup()
local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config") local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
if not status_ok then if not status_ok then
Log:get_default().error "Failed to load nvim-tree.config" Log:error "Failed to load nvim-tree.config"
return return
end end
local g = vim.g local g = vim.g

View file

@ -82,7 +82,7 @@ M._exec_toggle = function(exec)
local binary = M._split(exec)[1] local binary = M._split(exec)[1]
if vim.fn.executable(binary) ~= 1 then if vim.fn.executable(binary) ~= 1 then
local Log = require "core.log" local Log = require "core.log"
Log:get_default().error("Unable to run executable " .. binary .. ". Please make sure it is installed properly.") Log:error("Unable to run executable " .. binary .. ". Please make sure it is installed properly.")
return return
end end
local Terminal = require("toggleterm.terminal").Terminal local Terminal = require("toggleterm.terminal").Terminal
@ -122,7 +122,7 @@ M.toggle_log_view = function(name)
local Terminal = require("toggleterm.terminal").Terminal local Terminal = require("toggleterm.terminal").Terminal
local log_view = Terminal:new(term_opts) local log_view = Terminal:new(term_opts)
-- require("core.log"):get_default().debug("term", vim.inspect(term_opts)) -- require("core.log"):debug("term", vim.inspect(term_opts))
log_view:toggle() log_view:toggle()
end end

View file

@ -159,9 +159,7 @@ function M.config()
lvim.keys.normal_mode["<A-Down>"] = lvim.keys.normal_mode["<C-Down>"] lvim.keys.normal_mode["<A-Down>"] = lvim.keys.normal_mode["<C-Down>"]
lvim.keys.normal_mode["<A-Left>"] = lvim.keys.normal_mode["<C-Left>"] lvim.keys.normal_mode["<A-Left>"] = lvim.keys.normal_mode["<C-Left>"]
lvim.keys.normal_mode["<A-Right>"] = lvim.keys.normal_mode["<C-Right>"] lvim.keys.normal_mode["<A-Right>"] = lvim.keys.normal_mode["<C-Right>"]
if Log:get_default() then Log:info "Activated mac keymappings"
Log:get_default().info "Activated mac keymappings"
end
end end
end end

View file

@ -99,16 +99,14 @@ end
function M.common_on_init(client, bufnr) function M.common_on_init(client, bufnr)
if lvim.lsp.on_init_callback then if lvim.lsp.on_init_callback then
lvim.lsp.on_init_callback(client, bufnr) lvim.lsp.on_init_callback(client, bufnr)
Log:get_default().info "Called lsp.on_init_callback" Log:info "Called lsp.on_init_callback"
return return
end end
local formatters = lvim.lang[vim.bo.filetype].formatters local formatters = lvim.lang[vim.bo.filetype].formatters
if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then
client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_formatting = false
Log:get_default().info( Log:info(string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe))
string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe)
)
end end
end end

View file

@ -3,7 +3,7 @@ local formatters_by_ft = {}
local null_ls = require "null-ls" local null_ls = require "null-ls"
local services = require "lsp.null-ls.services" local services = require "lsp.null-ls.services"
local logger = require("core.log"):get_default() local Log = require "core.log"
local function list_names(formatters, options) local function list_names(formatters, options)
options = options or {} options = options or {}
@ -45,15 +45,15 @@ function M.list_configured(formatter_configs)
local formatter = null_ls.builtins.formatting[fmt_config.exe] local formatter = null_ls.builtins.formatting[fmt_config.exe]
if not formatter then if not formatter then
logger.error("Not a valid formatter:", fmt_config.exe) Log:error("Not a valid formatter:", fmt_config.exe)
errors[fmt_config.exe] = {} -- Add data here when necessary errors[fmt_config.exe] = {} -- Add data here when necessary
else else
local formatter_cmd = services.find_command(formatter._opts.command) local formatter_cmd = services.find_command(formatter._opts.command)
if not formatter_cmd then if not formatter_cmd then
logger.warn("Not found:", formatter._opts.command) Log:warn("Not found:", formatter._opts.command)
errors[fmt_config.exe] = {} -- Add data here when necessary errors[fmt_config.exe] = {} -- Add data here when necessary
else else
logger.info("Using formatter:", formatter_cmd) Log:info("Using formatter:", formatter_cmd)
formatters[fmt_config.exe] = formatter.with { command = formatter_cmd, extra_args = fmt_config.args } formatters[fmt_config.exe] = formatter.with { command = formatter_cmd, extra_args = fmt_config.args }
end end
end end

View file

@ -30,7 +30,7 @@ function M.setup(filetype, options)
local ok, _ = pcall(require, "null-ls") local ok, _ = pcall(require, "null-ls")
if not ok then if not ok then
require("core.log"):get_default().error "Missing null-ls dependency" require("core.log"):error "Missing null-ls dependency"
return return
end end

View file

@ -3,7 +3,7 @@ local linters_by_ft = {}
local null_ls = require "null-ls" local null_ls = require "null-ls"
local services = require "lsp.null-ls.services" local services = require "lsp.null-ls.services"
local logger = require("core.log"):get_default() local Log = require "core.log"
local function list_names(linters, options) local function list_names(linters, options)
options = options or {} options = options or {}
@ -45,15 +45,15 @@ function M.list_configured(linter_configs)
local linter = null_ls.builtins.diagnostics[lnt_config.exe] local linter = null_ls.builtins.diagnostics[lnt_config.exe]
if not linter then if not linter then
logger.error("Not a valid linter:", lnt_config.exe) Log:error("Not a valid linter:", lnt_config.exe)
errors[lnt_config.exe] = {} -- Add data here when necessary errors[lnt_config.exe] = {} -- Add data here when necessary
else else
local linter_cmd = services.find_command(linter._opts.command) local linter_cmd = services.find_command(linter._opts.command)
if not linter_cmd then if not linter_cmd then
logger.warn("Not found:", linter._opts.command) Log:warn("Not found:", linter._opts.command)
errors[lnt_config.exe] = {} -- Add data here when necessary errors[lnt_config.exe] = {} -- Add data here when necessary
else else
logger.info("Using linter:", linter_cmd) Log:info("Using linter:", linter_cmd)
linters[lnt_config.exe] = linter.with { command = linter_cmd, extra_args = lnt_config.args } linters[lnt_config.exe] = linter.with { command = linter_cmd, extra_args = lnt_config.args }
end end
end end

View file

@ -70,9 +70,7 @@ function utils.toggle_autoformat()
}, },
}, },
} }
if Log:get_default() then Log:info "Format on save active"
Log:get_default().info "Format on save active"
end
end end
if not lvim.format_on_save then if not lvim.format_on_save then
@ -81,9 +79,7 @@ function utils.toggle_autoformat()
:autocmd! autoformat :autocmd! autoformat
endif endif
]] ]]
if Log:get_default() then Log:info "Format on save off"
Log:get_default().info "Format on save off"
end
end end
end end
@ -104,7 +100,7 @@ function utils.reload_lv_config()
-- vim.cmd ":PackerClean" -- vim.cmd ":PackerClean"
local null_ls = require "lsp.null-ls" local null_ls = require "lsp.null-ls"
null_ls.setup(vim.bo.filetype, { force_reload = true }) null_ls.setup(vim.bo.filetype, { force_reload = true })
Log:get_default().info "Reloaded configuration" Log:info "Reloaded configuration"
end end
function utils.unrequire(m) function utils.unrequire(m)