Skip to content

Commit

Permalink
Merge pull request #25 from QuantumVim/6-feat-add-a-log-diroctory
Browse files Browse the repository at this point in the history
feat(#6): added log directory
  • Loading branch information
quantumfate authored Aug 21, 2023
2 parents 81cae48 + 925516a commit 7bb6743
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 113 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 16 additions & 16 deletions lua/qvim/bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")

Expand All @@ -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,
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lua/qvim/core/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}

Expand Down
102 changes: 10 additions & 92 deletions lua/qvim/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
---@field warn function
---@field error function
local Log = {}
Log.__index = Log

local Log_mt = { __index = Log }

Log.levels = {
TRACE = 1,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions utils/bin/qvim.template
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@"
4 changes: 3 additions & 1 deletion utils/ci/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion utils/installer/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion utils/installer/install_bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"}"
Expand Down

0 comments on commit 7bb6743

Please sign in to comment.