aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2021-12-13 13:24:01 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2021-12-13 13:24:01 +0100
commit14b2a777085b60f2b0fba4cfc8407d9228758f44 (patch)
tree4b2eb70f83292184ef89333d6a07e7371fd78534
parent45da026654b7efd744345e3ab7b769b3a8d4dbc4 (diff)
downloadvis-lspc-14b2a777085b60f2b0fba4cfc8407d9228758f44.tar.gz
vis-lspc-14b2a777085b60f2b0fba4cfc8407d9228758f44.zip
improve default logging
I use to have logging enabled to be able to debug weird vis-lspc behavior right away. But I got annoyed by vis-lspc cluttering all my directories with vis-lspc.log files. So vis-lspc will no write its log files to $XDG_DATA_HOME/vis-lspc/<iso-date>-<cwd-basename>.log if lspc.log_file is not set by the user. This patch also introduces a helper to run a program and capture its output.
-rw-r--r--README.md3
-rw-r--r--init.lua47
2 files changed, 46 insertions, 4 deletions
diff --git a/README.md b/README.md
index ed990ff..a882907 100644
--- a/README.md
+++ b/README.md
@@ -104,7 +104,8 @@ Available fields are:
* `name = 'vis-lspc'` - the name vis-lspc introduces itself to a language server
* `logging = false` - enable logging only useful for debugging vis-lspc
-* `log_file = 'vis-lspc.log'` - file vis-lspc writes all log messages to
+* `log_file = nil` - nil, filename or function returning a filename
+ * If `log_file` is `nil` vis-lspc will create a new file in `$XDG_DATA_HOME/vis-lspc`
* `autostart = true` - try to start a language server in WIN_OPEN
* `menu_cmd = 'fzf' or 'vis-menu'` - program to prompt for user choices
* `confirm_cmd = 'vis-menu'` - program to prompt for user confirmation
diff --git a/init.lua b/init.lua
index 0521a6c..12e6cf6 100644
--- a/init.lua
+++ b/init.lua
@@ -51,7 +51,7 @@ local lspc = {
version = '0.1.3',
-- write log messages to lspc.log_file
logging = false,
- log_file = 'vis-lspc.log',
+ log_file = nil,
-- automatically start a language server when a new window is opened
autostart = true,
-- program used to let the user make choices
@@ -69,6 +69,25 @@ local lspc = {
diagnostic_style = 'back:#e3514f',
}
+-- Forward declaration of lspc_err to use it in capture_cmd
+local lspc_err
+
+-- Helper to execute a command and capture its output
+local function capture_cmd(cmd)
+ local p = assert(io.popen(cmd, 'r'))
+ local s = assert(p:read('*a'))
+ local success, _, status = p:close()
+ if not success then
+ local err = cmd .. ' failed with exit code: ' .. status
+ if lspc_err then
+ lspc_err(err)
+ else
+ vis:info('LSPC Error: ' .. err)
+ end
+ end
+ return s
+end
+
-- logging system
-- if lspc.logging is set to true the first call to lspc.log
-- will open the log file and replace lspc.log with the actual log function
@@ -86,7 +105,29 @@ do
return
end
- log_fd = assert(io.open(lspc.log_file, 'w'))
+ local log_file = lspc.log_file
+
+ -- open the default log file in $XDG_DATA_HOME/vis-lspc
+ if not log_file then
+ local xdg_data = os.getenv('XDG_DATA_HOME') or os.getenv('HOME') ..
+ '/.local/share'
+ local log_dir = xdg_data .. '/vis-lspc'
+
+ -- ensure the direcoty exists
+ os.execute('mkdir -p ' .. log_dir)
+
+ -- log file format: {time-stamp}-{basename-cwd}.log
+ local log_file_fmt = log_dir .. '/%s-%s.log'
+ local timestamp = os.date('%Y-%m-%dT%H:%M:%S')
+ local cwd = capture_cmd('pwd')
+ local basename_cwd = capture_cmd('basename ' .. cwd):match('^%s*(.-)%s*$')
+ log_file = log_file_fmt:format(timestamp, basename_cwd)
+
+ elseif type(log_file) == 'function' then
+ log_file = log_file()
+ end
+
+ log_fd = assert(io.open(log_file, 'w'))
lspc.log = log
log(msg)
@@ -100,7 +141,7 @@ local function lspc_warn(msg)
vis:info(warning)
end
-local function lspc_err(msg)
+lspc_err = function(msg)
local err = 'LSPC Error: ' .. msg
lspc.log(err)
vis:info(err)