aboutsummaryrefslogtreecommitdiff
path: root/log.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-08-27 22:08:31 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2024-08-27 22:16:15 +0200
commit1bd1416c89f92f637ca5e22c94b3479f8d32f2d7 (patch)
tree7900c3570b07fc099a7310959fc31d16f806468b /log.lua
parent85a3fe068c572f6b3487c40cc3e962687c8998a7 (diff)
downloadvis-lspc-1bd1416c89f92f637ca5e22c94b3479f8d32f2d7.tar.gz
vis-lspc-1bd1416c89f92f637ca5e22c94b3479f8d32f2d7.zip
make the message handling functions methods of the lspc object
Move the logging code into its own module and export the default logger. Other modules can use the logger exported in the lspc table.
Diffstat (limited to 'log.lua')
-rw-r--r--log.lua64
1 files changed, 64 insertions, 0 deletions
diff --git a/log.lua b/log.lua
new file mode 100644
index 0000000..c1c36a7
--- /dev/null
+++ b/log.lua
@@ -0,0 +1,64 @@
+--- Simple logging module for vis-lspc.
+-- @module log
+-- @author Florian Fischer
+-- @license GPL-3
+-- @copyright 2024 Florian Fischer
+local log = {}
+
+--- Logger class metatable.
+local Logger = {}
+function Logger:log(msg)
+ self.log_fd:write(msg)
+ self.log_fd:write('\n')
+ self.log_fd:flush()
+end
+function Logger:close()
+ self.log_fd:close()
+end
+
+--- Dummy logger class metatable using NOP log functions.
+local DummyLogger = {}
+function DummyLogger:log()
+end
+function DummyLogger:close()
+end
+
+function log.new(name, logging, log_file)
+ local logger = {name = name, log_fd = nil}
+
+ if not logging then
+ setmetatable(logger, {__index = DummyLogger})
+ return logger
+ end
+
+ setmetatable(logger, {__index = Logger})
+
+ -- 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: {timestamp}-{basename-cwd}.log
+ local log_file_fmt = log_dir .. '/%s-%s.log'
+ local timestamp = os.date('%Y-%m-%dT%H:%M:%S')
+ local proc = assert(io.popen('basename "${PWD}"', 'r'))
+ local basename_cwd = assert(proc:read('*a')):match('^%s*(.-)%s*$')
+ local success, _, status = proc:close()
+ if not success then
+ local err = 'getting the basename of CWD failed with exit code: ' .. status
+ vis:info('LSPC Error: ' .. err)
+ end
+ log_file = log_file_fmt:format(timestamp, basename_cwd)
+
+ elseif type(log_file) == 'function' then
+ log_file = log_file()
+ end
+
+ logger.log_fd = assert(io.open(log_file, 'w'))
+ return logger
+end
+
+return log