Skip to content

Commit bf38d16

Browse files
committed
feat: truncating log file and GpInspectLog
1 parent cdb644a commit bf38d16

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lua/gp/config.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ local config = {
511511

512512
-- example hook functions (see Extend functionality section in the README)
513513
hooks = {
514+
-- GpInspectPlugin provides a detailed inspection of the plugin state
514515
InspectPlugin = function(plugin, params)
515516
local bufnr = vim.api.nvim_create_buf(false, true)
516517
local copy = vim.deepcopy(plugin)
@@ -529,6 +530,17 @@ local config = {
529530
vim.api.nvim_win_set_buf(0, bufnr)
530531
end,
531532

533+
-- GpInspectLog for checking the log file
534+
InspectLog = function(plugin, params)
535+
local log_file = plugin.config.log_file
536+
local buffer = plugin.helpers.get_buffer(log_file)
537+
if not buffer then
538+
vim.cmd("e " .. log_file)
539+
else
540+
vim.cmd("buffer " .. buffer)
541+
end
542+
end,
543+
532544
-- GpImplement rewrites the provided selection/range based on comments in it
533545
Implement = function(gp, params)
534546
local template = "Having following from {{filename}}:\n\n"

lua/gp/logger.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ M.setup = function(path, sensitive)
3535
end
3636
file = path
3737

38+
-- truncate log file if it's too big
39+
if uv.fs_stat(file) then
40+
local content = {}
41+
for line in io.lines(file) do
42+
table.insert(content, line)
43+
end
44+
45+
if #content > 20000 then
46+
local truncated_file = io.open(file, "w")
47+
if truncated_file then
48+
for i, line in ipairs(content) do
49+
if #content - i < 10000 then
50+
truncated_file:write(line .. "\n")
51+
end
52+
end
53+
truncated_file:close()
54+
M.debug("Log file " .. file .. " truncated to last 10K lines")
55+
end
56+
end
57+
end
58+
3859
local log_file = io.open(file, "a")
3960
if log_file then
4061
for _, line in ipairs(M._log_history) do
@@ -61,7 +82,7 @@ local log = function(msg, level, slevel, sensitive)
6182
if not sensitive then
6283
M._log_history[#M._log_history + 1] = raw
6384
end
64-
if #M._log_history > 100 then
85+
if #M._log_history > 20 then
6586
table.remove(M._log_history, 1)
6687
end
6788

0 commit comments

Comments
 (0)