fix(log): improve logging

This commit is contained in:
Barrett Ruth 2025-10-01 16:41:24 -04:00
parent 62af1965f8
commit a925686a17
10 changed files with 144 additions and 225 deletions

View file

@ -52,7 +52,7 @@ end
---@return {code: integer, stdout: string, stderr: string}
function M.compile_generic(language_config, substitutions)
if not language_config.compile then
logger.log('no compilation step required')
logger.log('No compilation step required for language - skipping.')
return { code = 0, stderr = '' }
end
@ -73,9 +73,9 @@ function M.compile_generic(language_config, substitutions)
result.stderr = ansi.bytes_to_string(result.stderr or '')
if result.code == 0 then
logger.log(('compilation successful (%.1fms)'):format(compile_time), vim.log.levels.INFO)
logger.log(('Compilation successful in %.1fms.'):format(compile_time), vim.log.levels.INFO)
else
logger.log(('compilation failed (%.1fms)'):format(compile_time))
logger.log(('Compilation failed in %.1fms.'):format(compile_time))
end
return result
@ -107,14 +107,14 @@ local function execute_command(cmd, input_data, timeout_ms)
local actual_code = result.code or 0
if result.code == 124 then
logger.log(('execution timed out after %.1fms'):format(execution_time), vim.log.levels.WARN)
logger.log(('Execution timed out in %.1fms.'):format(execution_time), vim.log.levels.WARN)
elseif actual_code ~= 0 then
logger.log(
('execution failed (exit code %d, %.1fms)'):format(actual_code, execution_time),
('Execution failed in %.1fms (exit code %d).'):format(execution_time, actual_code),
vim.log.levels.WARN
)
else
logger.log(('execution successful (%.1fms)'):format(execution_time))
logger.log(('Execution successful in %.1fms.'):format(execution_time))
end
return {
@ -177,8 +177,8 @@ function M.compile_problem(contest_config, is_debug)
local state = require('cp.state')
local source_file = state.get_source_file()
if not source_file then
logger.log('No source file found', vim.log.levels.ERROR)
return { success = false, output = 'No source file found' }
logger.log('No source file found.', vim.log.levels.ERROR)
return { success = false, output = 'No source file found.' }
end
local language = get_language_from_file(source_file, contest_config)
@ -186,7 +186,7 @@ function M.compile_problem(contest_config, is_debug)
if not language_config then
logger.log('No configuration for language: ' .. language, vim.log.levels.ERROR)
return { success = false, output = 'No configuration for language: ' .. language }
return { success = false, output = ('No configuration for language %s.'):format(language) }
end
local binary_file = state.get_binary_file()
@ -203,10 +203,6 @@ function M.compile_problem(contest_config, is_debug)
if compile_result.code ~= 0 then
return { success = false, output = compile_result.stdout or 'unknown error' }
end
logger.log(
('compilation successful (%s)'):format(is_debug and 'debug mode' or 'test mode'),
vim.log.levels.INFO
)
end
return { success = true, output = nil }
@ -220,7 +216,10 @@ function M.run_problem(contest_config, is_debug)
local output_file = state.get_output_file()
if not source_file or not output_file then
logger.log('Missing required file paths', vim.log.levels.ERROR)
logger.log(
('Missing required file paths %s and %s'):format(source_file, output_file),
vim.log.levels.ERROR
)
return
end
@ -257,13 +256,14 @@ function M.run_problem(contest_config, is_debug)
local cache = require('cp.cache')
cache.load()
local platform = state.get_platform()
local contest_id = state.get_contest_id()
local problem_id = state.get_problem_id()
local expected_file = state.get_expected_file()
if not platform or not contest_id or not expected_file then
logger.log('configure a contest before running a problem', vim.log.levels.ERROR)
logger.log('Configure a contest before running a problem', vim.log.levels.ERROR)
return
end
local timeout_ms, _ = cache.get_constraints(platform, contest_id, problem_id)

View file

@ -185,7 +185,7 @@ local function run_single_test_case(contest_config, cp_config, test_case)
}
if language_config.compile and binary_file and vim.fn.filereadable(binary_file) == 0 then
logger.log('binary not found, compiling first...')
logger.log('Binary not found - compiling first.')
local compile_cmd = substitute_template(language_config.compile, substitutions)
local redirected_cmd = vim.deepcopy(compile_cmd)
redirected_cmd[#redirected_cmd] = redirected_cmd[#redirected_cmd] .. ' 2>&1'
@ -219,9 +219,6 @@ local function run_single_test_case(contest_config, cp_config, test_case)
local start_time = vim.uv.hrtime()
local timeout_ms = run_panel_state.constraints and run_panel_state.constraints.timeout_ms or 2000
if not run_panel_state.constraints then
logger.log('no problem constraints available, using default 2000ms timeout')
end
local redirected_run_cmd = vim.deepcopy(run_cmd)
redirected_run_cmd[#redirected_run_cmd] = redirected_run_cmd[#redirected_run_cmd] .. ' 2>&1'
local result = vim
@ -315,14 +312,7 @@ function M.load_test_cases(state)
state.get_problem_id()
)
local constraint_info = run_panel_state.constraints
and string.format(
' with %dms/%dMB limits',
run_panel_state.constraints.timeout_ms,
run_panel_state.constraints.memory_mb
)
or ''
logger.log(('loaded %d test case(s)%s'):format(#test_cases, constraint_info), vim.log.levels.INFO)
logger.log(('Loaded %d test case(s)'):format(#test_cases), vim.log.levels.INFO)
return #test_cases > 0
end