From 925516a47ceb34a9fbeb75c361a602a667715f0c Mon Sep 17 00:00:00 2001 From: Leon Connor Holm Date: Mon, 21 Aug 2023 12:41:49 +0200 Subject: [PATCH] feat(#6): added log directory --- .luacheckrc | 1 + lua/qvim/bootstrap.lua | 32 +++++------ lua/qvim/core/manager.lua | 4 +- lua/qvim/log.lua | 102 ++++----------------------------- utils/bin/qvim.template | 1 + utils/ci/run_test.sh | 4 +- utils/installer/install.sh | 4 +- utils/installer/install_bin.sh | 3 +- 8 files changed, 38 insertions(+), 113 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 1874940..bef024b 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -23,6 +23,7 @@ stds.nvim = { "path_sep", "vim", "join_paths", + "get_qvim_log_dir", "get_qvim_cache_dir", "get_qvim_state_dir", "get_qvim_config_dir", diff --git a/lua/qvim/bootstrap.lua b/lua/qvim/bootstrap.lua index 50ea749..7f75570 100644 --- a/lua/qvim/bootstrap.lua +++ b/lua/qvim/bootstrap.lua @@ -43,6 +43,16 @@ function _G.get_qvim_config_dir() return qvim_config_dir end +---Get the full path to `$QUANTUMVIM_LOG_DIR` +---@return string +function _G.get_qvim_log_dir() + local qvim_log_dir = os.getenv("QUANTUMVIM_LOG_DIR") + if not qvim_log_dir then + return vim.call("stdpath", "log") + end + return qvim_log_dir +end + ---Get the full path to `$QUANTUMVIM_STATE_DIR` ---@return string function _G.get_qvim_state_dir() @@ -83,6 +93,8 @@ M.qvim_config_dir = get_qvim_config_dir() M.qvim_state_dir = get_qvim_state_dir() M.qvim_rtp_dir = get_qvim_rtp_dir() M.qvim_cache_dir = get_qvim_cache_dir() +M.qvim_data_dir = get_qvim_data_dir() +M.qvim_log_dir = get_qvim_log_dir() M.opt_dir = join_paths(get_qvim_data_dir(), "after", "pack", "lazy", "opt") M.lazy_install_dir = join_paths(M.opt_dir, "lazy.nvim") @@ -109,19 +121,15 @@ function M:init() return get_qvim_data_dir() elseif what == "config" then return get_qvim_config_dir() + elseif what == "log" then + return get_qvim_log_dir() else return vim.call("stdpath", what) end end - local structlog_path = join_paths(self.opt_dir, "structlog") - if - not os.getenv("QV_FIRST_TIME_SETUP") - and utils.is_directory(structlog_path) - then - vim.opt.rtp:append(structlog_path) - require("qvim.log"):init_pre_setup() - end + local log_path = join_paths(self.opt_dir, "structlog") + vim.opt.rtp:prepend(log_path) require("qvim.core.manager"):init({ package_root = self.opt_dir, @@ -136,16 +144,8 @@ function M:init() end function M:setup() - local utils = require("qvim.utils") - - local structlog_path = join_paths(self.opt_dir, "structlog") - local manager = require("qvim.core.manager") manager:load() - if utils.is_directory(structlog_path) then - require("qvim.log"):init_post_setup() - end - require("qvim.core.plugins.mason").bootstrap() end diff --git a/lua/qvim/core/manager.lua b/lua/qvim/core/manager.lua index b045c67..f5fe026 100644 --- a/lua/qvim/core/manager.lua +++ b/lua/qvim/core/manager.lua @@ -113,14 +113,14 @@ function manager:load(spec) git = { timeout = 120, }, - lockfile = join_paths(get_qvim_config_dir(), "lazy-lock.json"), + lockfile = join_paths(get_qvim_rtp_dir(), "lazy-lock.json"), performance = { rtp = { reset = true, }, }, readme = { - root = join_paths(get_qvim_config_dir(), "lazy", "readme"), + root = join_paths(get_qvim_rtp_dir(), "lazy", "readme"), }, } diff --git a/lua/qvim/log.lua b/lua/qvim/log.lua index dc911ef..4b92946 100644 --- a/lua/qvim/log.lua +++ b/lua/qvim/log.lua @@ -12,6 +12,9 @@ ---@field warn function ---@field error function local Log = {} +Log.__index = Log + +local Log_mt = { __index = Log } Log.levels = { TRACE = 1, @@ -40,7 +43,7 @@ function Log:set_level(level) end end -function Log:init_pre_setup() +function Log:setup() local status_ok, structlog = pcall(require, "structlog") if not status_ok then return nil @@ -116,93 +119,6 @@ function Log:init_pre_setup() }) end -function Log:init_post_setup() - local status_ok, structlog = pcall(require, "structlog") - if not status_ok then - return nil - end - structlog.configure({ - qvim = { - pipelines = { - { - level = structlog.level.INFO, - processors = { - structlog.processors.StackWriter( - { "line", "file" }, - { max_parents = 0, stack_level = 0 } - ), - structlog.processors.Timestamper("%H:%M:%S"), - }, - formatter = structlog.formatters.FormatColorizer( -- - "%s [%s] %s: %-30s", - { "timestamp", "level", "logger_name", "msg" }, - { - level = structlog.formatters.FormatColorizer.color_level(), - } - ), - sink = structlog.sinks.Console(), - }, - { - level = structlog.level.DEBUG, - processors = { - structlog.processors.StackWriter( - { "line", "file" }, - { max_parents = 3 } - ), - structlog.processors.Timestamper("%H:%M:%S"), - }, - formatter = structlog.formatters.Format( -- - "%s [%s] %s: %-30s", - { "timestamp", "level", "logger_name", "msg" } - ), - sink = structlog.sinks.File(self:get_path("debug")), - }, - { - level = structlog.level.TRACE, - processors = { - structlog.processors.StackWriter( - { "line", "file" }, - { max_parents = 3 } - ), - structlog.processors.Timestamper("%H:%M:%S"), - }, - formatter = structlog.formatters.Format( -- - "%s [%s] %s: %-30s", - { "timestamp", "level", "logger_name", "msg" } - ), - sink = structlog.sinks.File(self:get_path("trace")), - }, - { - level = structlog.level.WARN, - processors = {}, - formatter = structlog.formatters.Format( -- - "%s", - { "msg" }, - { blacklist = { "level", "logger_name" } } - ), - sink = structlog.sinks.NvimNotify(), - }, - { - level = structlog.level.ERROR, - processors = { - structlog.processors.StackWriter( - { "line", "file" }, - { max_parents = 3 } - ), - structlog.processors.Timestamper("%H:%M:%S"), - }, - formatter = structlog.formatters.Format( -- - "%s [%s] %s: %-30s", - { "timestamp", "level", "logger_name", "msg" } - ), - sink = structlog.sinks.File(self:get_path("error")), - }, - }, - }, - }) -end - ---- Adds a log entry using Plenary.log ---@param level integer [same as vim.log.levels] ---@param msg any ---@param event any @@ -221,10 +137,12 @@ function Log:add_entry(level, msg, event) end ---Retrieves the handle of the logger object +---@param pipeline string|nil ---@return table|nil logger handle if found -function Log:get_logger() +function Log:get_logger(pipeline) + pipeline = pipeline or "qvim" local logger_ok, logger = pcall(function() - return require("structlog").get_logger("qvim") + return require("structlog").get_logger(pipeline) end) if logger_ok and logger then return logger @@ -238,7 +156,7 @@ function Log:get_path(variant) variant = variant or "" return string.format( "%s/%s-%s.log", - get_qvim_cache_dir(), + get_qvim_log_dir(), variant:lower(), "qvim" ) @@ -279,4 +197,4 @@ function Log:error(msg, event) Log:add_entry(self.levels.ERROR, msg, event) end -return Log +return setmetatable(Log, Log_mt) diff --git a/utils/bin/qvim.template b/utils/bin/qvim.template index 80d27b5..dedd70b 100644 --- a/utils/bin/qvim.template +++ b/utils/bin/qvim.template @@ -5,6 +5,7 @@ export QUANTUMVIM_STATE_DIR="${QUANTUMVIM_STATE_DIR:-STATE_DIR_VAR}" export QUANTUMVIM_CONFIG_DIR="${QUANTUMVIM_CONFIG_DIR:-CONFIG_DIR_VAR}" export QUANTUMVIM_DATA_DIR="${QUANTUMVIM_DATA_DIR:-DATA_DIR_VAR}" export QUANTUMVIM_CACHE_DIR="${QUANTUMVIM_CACHE_DIR:-CACHE_DIR_VAR}" +export QUANTUMVIM_LOG_DIR="${QUANTUMVIM_LOG_DIR:-LOG_DIR_VAR}" export NVIM_APPNAME="${NVIM_APPNAME:-APPNAME_VAR}" exec -a qvim nvim -u "$QUANTUMVIM_RTP_DIR/init.lua" "$@" diff --git a/utils/ci/run_test.sh b/utils/ci/run_test.sh index 2fa16f3..19e733f 100644 --- a/utils/ci/run_test.sh +++ b/utils/ci/run_test.sh @@ -3,17 +3,19 @@ set -e qvim_state_name="quantumvim" export XDG_STATE_HOME="$HOME/.local/state" +export XDG_LOG_HOME="$HOME/.local/log" export QV_FIRST_TIME_SETUP=1 export NVIM_APPNAME="${NVIM_APPNAME:-"qvim"}" +QUANTUMVIM_LOG_DIR="${QUANTUMVIM_LOG_DIR:-"$XDG_LOG_HOME/$NVIM_APPNAME"}" QUANTUMVIM_STATE_DIR="${QUANTUMVIM_STATE_DIR:-"$XDG_STATE_HOME/$qvim_state_name"}" QUANTUMVIM_RTP_DIR="${QUANTUMVIM_RTP_DIR:-"$QUANTUMVIM_STATE_DIR/$NVIM_APPNAME"}" QUANTUMVIM_CONFIG_DIR="${QUANTUMVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/$NVIM_APPNAME"}" QUANTUMVIM_CACHE_DIR="$(mktemp -d)" QUANTUMVIM_DATA_DIR="$(mktemp -d)" -export QUANTUMVIM_CACHE_DIR QUANTUMVIM_STATE_DIR QUANTUMVIM_DATA_DIR QUANTUMVIM_CONFIG_DIR QUANTUMVIM_RTP_DIR +export QUANTUMVIM_CACHE_DIR QUANTUMVIM_STATE_DIR QUANTUMVIM_DATA_DIR QUANTUMVIM_CONFIG_DIR QUANTUMVIM_RTP_DIR QUANTUMVIM_LOG_DIR echo "state: $QUANTUMVIM_STATE_DIR" echo "rtp: $QUANTUMVIM_RTP_DIR" diff --git a/utils/installer/install.sh b/utils/installer/install.sh index 37df2d6..a6210cf 100644 --- a/utils/installer/install.sh +++ b/utils/installer/install.sh @@ -14,11 +14,13 @@ declare -xr XDG_DATA_HOME="${XDG_DATA_HOME:-"$HOME/.local/share"}" declare -xr XDG_CACHE_HOME="${XDG_CACHE_HOME:-"$HOME/.cache"}" declare -xr XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}" declare -xr XDG_STATE_HOME="${XDG_STATE_HOME:-"$HOME/.local/state"}" +declare -xr XDG_LOG_HOME="${XDG_LOG_HOME:-"$HOME/.local/log"}" declare -xr QUANTUMVIM_STATE_DIR="${QUANTUMVIM_STATE_DIR:-"$XDG_STATE_HOME/$qvim_state_name"}" declare -xr QUANTUMVIM_RTP_DIR="${QUANTUMVIM_RTP_DIR:-"$QUANTUMVIM_STATE_DIR/$NVIM_APPNAME"}" declare -xr QUANTUMVIM_CACHE_DIR="${QUANTUMVIM_CACHE_DIR:-"$XDG_CACHE_HOME/$NVIM_APPNAME"}" declare -xr QUANTUMVIM_CONFIG_DIR="${QUANTUMVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/$NVIM_APPNAME"}" +declare -xr QUANTUMVIM_LOG_DIR="${QUANTUMVIM_LOG_DIR:-"$XDG_LOG_HOME/$NVIM_APPNAME"}" declare -xr QUANTUMVIM_LOG_LEVEL="${QUANTUMVIM_LOG_LEVEL:-warn}" declare -xir QV_FIRST_TIME_SETUP=1 @@ -38,7 +40,7 @@ declare -a __qvim_dirs=( "$QUANTUMVIM_STATE_DIR" "$QUANTUMVIM_CACHE_DIR" "$QUANTUMVIM_CONFIG_DIR" - # TODO log dir + "$QUANTUMVIM_LOG_DIR" ) function usage() { diff --git a/utils/installer/install_bin.sh b/utils/installer/install_bin.sh index 3d664c4..8c76e27 100644 --- a/utils/installer/install_bin.sh +++ b/utils/installer/install_bin.sh @@ -10,8 +10,9 @@ XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}" XDG_DATA_HOME="${XDG_DATA_HOME:-"$HOME/.local/share"}" XDG_STATE_HOME="${XDG_STATE_HOME:-"$HOME/.local/state"}" XDG_CACHE_HOME="${XDG_CACHE_HOME:-"$HOME/.cache"}" +XDG_LOG_HOME="${XDG_LOG_HOME:-"$HOME/.local/log"}" - +QUANTUMVIM_LOG_DIR="${QUANTUMVIM_LOG_DIR:-"$XDG_LOG_HOME/$NVIM_APPNAME"}" QUANTUMVIM_CONFIG_DIR="${QUANTUMVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/$NVIM_APPNAME"}" QUANTUMVIM_DATA_DIR="${QUANTUMVIM_DATA_DIR:-"$XDG_DATA_HOME/$NVIM_APPNAME"}" QUANTUMVIM_STATE_DIR="${QUANTUMVIM_STATE_DIR:-"$XDG_STATE_HOME/$qvim_state_name"}"