diff options
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | init.lua | 47 |
2 files changed, 46 insertions, 4 deletions
@@ -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 @@ -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) |
