diff options
| author | kylo252 <[email protected]> | 2021-08-09 19:02:37 +0200 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2021-08-09 19:02:37 +0200 | 
| commit | 405423108fc31981c40116a827e845a1179c9053 (patch) | |
| tree | 41a7cb23536c982ccdc3402ab9d4602f2538eb40 /lua/core | |
| parent | 625df947dcacf3804f4ec7335478535ecd8219af (diff) | |
feat: Add an async logger using plenary (#1207)
Co-authored-by: rebuilt <[email protected]>
Diffstat (limited to 'lua/core')
| -rw-r--r-- | lua/core/autopairs.lua | 2 | ||||
| -rw-r--r-- | lua/core/compe.lua | 2 | ||||
| -rw-r--r-- | lua/core/dap.lua | 2 | ||||
| -rw-r--r-- | lua/core/galaxyline.lua | 2 | ||||
| -rw-r--r-- | lua/core/gitsigns.lua | 2 | ||||
| -rw-r--r-- | lua/core/log.lua | 29 | ||||
| -rw-r--r-- | lua/core/nvimtree.lua | 2 | ||||
| -rw-r--r-- | lua/core/telescope.lua | 2 | ||||
| -rw-r--r-- | lua/core/terminal.lua | 48 | ||||
| -rw-r--r-- | lua/core/treesitter.lua | 2 | ||||
| -rw-r--r-- | lua/core/which-key.lua | 20 | 
11 files changed, 109 insertions, 4 deletions
| diff --git a/lua/core/autopairs.lua b/lua/core/autopairs.lua index f989864f..a5f21a1b 100644 --- a/lua/core/autopairs.lua +++ b/lua/core/autopairs.lua @@ -1,8 +1,10 @@  -- if not package.loaded['nvim-autopairs'] then  --   return  -- end +local Log = require "core.log"  local status_ok, _ = pcall(require, "nvim-autopairs")  if not status_ok then +  Log:get_default().error "Failed to load autopairs"    return  end  local npairs = require "nvim-autopairs" diff --git a/lua/core/compe.lua b/lua/core/compe.lua index 742fd07a..c2f97e27 100644 --- a/lua/core/compe.lua +++ b/lua/core/compe.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  M.config = function()    lvim.builtin.compe = {      enabled = true, @@ -62,6 +63,7 @@ M.setup = function()    local status_ok, compe = pcall(require, "compe")    if not status_ok then +    Log:get_default().error "Failed to load compe"      return    end diff --git a/lua/core/dap.lua b/lua/core/dap.lua index f2ed5795..4e21cc4c 100644 --- a/lua/core/dap.lua +++ b/lua/core/dap.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  M.config = function()    lvim.builtin.dap = {      active = false, @@ -14,6 +15,7 @@ end  M.setup = function()    local status_ok, dap = pcall(require, "dap")    if not status_ok then +    Log:get_default().error "Failed to load dap"      return    end diff --git a/lua/core/galaxyline.lua b/lua/core/galaxyline.lua index d3f9342b..ee0a317d 100644 --- a/lua/core/galaxyline.lua +++ b/lua/core/galaxyline.lua @@ -1,8 +1,10 @@  -- if not package.loaded['galaxyline'] then  --   return  -- end +local Log = require "core.log"  local status_ok, gl = pcall(require, "galaxyline")  if not status_ok then +  Log:get_default().error "Failed to load galaxyline"    return  end diff --git a/lua/core/gitsigns.lua b/lua/core/gitsigns.lua index f2c98f7c..9e023762 100644 --- a/lua/core/gitsigns.lua +++ b/lua/core/gitsigns.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  M.config = function()    lvim.builtin.gitsigns = {      signs = { @@ -50,6 +51,7 @@ end  M.setup = function()    local status_ok, gitsigns = pcall(require, "gitsigns")    if not status_ok then +    Log:get_default().error "Failed to load gitsigns"      return    end    gitsigns.setup(lvim.builtin.gitsigns) diff --git a/lua/core/log.lua b/lua/core/log.lua new file mode 100644 index 00000000..5dd5622e --- /dev/null +++ b/lua/core/log.lua @@ -0,0 +1,29 @@ +local Log = {} + +--- Creates a log handle based on Plenary.log +---@param opts these are passed verbatim to Plenary.log +---@return log handle +function Log:new(opts) +  local status_ok, _ = pcall(require, "plenary.log") +  if not status_ok then +    return nil +  end + +  local obj = require("plenary.log").new(opts) +  local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin) + +  obj.get_path = function() +    return path +  end + +  return obj +end + +--- Creates or retrieves a log handle for the default logfile +--- based on Plenary.log +---@return log handle +function Log:get_default() +  return Log:new { plugin = "lunarvim", level = lvim.log.level } +end + +return Log diff --git a/lua/core/nvimtree.lua b/lua/core/nvimtree.lua index 1a0de0b8..4d15b1b5 100644 --- a/lua/core/nvimtree.lua +++ b/lua/core/nvimtree.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  --  M.config = function()    lvim.builtin.nvimtree = { @@ -49,6 +50,7 @@ end  M.setup = function()    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"      return    end    local g = vim.g diff --git a/lua/core/telescope.lua b/lua/core/telescope.lua index 37d59982..f4d154b0 100644 --- a/lua/core/telescope.lua +++ b/lua/core/telescope.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  M.config = function()    local status_ok, actions = pcall(require, "telescope.actions")    if not status_ok then @@ -79,6 +80,7 @@ end  M.setup = function()    local status_ok, telescope = pcall(require, "telescope")    if not status_ok then +    Log:get_default().error "Failed to load telescope"      return    end    telescope.setup(lvim.builtin.telescope) diff --git a/lua/core/terminal.lua b/lua/core/terminal.lua index bd7815aa..818038fd 100644 --- a/lua/core/terminal.lua +++ b/lua/core/terminal.lua @@ -1,8 +1,11 @@  local M = {} +local Log = require "core.log" +local utils = require "utils" +  M.config = function()    lvim.builtin["terminal"] = {      -- size can be a number or function which is passed the current terminal -    size = 5, +    size = 20,      -- open_mapping = [[<c-\>]],      open_mapping = [[<c-t>]],      hide_numbers = true, -- hide the number column in toggleterm buffers @@ -11,7 +14,7 @@ M.config = function()      shading_factor = 2, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light      start_in_insert = true,      insert_mappings = true, -- whether or not the open mapping applies in insert mode -    persist_size = true, +    persist_size = false,      -- direction = 'vertical' | 'horizontal' | 'window' | 'float',      direction = "float",      close_on_exit = true, -- close the terminal window when the process exits @@ -36,13 +39,16 @@ M.config = function()      -- { exec, keymap, name}      -- lvim.builtin.terminal.execs = {{}} to overwrite      -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"} -    execs = { { "lazygit", "gg", "LazyGit" } }, +    execs = { +      { "lazygit", "gg", "LazyGit" }, +    },    }  end  M.setup = function()    local status_ok, terminal = pcall(require, "toggleterm")    if not status_ok then +    Log:get_default().error "Failed to load toggleterm"      print(terminal)      return    end @@ -88,4 +94,40 @@ M._exec_toggle = function(exec)    exec_term:toggle()  end +local function get_log_path(name) +  --handle custom paths not managed by Plenary.log +  local logger = require "core.log" +  local file +  if name == "nvim" then +    file = CACHE_PATH .. "/log" +  else +    file = logger:new({ plugin = name }):get_path() +  end +  if utils.is_file(file) then +    return file +  end +end + +---Toggles a log viewer according to log.viewer.layout_config +---@param name can be the name of any of the managed logs, e,g. "lunarvim" or the default ones {"nvim", "lsp", "packer.nvim"} +M.toggle_log_view = function(name) +  local logfile = get_log_path(name) +  if not logfile then +    return +  end +  local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, { +    cmd = lvim.log.viewer.cmd .. " " .. logfile, +    open_mapping = lvim.log.viewer.layout_config.open_mapping, +    direction = lvim.log.viewer.layout_config.direction, +    -- TODO: this might not be working as expected +    size = lvim.log.viewer.layout_config.size, +    float_opts = lvim.log.viewer.layout_config.float_opts, +  }) + +  local Terminal = require("toggleterm.terminal").Terminal +  local log_view = Terminal:new(term_opts) +  -- require("core.log"):get_default().debug("term", vim.inspect(term_opts)) +  log_view:toggle() +end +  return M diff --git a/lua/core/treesitter.lua b/lua/core/treesitter.lua index cfc58bb7..0a8a2ff2 100644 --- a/lua/core/treesitter.lua +++ b/lua/core/treesitter.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  M.config = function()    lvim.builtin.treesitter = {      ensure_installed = {}, -- one of "all", "maintained" (parsers with maintainers), or a list of languages @@ -64,6 +65,7 @@ end  M.setup = function()    local status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")    if not status_ok then +    Log:get_default().error "Failed to load nvim-treesitter.configs"      return    end diff --git a/lua/core/which-key.lua b/lua/core/which-key.lua index 268243e4..90bf1ace 100644 --- a/lua/core/which-key.lua +++ b/lua/core/which-key.lua @@ -1,4 +1,5 @@  local M = {} +local Log = require "core.log"  M.config = function()    lvim.builtin.which_key = {      active = false, @@ -173,8 +174,24 @@ M.config = function()            "<cmd>lua require('core.info').toggle_popup(vim.bo.filetype)<cr>",            "Toggle LunarVim Info",          }, +        l = { +          name = "+logs", +          d = { +            "<cmd>lua require('core.terminal').toggle_log_view('lunarvim')<cr>", +            "view default log", +          }, +          D = { "<cmd>edit ~/.cache/nvim/lunarvim.log<cr>", "Open the default logfile" }, +          n = { "<cmd>lua require('core.terminal').toggle_log_view('lsp')<cr>", "view lsp log" }, +          N = { "<cmd>edit ~/.cache/nvim/log<cr>", "Open the Neovim logfile" }, +          l = { "<cmd>lua require('core.terminal').toggle_log_view('nvim')<cr>", "view neovim log" }, +          L = { "<cmd>edit ~/.cache/nvim/lsp.log<cr>", "Open the LSP logfile" }, +          p = { +            "<cmd>lua require('core.terminal').toggle_log_view('packer.nvim')<cr>", +            "view packer log", +          }, +          P = { "<cmd>edit ~/.cache/nvim/packer.nvim.log<cr>", "Open the Packer logfile" }, +        },        }, -        s = {          name = "Search",          b = { "<cmd>Telescope git_branches<cr>", "Checkout branch" }, @@ -206,6 +223,7 @@ M.setup = function()    -- end    local status_ok, which_key = pcall(require, "which-key")    if not status_ok then +    Log:get_default "Failed to load whichkey"      return    end | 
