Prechádzať zdrojové kódy

fix logging when plenary is not available (#1390)

kylo252 3 rokov pred
rodič
commit
5b94e3cee2

+ 4 - 0
init.lua

@@ -24,6 +24,10 @@ config:load()
 local plugins = require "plugins"
 local plugin_loader = require("plugin-loader").init()
 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.cmd("colorscheme " .. lvim.colorscheme)
 

+ 37 - 6
lua/core/log.lua

@@ -4,26 +4,57 @@ local Log = {}
 ---@param opts these are passed verbatim to Plenary.log
 ---@return log handle
 function Log:new(opts)
-  local status_ok, _ = pcall(require, "plenary.log")
+  local status_ok, handle = pcall(require, "plenary.log")
   if not status_ok then
-    return nil
+    vim.notify("Plenary.log is not available. Logging to console only", vim.log.levels.DEBUG)
   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)
 
-  obj.get_path = function()
+  self.get_path = function()
     return path
   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
 
 --- Creates or retrieves a log handle for the default logfile
 --- based on Plenary.log
 ---@return log handle
-function Log:get_default()
+function Log:new_default()
   return Log:new { plugin = "lunarvim", level = lvim.log.level }
 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

+ 2 - 3
lua/core/lualine/styles.lua

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

+ 1 - 1
lua/core/nvimtree.lua

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

+ 2 - 2
lua/core/terminal.lua

@@ -82,7 +82,7 @@ M._exec_toggle = function(exec)
   local binary = M._split(exec)[1]
   if vim.fn.executable(binary) ~= 1 then
     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
   end
   local Terminal = require("toggleterm.terminal").Terminal
@@ -122,7 +122,7 @@ M.toggle_log_view = function(name)
 
   local Terminal = require("toggleterm.terminal").Terminal
   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()
 end
 

+ 1 - 3
lua/keymappings.lua

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

+ 2 - 4
lua/lsp/init.lua

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

+ 4 - 4
lua/lsp/null-ls/formatters.lua

@@ -3,7 +3,7 @@ local formatters_by_ft = {}
 
 local null_ls = require "null-ls"
 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)
   options = options or {}
@@ -45,15 +45,15 @@ function M.list_configured(formatter_configs)
     local formatter = null_ls.builtins.formatting[fmt_config.exe]
 
     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
     else
       local formatter_cmd = services.find_command(formatter._opts.command)
       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
       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 }
       end
     end

+ 1 - 1
lua/lsp/null-ls/init.lua

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

+ 4 - 4
lua/lsp/null-ls/linters.lua

@@ -3,7 +3,7 @@ local linters_by_ft = {}
 
 local null_ls = require "null-ls"
 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)
   options = options or {}
@@ -45,15 +45,15 @@ function M.list_configured(linter_configs)
     local linter = null_ls.builtins.diagnostics[lnt_config.exe]
 
     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
     else
       local linter_cmd = services.find_command(linter._opts.command)
       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
       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 }
       end
     end

+ 3 - 7
lua/utils/init.lua

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