diff options
author | Gustaf Rydholm <gustaf.rydholm@gmail.com> | 2021-09-06 21:53:56 +0200 |
---|---|---|
committer | Gustaf Rydholm <gustaf.rydholm@gmail.com> | 2021-09-06 21:53:56 +0200 |
commit | 6a1732982287ef5aff2a6de171192b9e2bb90758 (patch) | |
tree | 543f94f80d548b9ed7a9d0daecb08575a01a9a45 /.config/nvim/lua/core/log.lua | |
parent | b89e893bfc7b8893dc4c13aa55f627096b32758a (diff) |
Updates to nvim from lvim
Diffstat (limited to '.config/nvim/lua/core/log.lua')
-rw-r--r-- | .config/nvim/lua/core/log.lua | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/.config/nvim/lua/core/log.lua b/.config/nvim/lua/core/log.lua index 35ba4be..bcc68e4 100644 --- a/.config/nvim/lua/core/log.lua +++ b/.config/nvim/lua/core/log.lua @@ -1,29 +1,59 @@ local Log = {} ---- Creates a log handle based on Plenary.log ----@param opts these are passed verbatim to Plenary.log ----@return log handle -function Log:new(opts) - local status_ok, _ = pcall(require, "plenary.log") - if not status_ok then - return nil +--- Adds a log entry using Plenary.log +---@param msg any +---@param level string [same as vim.log.log_levels] +function Log:add_entry(msg, level) + assert(type(level) == "string") + if self.__handle then + -- plenary uses lower-case log levels + self.__handle[level:lower()](msg) end + local status_ok, plenary = pcall(require, "plenary") + if status_ok then + local default_opts = { plugin = "nvim", level = options.log.level } + local handle = plenary.log.new(default_opts) + handle[level:lower()](msg) + self.__handle = handle + end + -- don't do anything if plenary is not available +end - local obj = require("plenary.log").new(opts) - local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin) +---Retrieves the path of the logfile +---@return string path of the logfile +function Log:get_path() + return string.format("%s/%s.log", vim.fn.stdpath "cache", "nvim") +end - obj.get_path = function() - return path - end +---Add a log entry at TRACE level +---@param msg any +function Log:trace(msg) + self:add_entry(msg, "TRACE") +end + +---Add a log entry at DEBUG level +---@param msg any +function Log:debug(msg) + self:add_entry(msg, "DEBUG") +end + +---Add a log entry at INFO level +---@param msg any +function Log:info(msg) + self:add_entry(msg, "INFO") +end - return obj +---Add a log entry at WARN level +---@param msg any +function Log:warn(msg) + self:add_entry(msg, "WARN") end ---- Creates or retrieves a log handle for the default logfile ---- based on Plenary.log ----@return log handle -function Log:get_default() - return Log:new { plugin = "lunarvim", level = options.log.level } +---Add a log entry at ERROR level +---@param msg any +function Log:error(msg) + self:add_entry(msg, "ERROR") end +setmetatable({}, Log) return Log |