1
0
mirror of https://github.com/jiriks74/presence.nvim synced 2024-11-27 22:29:01 +01:00
presence.nvim/lua/lib/log.lua

39 lines
991 B
Lua
Raw Normal View History

2021-01-03 01:08:17 -08:00
local Log = {}
Log.codes = {}
Log.levels = {
{ "debug", "Comment" },
{ "info", "None" },
{ "warn", "WarningMsg" },
{ "error", "ErrorMsg" },
}
function Log:init(options)
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
local level, hl = unpack(Log.levels[i])
Log.codes[level] = i
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
2021-01-03 01:08:17 -08:00
return
end
vim.schedule(function()
2021-04-28 21:42:22 -07:00
local escaped_message = vim.fn.escape(message, '"'):gsub("\n", "\\n")
2021-01-03 01:08:17 -08:00
vim.cmd(string.format("echohl %s", hl))
2021-04-28 21:42:22 -07:00
vim.cmd(string.format([[echom "[%s] %s"]], "presence.nvim", escaped_message))
2021-01-03 01:08:17 -08:00
vim.cmd("echohl NONE")
end)
end
end
return Log