diff options
author | ii14 <59243201+ii14@users.noreply.github.com> | 2022-05-03 16:49:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 08:49:23 -0600 |
commit | 70e2c5d10d2574b77c56d0bebeb61527876ff0b1 (patch) | |
tree | 06183b3c49b204ff7d63efb72f7e0ba2da28a3f5 | |
parent | 73741e94867a8dedabcbd356e1e929f198c51905 (diff) | |
download | rneovim-70e2c5d10d2574b77c56d0bebeb61527876ff0b1.tar.gz rneovim-70e2c5d10d2574b77c56d0bebeb61527876ff0b1.tar.bz2 rneovim-70e2c5d10d2574b77c56d0bebeb61527876ff0b1.zip |
feat(lsp): add logging level "OFF" (#18379)
-rw-r--r-- | runtime/doc/lsp.txt | 4 | ||||
-rw-r--r-- | runtime/doc/lua.txt | 1 | ||||
-rw-r--r-- | runtime/lua/vim/_editor.lua | 1 | ||||
-rw-r--r-- | runtime/lua/vim/lsp.lua | 5 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/log.lua | 81 |
5 files changed, 60 insertions, 32 deletions
diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index f55c959a03..71ff0b2be5 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -750,7 +750,9 @@ omnifunc({findstart}, {base}) *vim.lsp.omnifunc()* set_log_level({level}) *vim.lsp.set_log_level()* Sets the global log level for LSP logging. - Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" + Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", + "OFF" + Level numbers begin with "TRACE" at 0 Use `lsp.log_levels` for reverse lookup. diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 9ae45ab7fb..534a40ff4f 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -1012,6 +1012,7 @@ Log levels are one of the values defined in `vim.log.levels`: vim.log.levels.INFO vim.log.levels.TRACE vim.log.levels.WARN + vim.log.levels.OFF ------------------------------------------------------------------------------ LUA-VIMSCRIPT BRIDGE *lua-vimscript* diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 62a7b3df13..119467de16 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -58,6 +58,7 @@ vim.log = { INFO = 2; WARN = 3; ERROR = 4; + OFF = 5; } } diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 19ee75a1b6..00f1c26692 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -1790,13 +1790,14 @@ end -- -- Can be used to lookup the number from the name or the -- name from the number. --- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" +-- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" -- Level numbers begin with "TRACE" at 0 lsp.log_levels = log.levels --- Sets the global log level for LSP logging. --- ---- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" +--- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" +--- --- Level numbers begin with "TRACE" at 0 --- --- Use `lsp.log_levels` for reverse lookup. diff --git a/runtime/lua/vim/lsp/log.lua b/runtime/lua/vim/lsp/log.lua index e0b5653587..fff42fd011 100644 --- a/runtime/lua/vim/lsp/log.lua +++ b/runtime/lua/vim/lsp/log.lua @@ -8,7 +8,7 @@ local log = {} -- Log level dictionary with reverse lookup as well. -- -- Can be used to lookup the number from the name or the name from the number. --- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" +-- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" -- Level numbers begin with "TRACE" at 0 log.levels = vim.deepcopy(vim.log.levels) @@ -25,27 +25,47 @@ do end local logfilename = path_join(vim.fn.stdpath('cache'), 'lsp.log') + -- TODO: Ideally the directory should be created in open_logfile(), right + -- before opening the log file, but open_logfile() can be called from libuv + -- callbacks, where using fn.mkdir() is not allowed. + vim.fn.mkdir(vim.fn.stdpath('cache'), "p") + --- Returns the log filename. ---@returns (string) log filename function log.get_filename() return logfilename end - vim.fn.mkdir(vim.fn.stdpath('cache'), "p") - local logfile = assert(io.open(logfilename, "a+")) - - local log_info = vim.loop.fs_stat(logfilename) - if log_info and log_info.size > 1e9 then - local warn_msg = string.format( - "LSP client log is large (%d MB): %s", - log_info.size / (1000 * 1000), - logfilename - ) - vim.notify(warn_msg) + local logfile, openerr + ---@private + --- Opens log file. Returns true if file is open, false on error + local function open_logfile() + -- Try to open file only once + if logfile then return true end + if openerr then return false end + + logfile, openerr = io.open(logfilename, "a+") + if not logfile then + local err_msg = string.format("Failed to open LSP client log file: %s", openerr) + vim.notify(err_msg, vim.log.levels.ERROR) + return false + end + + local log_info = vim.loop.fs_stat(logfilename) + if log_info and log_info.size > 1e9 then + local warn_msg = string.format( + "LSP client log is large (%d MB): %s", + log_info.size / (1000 * 1000), + logfilename + ) + vim.notify(warn_msg) + end + + -- Start message for logging + logfile:write(string.format("[START][%s] LSP logging initiated\n", os.date(log_date_format))) + return true end - -- Start message for logging - logfile:write(string.format("[START][%s] LSP logging initiated\n", os.date(log_date_format))) for level, levelnr in pairs(log.levels) do -- Also export the log level on the root object. log[level] = levelnr @@ -63,23 +83,26 @@ do -- ``` -- -- This way you can avoid string allocations if the log level isn't high enough. - log[level:lower()] = function(...) - local argc = select("#", ...) - if levelnr < current_log_level then return false end - if argc == 0 then return true end - local info = debug.getinfo(2, "Sl") - local header = string.format("[%s][%s] ...%s:%s", level, os.date(log_date_format), string.sub(info.short_src, #info.short_src - 15), info.currentline) - local parts = { header } - for i = 1, argc do - local arg = select(i, ...) - if arg == nil then - table.insert(parts, "nil") - else - table.insert(parts, format_func(arg)) + if level ~= "OFF" then + log[level:lower()] = function(...) + local argc = select("#", ...) + if levelnr < current_log_level then return false end + if argc == 0 then return true end + if not open_logfile() then return false end + local info = debug.getinfo(2, "Sl") + local header = string.format("[%s][%s] ...%s:%s", level, os.date(log_date_format), string.sub(info.short_src, #info.short_src - 15), info.currentline) + local parts = { header } + for i = 1, argc do + local arg = select(i, ...) + if arg == nil then + table.insert(parts, "nil") + else + table.insert(parts, format_func(arg)) + end end + logfile:write(table.concat(parts, '\t'), "\n") + logfile:flush() end - logfile:write(table.concat(parts, '\t'), "\n") - logfile:flush() end end end |