Refactor logger class
This commit is contained in:
13
lua/dep.lua
13
lua/dep.lua
@ -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
|
||||||
|
141
lua/dep/log.lua
141
lua/dep/log.lua
@ -1,63 +1,98 @@
|
|||||||
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(...)
|
||||||
skip = "Comment",
|
local ok, s = pcall(string.format, ...)
|
||||||
clean = "Boolean",
|
if ok then
|
||||||
install = "MoreMsg",
|
return s
|
||||||
update = "WarningMsg",
|
|
||||||
delete = "Directory",
|
|
||||||
error = "ErrorMsg",
|
|
||||||
}
|
|
||||||
|
|
||||||
function logger:open(path)
|
|
||||||
self:close()
|
|
||||||
|
|
||||||
self.path = path or self.path
|
|
||||||
self.handle = vim.loop.fs_open(self.path, "w", 0x1A4) -- 0644
|
|
||||||
self.pipe = vim.loop.new_pipe()
|
|
||||||
|
|
||||||
self.pipe:open(self.handle)
|
|
||||||
end
|
|
||||||
|
|
||||||
function logger:close()
|
|
||||||
if self.pipe then
|
|
||||||
self.pipe:close()
|
|
||||||
self.pipe = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.handle then
|
|
||||||
vim.loop.fs_close(self.handle)
|
|
||||||
self.handle = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function logger:log(op, message)
|
-- Writes logs to a file and prints pretty status messages.
|
||||||
local source = debug.getinfo(2, "Sl").short_src
|
local Logger = {
|
||||||
|
-- Constructs a new `Logger`.
|
||||||
|
new = function(mt, path)
|
||||||
|
path = path or vim.fn.stdpath("cache") .. "/dep.log"
|
||||||
|
|
||||||
vim.schedule(function()
|
-- clear and open log file
|
||||||
if type(message) ~= "string" then
|
local handle = vim.loop.fs_open(path, "w", 0x1b4) -- 0664
|
||||||
message = vim.inspect(message)
|
local pipe = vim.loop.new_pipe()
|
||||||
end
|
pipe:open(handle)
|
||||||
|
|
||||||
if not self.silent and colors[op] then
|
return setmetatable({
|
||||||
if op == "error" then
|
path = path,
|
||||||
vim.api.nvim_err_writeln(string.format("[dep] %s", message))
|
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",
|
||||||
|
clean = "Boolean",
|
||||||
|
install = "MoreMsg",
|
||||||
|
update = "WarningMsg",
|
||||||
|
delete = "Directory",
|
||||||
|
error = "ErrorMsg",
|
||||||
|
},
|
||||||
|
}, mt)
|
||||||
|
end,
|
||||||
|
|
||||||
|
__index = {
|
||||||
|
-- Prints a message associated with a stage.
|
||||||
|
log = function(self, stage, message, ...)
|
||||||
|
-- calling function
|
||||||
|
local source = debug.getinfo(2, "Sl").short_src
|
||||||
|
|
||||||
|
-- format or stringify message
|
||||||
|
if type(message) == "string" then
|
||||||
|
message = try_format(message, ...) or message
|
||||||
else
|
else
|
||||||
vim.api.nvim_echo({
|
message = vim.inspect(message)
|
||||||
{ "[dep]", "Identifier" },
|
|
||||||
{ " " },
|
|
||||||
{ message, colors[op] },
|
|
||||||
}, true, {})
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if self.pipe then
|
-- print and write must be done on the main event loop
|
||||||
self.pipe:write(string.format("[%s] %s: %s\n", os.date(), source, message))
|
vim.schedule(function()
|
||||||
end
|
if not self.silent then
|
||||||
end)
|
if stage == "error" then
|
||||||
end
|
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
|
||||||
|
|
||||||
return logger
|
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
|
||||||
|
self.pipe:close()
|
||||||
|
self.pipe = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.handle then
|
||||||
|
vim.loop.fs_close(self.handle)
|
||||||
|
self.handle = nil
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
Logger = Logger,
|
||||||
|
global = Logger:new(),
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
Reference in New Issue
Block a user