aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform/log.lua
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2023-08-25 11:15:12 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2023-08-25 11:43:47 -0700
commiteb5987e9dd40ce1e27c9c07e41d09571f1bd876e (patch)
treeb4cffe35e0893272cedc0ecf0229d08be343d70e /lua/conform/log.lua
parent100fd00d40423af85c4c7efcf875f8e4ee329f50 (diff)
feat: first working version
Diffstat (limited to 'lua/conform/log.lua')
-rw-r--r--lua/conform/log.lua108
1 files changed, 108 insertions, 0 deletions
diff --git a/lua/conform/log.lua b/lua/conform/log.lua
new file mode 100644
index 0000000..b42d1f8
--- /dev/null
+++ b/lua/conform/log.lua
@@ -0,0 +1,108 @@
+local uv = vim.uv or vim.loop
+local levels = vim.deepcopy(vim.log.levels)
+vim.tbl_add_reverse_lookup(levels)
+
+local Log = {}
+
+---@type integer
+Log.level = vim.log.levels.ERROR
+
+---@return string
+Log.get_logfile = function()
+ local fs = require("conform.fs")
+
+ local ok, stdpath = pcall(vim.fn.stdpath, "log")
+ if not ok then
+ stdpath = vim.fn.stdpath("cache")
+ end
+ return fs.join(stdpath, "conform.log")
+end
+
+---@param level integer
+---@param msg string
+---@param ... any[]
+---@return string
+local function format(level, msg, ...)
+ local args = vim.F.pack_len(...)
+ for i = 1, args.n do
+ local v = args[i]
+ if type(v) == "table" then
+ args[i] = vim.inspect(v)
+ elseif v == nil then
+ args[i] = "nil"
+ end
+ end
+ local ok, text = pcall(string.format, msg, vim.F.unpack_len(args))
+ if ok then
+ local str_level = levels[level]
+ return string.format("[%s] %s", str_level, text)
+ else
+ return string.format("[ERROR] error formatting log line: '%s' args %s", msg, vim.inspect(args))
+ end
+end
+
+---@param line string
+local function write(line)
+ -- This will be replaced during initialization
+end
+
+local initialized = false
+local function initialize()
+ if initialized then
+ return
+ end
+ initialized = true
+ local filepath = Log.get_logfile()
+
+ local stat = uv.fs_stat(filepath)
+ if stat and stat.size > 10 * 1024 * 1024 then
+ local backup = filepath .. ".1"
+ uv.fs_unlink(backup)
+ uv.fs_rename(filepath, backup)
+ end
+
+ local parent = vim.fs.dirname(filepath)
+ vim.fn.mkdir(parent, "p")
+
+ local logfile, openerr = io.open(filepath, "a+")
+ if not logfile then
+ local err_msg = string.format("Failed to open conform.nvim log file: %s", openerr)
+ vim.notify(err_msg, vim.log.levels.ERROR)
+ else
+ write = function(line)
+ logfile:write(line)
+ logfile:write("\n")
+ logfile:flush()
+ end
+ end
+end
+
+function Log.log(level, msg, ...)
+ if Log.level <= level then
+ initialize()
+ local text = format(level, msg, ...)
+ write(text)
+ end
+end
+
+function Log.trace(...)
+ Log.log(vim.log.levels.TRACE, ...)
+end
+
+function Log.debug(...)
+ Log.log(vim.log.levels.DEBUG, ...)
+end
+
+function Log.info(...)
+ Log.log(vim.log.levels.INFO, ...)
+end
+
+function Log.warn(...)
+ Log.log(vim.log.levels.WARN, ...)
+end
+
+function Log.error(...)
+ Log.log(vim.log.levels.ERROR, ...)
+end
+
+return Log