Refactor logger class

This commit is contained in:
luaneko
2022-12-20 21:40:25 +11:00
parent d2b3ec29af
commit 8d4a0df7d6
3 changed files with 99 additions and 57 deletions

View File

@ -1,7 +1,14 @@
local logger = require("dep/log") --
local proc = require("dep/proc") -- Copyright (c) 2022 chiya.dev
--
-- Use of this source code is governed by the MIT License
-- which can be found in the LICENSE file and at:
--
-- https://opensource.org/licenses/MIT
--
logger:open() local logger = require("dep.log").global
local proc = require("dep.proc")
local initialized, perf, config_path, base_dir local initialized, perf, config_path, base_dir
local packages, root local packages, root

View File

@ -1,28 +1,84 @@
local logger = { --
path = vim.fn.stdpath("cache") .. "/dep.log", -- Copyright (c) 2022 chiya.dev
silent = false, --
} -- Use of this source code is governed by the MIT License
-- which can be found in the LICENSE file and at:
--
-- https://opensource.org/licenses/MIT
--
local vim, setmetatable, pcall, debug, string, os = vim, setmetatable, pcall, debug, string, os
local colors = { local function try_format(...)
local ok, s = pcall(string.format, ...)
if ok then
return s
end
end
-- Writes logs to a file and prints pretty status messages.
local Logger = {
-- Constructs a new `Logger`.
new = function(mt, path)
path = path or vim.fn.stdpath("cache") .. "/dep.log"
-- clear and open log file
local handle = vim.loop.fs_open(path, "w", 0x1b4) -- 0664
local pipe = vim.loop.new_pipe()
pipe:open(handle)
return setmetatable({
path = path,
handle = handle,
pipe = pipe,
silent = false,
-- TODO: This looks good for me ;) but it should have proper vim color mapping for other people.
stage_colors = {
skip = "Comment", skip = "Comment",
clean = "Boolean", clean = "Boolean",
install = "MoreMsg", install = "MoreMsg",
update = "WarningMsg", update = "WarningMsg",
delete = "Directory", delete = "Directory",
error = "ErrorMsg", error = "ErrorMsg",
} },
}, mt)
end,
function logger:open(path) __index = {
self:close() -- Prints a message associated with a stage.
log = function(self, stage, message, ...)
-- calling function
local source = debug.getinfo(2, "Sl").short_src
self.path = path or self.path -- format or stringify message
self.handle = vim.loop.fs_open(self.path, "w", 0x1A4) -- 0644 if type(message) == "string" then
self.pipe = vim.loop.new_pipe() message = try_format(message, ...) or message
else
self.pipe:open(self.handle) message = vim.inspect(message)
end end
function logger:close() -- print and write must be done on the main event loop
vim.schedule(function()
if not self.silent then
if stage == "error" then
vim.api.nvim_err_writeln(string.format("[dep] %s", message))
elseif self.stage_colors[stage] then
vim.api.nvim_echo({
{ "[dep]", "Identifier" },
{ " " },
{ message, self.stage_colors[stage] },
}, true, {})
end
end
if self.pipe then
self.pipe:write(string.format("[%s] %s: %s\n", os.date(), source, message))
end
end)
end,
-- Closes the log file handle.
close = function(self)
if self.pipe then if self.pipe then
self.pipe:close() self.pipe:close()
self.pipe = nil self.pipe = nil
@ -32,32 +88,11 @@ function logger:close()
vim.loop.fs_close(self.handle) vim.loop.fs_close(self.handle)
self.handle = nil self.handle = nil
end end
end end,
},
}
function logger:log(op, message) return {
local source = debug.getinfo(2, "Sl").short_src Logger = Logger,
global = Logger:new(),
vim.schedule(function() }
if type(message) ~= "string" then
message = vim.inspect(message)
end
if not self.silent and colors[op] then
if op == "error" then
vim.api.nvim_err_writeln(string.format("[dep] %s", message))
else
vim.api.nvim_echo({
{ "[dep]", "Identifier" },
{ " " },
{ message, colors[op] },
}, true, {})
end
end
if self.pipe then
self.pipe:write(string.format("[%s] %s: %s\n", os.date(), source, message))
end
end)
end
return logger

View File

@ -1,4 +1,4 @@
local logger = require("dep/log") local logger = require("dep.log").global
local proc = {} local proc = {}
function proc.exec(process, args, cwd, env, cb) function proc.exec(process, args, cwd, env, cb)