diff options
author | luaneko <luaneko@chiya.dev> | 2022-12-20 21:40:25 +1100 |
---|---|---|
committer | luaneko <luaneko@chiya.dev> | 2022-12-20 21:40:25 +1100 |
commit | 8d4a0df7d6b61e275932e42858b74f7c8a34c05a (patch) | |
tree | 2296e6a72407aafe52f1961f99168bbf68a5a896 | |
parent | d2b3ec29affa099b681ea21cbecb6718b4ac3134 (diff) | |
download | dep-8d4a0df7d6b61e275932e42858b74f7c8a34c05a.tar.gz dep-8d4a0df7d6b61e275932e42858b74f7c8a34c05a.tar.bz2 dep-8d4a0df7d6b61e275932e42858b74f7c8a34c05a.zip |
Refactor logger class
Diffstat (limited to '')
-rwxr-xr-x | lua/dep.lua | 15 | ||||
-rwxr-xr-x | lua/dep/log.lua | 137 | ||||
-rwxr-xr-x | lua/dep/proc.lua | 2 |
3 files changed, 98 insertions, 56 deletions
diff --git a/lua/dep.lua b/lua/dep.lua index 2ae3105..1c6d9a1 100755 --- a/lua/dep.lua +++ b/lua/dep.lua @@ -1,7 +1,14 @@ -local logger = require("dep/log") -local proc = require("dep/proc") - -logger:open() +-- +-- 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 +-- + +local logger = require("dep.log").global +local proc = require("dep.proc") local initialized, perf, config_path, base_dir local packages, root diff --git a/lua/dep/log.lua b/lua/dep/log.lua index 7ed32ee..d841953 100755 --- a/lua/dep/log.lua +++ b/lua/dep/log.lua @@ -1,63 +1,98 @@ -local logger = { - path = vim.fn.stdpath("cache") .. "/dep.log", - silent = false, -} +-- +-- 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 +-- +local vim, setmetatable, pcall, debug, string, os = vim, setmetatable, pcall, debug, string, os -local colors = { - skip = "Comment", - clean = "Boolean", - install = "MoreMsg", - update = "WarningMsg", - delete = "Directory", - error = "ErrorMsg", -} +local function try_format(...) + local ok, s = pcall(string.format, ...) + if ok then + return s + end +end -function logger:open(path) - self:close() +-- 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" - self.path = path or self.path - self.handle = vim.loop.fs_open(self.path, "w", 0x1A4) -- 0644 - self.pipe = vim.loop.new_pipe() + -- clear and open log file + local handle = vim.loop.fs_open(path, "w", 0x1b4) -- 0664 + local pipe = vim.loop.new_pipe() + pipe:open(handle) - self.pipe:open(self.handle) -end + return setmetatable({ + path = path, + handle = handle, + pipe = pipe, + silent = false, -function logger:close() - if self.pipe then - self.pipe:close() - self.pipe = nil - end + -- 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, - if self.handle then - vim.loop.fs_close(self.handle) - self.handle = nil - end -end + __index = { + -- Prints a message associated with a stage. + log = function(self, stage, message, ...) + -- calling function + local source = debug.getinfo(2, "Sl").short_src -function logger:log(op, message) - local source = debug.getinfo(2, "Sl").short_src + -- format or stringify message + if type(message) == "string" then + message = try_format(message, ...) or message + else + message = vim.inspect(message) + end - vim.schedule(function() - if type(message) ~= "string" then - message = vim.inspect(message) - end + -- 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 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, {}) + 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 - end - if self.pipe then - self.pipe:write(string.format("[%s] %s: %s\n", os.date(), source, message)) - end - end) -end + if self.handle then + vim.loop.fs_close(self.handle) + self.handle = nil + end + end, + }, +} -return logger +return { + Logger = Logger, + global = Logger:new(), +} diff --git a/lua/dep/proc.lua b/lua/dep/proc.lua index 376a98c..0082239 100755 --- a/lua/dep/proc.lua +++ b/lua/dep/proc.lua @@ -1,4 +1,4 @@ -local logger = require("dep/log") +local logger = require("dep.log").global local proc = {} function proc.exec(process, args, cwd, env, cb) |