aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-12-30 08:55:49 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2024-12-30 08:55:49 +0100
commit24e3bfa168250a99d5543b7c6604af9f11a4d2b6 (patch)
tree5876cc2bcdada9c9dfd57e4ce3413298529ca1b0
parentd71124b460ae338da69ae8dac694a18a639bb0fa (diff)
downloadvis-lspc-24e3bfa168250a99d5543b7c6604af9f11a4d2b6.tar.gz
vis-lspc-24e3bfa168250a99d5543b7c6604af9f11a4d2b6.zip
lazily initialize the default logger
In order for the user to configure the logging behavior the values of lspc.logging and lspc.log_file must be evaluated after vis-lspc was required. To lazily evaluate the logging configuration a new LazyInitLogger metatable is introduced, which initializes the actual logger on first use. Fixes: 1bd1416c89f92f637ca5e22c94b3479f8d32f2d7
-rw-r--r--init.lua2
-rw-r--r--log.lua29
2 files changed, 30 insertions, 1 deletions
diff --git a/init.lua b/init.lua
index 15d03b5..9906f29 100644
--- a/init.lua
+++ b/init.lua
@@ -27,7 +27,7 @@ local source_path = source_str:match('(.*/)')
local lspc = dofile(source_path .. 'lspc.lua')
-- initialise the logging system
-lspc.logger = dofile(source_path .. 'log.lua').new('lspc', lspc.logging, lspc.log_file)
+lspc.logger = dofile(source_path .. 'log.lua').lazyNew('lspc', lspc)
local parser = dofile(source_path .. 'parser.lua')
diff --git a/log.lua b/log.lua
index 789522d..a41b819 100644
--- a/log.lua
+++ b/log.lua
@@ -23,6 +23,35 @@ end
function DummyLogger.close()
end
+--- Logger that initialises itself on first use
+local LazyInitLogger = {}
+function LazyInitLogger:log(first_msg)
+ self.logger = log.new(self.name, self.conf.logging, self.conf.log_file)
+ self.log = function(lazyLogger, msg)
+ lazyLogger.logger:log(msg)
+ end
+ self.logger:log(first_msg)
+end
+function LazyInitLogger:close()
+ self.logger:close()
+end
+
+--- Create a new lazily initializing logger
+--
+-- The created logger will initialize itself using log.new on first use.
+-- The logging and log_file fields of the conf table are passed to log.new.
+-- @param name The name of the logger
+-- @param conf Table containing logging and log_file members
+function log.lazyNew(name, conf)
+ local logger = {name = name, conf = conf}
+ setmetatable(logger, {__index = LazyInitLogger})
+ return logger
+end
+
+--- Create a new named logger
+-- @param name The name of the logger
+-- @param logging Is logging enabled
+-- @param log_file File path to the log file to use
function log.new(name, logging, log_file)
local logger = {name = name, log_fd = nil}