feat: update logging with debug logs
This commit is contained in:
parent
5c93bd259e
commit
e9f1b06843
5 changed files with 51 additions and 35 deletions
|
|
@ -29,6 +29,7 @@ M.defaults = {
|
|||
before_run = nil,
|
||||
before_debug = nil,
|
||||
},
|
||||
debug = false,
|
||||
}
|
||||
|
||||
---@param base_config table
|
||||
|
|
@ -56,6 +57,7 @@ function M.setup(user_config)
|
|||
contests = { user_config.contests, { "table", "nil" }, true },
|
||||
snippets = { user_config.snippets, { "table", "nil" }, true },
|
||||
hooks = { user_config.hooks, { "table", "nil" }, true },
|
||||
debug = { user_config.debug, { "boolean", "nil" }, true },
|
||||
})
|
||||
|
||||
if user_config.hooks then
|
||||
|
|
|
|||
|
|
@ -3,16 +3,13 @@ local snippets = require("cp.snippets")
|
|||
local execute = require("cp.execute")
|
||||
local scrape = require("cp.scrape")
|
||||
local window = require("cp.window")
|
||||
local logger = require("cp.log")
|
||||
|
||||
local M = {}
|
||||
local config = {}
|
||||
|
||||
local function log(msg, level)
|
||||
vim.notify(("[cp.nvim]: %s"):format(msg), level or vim.log.levels.INFO)
|
||||
end
|
||||
|
||||
if not vim.fn.has("nvim-0.10.0") then
|
||||
log("cp.nvim requires nvim-0.10.0+", vim.log.levels.ERROR)
|
||||
logger.log("cp.nvim requires nvim-0.10.0+", vim.log.levels.ERROR)
|
||||
return M
|
||||
end
|
||||
|
||||
|
|
@ -20,7 +17,7 @@ local competition_types = { "atcoder", "codeforces", "cses" }
|
|||
|
||||
local function setup_contest(contest_type)
|
||||
if not vim.tbl_contains(competition_types, contest_type) then
|
||||
log(
|
||||
logger.log(
|
||||
("unknown contest type. Available: [%s]"):format(table.concat(competition_types, ", ")),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
|
|
@ -30,12 +27,12 @@ local function setup_contest(contest_type)
|
|||
vim.g.cp_contest = contest_type
|
||||
vim.fn.mkdir("build", "p")
|
||||
vim.fn.mkdir("io", "p")
|
||||
log(("set up %s contest environment"):format(contest_type))
|
||||
logger.log(("set up %s contest environment"):format(contest_type))
|
||||
end
|
||||
|
||||
local function setup_problem(problem_id, problem_letter)
|
||||
if not vim.g.cp_contest then
|
||||
log("no contest mode set. run :CP <contest> first", vim.log.levels.ERROR)
|
||||
logger.log("no contest mode set. run :CP <contest> first", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -57,10 +54,10 @@ local function setup_problem(problem_id, problem_letter)
|
|||
local scrape_result = scrape.scrape_problem(vim.g.cp_contest, problem_id, problem_letter)
|
||||
|
||||
if not scrape_result.success then
|
||||
log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN)
|
||||
log("you can manually add test cases to io/ directory", vim.log.levels.INFO)
|
||||
logger.log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN)
|
||||
logger.log("you can manually add test cases to io/ directory", vim.log.levels.INFO)
|
||||
else
|
||||
log(("scraped %d test case(s) for %s"):format(scrape_result.test_count, scrape_result.problem_id))
|
||||
logger.log(("scraped %d test case(s) for %s"):format(scrape_result.test_count, scrape_result.problem_id))
|
||||
end
|
||||
|
||||
local full_problem_id = scrape_result.success and scrape_result.problem_id
|
||||
|
|
@ -115,13 +112,13 @@ local function setup_problem(problem_id, problem_letter)
|
|||
window.clearcol()
|
||||
vim.cmd.wincmd("h")
|
||||
|
||||
log(("switched to problem %s"):format(full_problem_id))
|
||||
logger.log(("switched to problem %s"):format(full_problem_id))
|
||||
end
|
||||
|
||||
local function get_current_problem()
|
||||
local filename = vim.fn.expand("%:t:r")
|
||||
if filename == "" then
|
||||
log("no file open", vim.log.levels.ERROR)
|
||||
logger.log("no file open", vim.log.levels.ERROR)
|
||||
return nil
|
||||
end
|
||||
return filename
|
||||
|
|
@ -138,7 +135,7 @@ local function run_problem()
|
|||
end
|
||||
|
||||
if not vim.g.cp_contest then
|
||||
log("no contest mode set", vim.log.levels.ERROR)
|
||||
logger.log("no contest mode set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -161,7 +158,7 @@ local function debug_problem()
|
|||
end
|
||||
|
||||
if not vim.g.cp_contest then
|
||||
log("no contest mode set", vim.log.levels.ERROR)
|
||||
logger.log("no contest mode set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -178,7 +175,7 @@ local function diff_problem()
|
|||
window.restore_layout(vim.g.cp_saved_layout)
|
||||
vim.g.cp_diff_mode = false
|
||||
vim.g.cp_saved_layout = nil
|
||||
log("exited diff mode")
|
||||
logger.log("exited diff mode")
|
||||
else
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
|
|
@ -191,7 +188,7 @@ local function diff_problem()
|
|||
local input = ("%s/io/%s.in"):format(base_fp, problem_id)
|
||||
|
||||
if vim.fn.filereadable(expected) == 0 then
|
||||
log(("No expected output file found: %s"):format(expected), vim.log.levels.ERROR)
|
||||
logger.log(("No expected output file found: %s"):format(expected), vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -203,7 +200,7 @@ local function diff_problem()
|
|||
window.setup_diff_layout(actual_output, expected, input)
|
||||
|
||||
vim.g.cp_diff_mode = true
|
||||
log("entered diff mode")
|
||||
logger.log("entered diff mode")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -219,6 +216,7 @@ function M.setup(user_config)
|
|||
end
|
||||
|
||||
config = config_module.setup(user_config)
|
||||
logger.set_config(config)
|
||||
snippets.setup(config)
|
||||
initialized = true
|
||||
end
|
||||
|
|
@ -226,7 +224,7 @@ end
|
|||
function M.handle_command(opts)
|
||||
local args = opts.fargs
|
||||
if #args == 0 then
|
||||
log("Usage: :CP <contest|problem_id|run|debug|diff>", vim.log.levels.ERROR)
|
||||
logger.log("Usage: :CP <contest|problem_id|run|debug|diff>", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -256,7 +254,7 @@ function M.handle_command(opts)
|
|||
setup_problem(cmd)
|
||||
end
|
||||
else
|
||||
log(
|
||||
logger.log(
|
||||
("unknown contest type '%s'. Available: [%s]"):format(cmd, table.concat(competition_types, ", ")),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
|
|
|
|||
16
lua/cp/log.lua
Normal file
16
lua/cp/log.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
local M = {}
|
||||
|
||||
local config = nil
|
||||
|
||||
function M.set_config(user_config)
|
||||
config = user_config
|
||||
end
|
||||
|
||||
function M.log(msg, level)
|
||||
level = level or vim.log.levels.INFO
|
||||
if not config or config.debug or level >= vim.log.levels.WARN then
|
||||
vim.notify(("[cp.nvim]: %s"):format(msg), level)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
local M = {}
|
||||
local logger = require("cp.log")
|
||||
|
||||
local function get_plugin_path()
|
||||
local plugin_path = debug.getinfo(1, "S").source:sub(2)
|
||||
|
|
@ -9,16 +10,12 @@ local function ensure_io_directory()
|
|||
vim.fn.mkdir("io", "p")
|
||||
end
|
||||
|
||||
local function log(msg, level)
|
||||
vim.notify(("[cp.nvim]: %s"):format(msg), level or vim.log.levels.INFO)
|
||||
end
|
||||
|
||||
local function setup_python_env()
|
||||
local plugin_path = get_plugin_path()
|
||||
local venv_dir = plugin_path .. "/.venv"
|
||||
|
||||
if vim.fn.executable("uv") == 0 then
|
||||
log(
|
||||
logger.log(
|
||||
"uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/",
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
|
|
@ -26,13 +23,13 @@ local function setup_python_env()
|
|||
end
|
||||
|
||||
if vim.fn.isdirectory(venv_dir) == 0 then
|
||||
log("setting up Python environment for scrapers...")
|
||||
logger.log("setting up Python environment for scrapers...")
|
||||
local result = vim.system({ "uv", "sync" }, { cwd = plugin_path, text = true }):wait()
|
||||
if result.code ~= 0 then
|
||||
log("failed to setup Python environment: " .. result.stderr, vim.log.levels.ERROR)
|
||||
logger.log("failed to setup Python environment: " .. result.stderr, vim.log.levels.ERROR)
|
||||
return false
|
||||
end
|
||||
log("python environment setup complete")
|
||||
logger.log("python environment setup complete")
|
||||
end
|
||||
|
||||
return true
|
||||
|
|
@ -41,20 +38,20 @@ end
|
|||
function M.scrape_problem(contest, problem_id, problem_letter)
|
||||
ensure_io_directory()
|
||||
|
||||
local full_problem_id = problem_id:lower()
|
||||
local cache_problem_id = problem_id:lower()
|
||||
if contest == "atcoder" or contest == "codeforces" then
|
||||
if problem_letter then
|
||||
full_problem_id = full_problem_id .. problem_letter:upper()
|
||||
cache_problem_id = cache_problem_id .. problem_letter:lower()
|
||||
end
|
||||
end
|
||||
|
||||
local input_file = "io/" .. full_problem_id .. ".in"
|
||||
local expected_file = "io/" .. full_problem_id .. ".expected"
|
||||
local input_file = "io/" .. cache_problem_id .. ".in"
|
||||
local expected_file = "io/" .. cache_problem_id .. ".expected"
|
||||
|
||||
if vim.fn.filereadable(input_file) == 1 and vim.fn.filereadable(expected_file) == 1 then
|
||||
return {
|
||||
success = true,
|
||||
problem_id = full_problem_id,
|
||||
problem_id = cache_problem_id,
|
||||
test_count = 1,
|
||||
}
|
||||
end
|
||||
|
|
@ -101,7 +98,9 @@ function M.scrape_problem(contest, problem_id, problem_letter)
|
|||
return data
|
||||
end
|
||||
|
||||
full_problem_id = data.problem_id:lower()
|
||||
local full_problem_id = data.problem_id:lower()
|
||||
input_file = "io/" .. full_problem_id .. ".in"
|
||||
expected_file = "io/" .. full_problem_id .. ".expected"
|
||||
|
||||
if #data.test_cases > 0 then
|
||||
local all_inputs = {}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
local M = {}
|
||||
local logger = require("cp.log")
|
||||
|
||||
function M.setup(config)
|
||||
local ok, ls = pcall(require, "luasnip")
|
||||
if not ok then
|
||||
vim.notify("[cp.nvim]: LuaSnip not available - snippets disabled", vim.log.levels.INFO)
|
||||
logger.log("LuaSnip not available - snippets disabled", vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue