2021-01-03 01:08:17 -08:00
|
|
|
local Log = {}
|
|
|
|
|
|
|
|
Log.codes = {}
|
|
|
|
Log.levels = {
|
2023-09-29 02:14:33 +02:00
|
|
|
{ "debug", "Comment" },
|
|
|
|
{ "info", "None" },
|
|
|
|
{ "warn", "WarningMsg" },
|
|
|
|
{ "error", "ErrorMsg" },
|
2021-01-03 01:08:17 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 16:44:00 -08:00
|
|
|
function Log:init(options)
|
2023-09-29 02:14:33 +02:00
|
|
|
self.level = options.level
|
|
|
|
return self
|
2021-01-03 01:08:17 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Initialize logger with log functions for each level
|
|
|
|
for i = 1, #Log.levels do
|
2023-09-29 02:14:33 +02:00
|
|
|
local level, hl = unpack(Log.levels[i])
|
2021-01-03 01:08:17 -08:00
|
|
|
|
2023-09-29 02:14:33 +02:00
|
|
|
Log.codes[level] = i
|
2021-01-03 01:08:17 -08:00
|
|
|
|
2023-09-29 02:14:33 +02:00
|
|
|
Log[level] = function(self, message)
|
|
|
|
-- Skip if log level is not set or the log is below the configured or default level
|
|
|
|
if not self.level or self.codes[level] < self.codes[self.level] or type(message) ~= "string" then
|
|
|
|
return
|
|
|
|
end
|
2021-01-03 01:08:17 -08:00
|
|
|
|
2023-09-29 02:14:33 +02:00
|
|
|
vim.schedule(function()
|
|
|
|
local escaped_message = vim.fn.escape(message, '"'):gsub("\n", "\\n")
|
2021-04-28 21:42:22 -07:00
|
|
|
|
2023-09-29 02:14:33 +02:00
|
|
|
vim.cmd(string.format("echohl %s", hl))
|
|
|
|
vim.cmd(string.format([[echom "[%s] %s"]], "presence.nvim", escaped_message))
|
|
|
|
vim.cmd("echohl NONE")
|
|
|
|
end)
|
|
|
|
end
|
2021-01-03 01:08:17 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
return Log
|