Skip to content

Commit e990a8d

Browse files
author
Zhe Yu
committed
add logging for APIs.
1 parent 81dce29 commit e990a8d

7 files changed

Lines changed: 113 additions & 20 deletions

File tree

lua/vectorcode/cacher/default.lua

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local M = {}
33
local vc_config = require("vectorcode.config")
44
local notify_opts = vc_config.notify_opts
55

6+
local logger = vc_config.logger
7+
68
---@type table<integer, VectorCode.Cache>
79
local CACHE = {}
810

@@ -25,6 +27,11 @@ local function async_runner(query_message, buf_nr)
2527
if CACHE[buf_nr] == nil or not CACHE[buf_nr].enabled then
2628
return
2729
end
30+
local buf_name
31+
vim.schedule(function()
32+
buf_name = vim.api.nvim_buf_get_name(buf_nr)
33+
logger.debug("Started default cacher job on :", buf_name)
34+
end)
2835
---@type VectorCode.Cache
2936
local cache = CACHE[buf_nr]
3037
local args = {
@@ -52,6 +59,7 @@ local function async_runner(query_message, buf_nr)
5259
vim.list_extend(args, { "--project_root", project_root })
5360
end
5461
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count + 1
62+
logger.debug("vectorcode default cacher job args: ", args)
5563
local job = require("plenary.job"):new({
5664
command = "vectorcode",
5765
args = args,
@@ -65,6 +73,7 @@ local function async_runner(query_message, buf_nr)
6573
if not M.buf_is_registered(buf_nr) then
6674
return
6775
end
76+
logger.debug("vectorcode ", buf_name, " default cacher results: ", self:result())
6877
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count - 1
6978
CACHE[buf_nr].jobs[self.pid] = nil
7079
local ok, json = pcall(
@@ -130,6 +139,12 @@ M.register_buffer = vc_config.check_cli_wrap(
130139
if bufnr == 0 or bufnr == nil then
131140
bufnr = vim.api.nvim_get_current_buf()
132141
end
142+
logger.info(
143+
("Registering buffer %s %s for default cacher."):format(
144+
bufnr,
145+
vim.api.nvim_buf_get_name(bufnr)
146+
)
147+
)
133148
if M.buf_is_registered(bufnr) then
134149
opts = vim.tbl_deep_extend("force", CACHE[bufnr].options, opts or {})
135150
end
@@ -140,6 +155,12 @@ M.register_buffer = vc_config.check_cli_wrap(
140155
-- update the options and/or query_cb
141156
CACHE[bufnr].options =
142157
vim.tbl_deep_extend("force", CACHE[bufnr].options, opts or {})
158+
logger.debug(
159+
("Updated `default` cacher opts for buffer %s:\n%s"):format(
160+
bufnr,
161+
vim.inspect(opts)
162+
)
163+
)
143164
else
144165
CACHE[bufnr] = {
145166
enabled = true,
@@ -196,6 +217,9 @@ M.deregister_buffer = vc_config.check_cli_wrap(
196217
if bufnr == nil or bufnr == 0 then
197218
bufnr = vim.api.nvim_get_current_buf()
198219
end
220+
logger.info(
221+
("Deregistering buffer %s %s"):format(bufnr, vim.api.nvim_buf_get_name(bufnr))
222+
)
199223
if M.buf_is_registered(bufnr) then
200224
kill_jobs(bufnr)
201225
vim.api.nvim_del_augroup_by_name(("VectorCodeCacheGroup%d"):format(bufnr))
@@ -244,13 +268,11 @@ M.query_from_cache = vc_config.check_cli_wrap(
244268
opts or {}
245269
)
246270
result = CACHE[bufnr].retrieval or {}
271+
local message = ("Retrieved %d documents from cache."):format(#result)
272+
logger.info(("vectorcode cmd cacher for buf %s: %s"):format(bufnr, message))
247273
if opts.notify then
248274
vim.schedule(function()
249-
vim.notify(
250-
("Retrieved %d documents from cache."):format(#result),
251-
vim.log.levels.INFO,
252-
notify_opts
253-
)
275+
vim.notify(message, vim.log.levels.INFO, notify_opts)
254276
end)
255277
end
256278
end

lua/vectorcode/cacher/lsp.lua

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---@type VectorCode.CacheBackend
22
local M = {}
33
local vc_config = require("vectorcode.config")
4+
local logger = vc_config.logger
5+
46
local notify_opts = vc_config.notify_opts
57

68
local job_runner = require("vectorcode.jobrunner.lsp")
@@ -86,6 +88,11 @@ local function async_runner(query_message, buf_nr)
8688
if CACHE[buf_nr] == nil or not CACHE[buf_nr].enabled or not is_lsp_running() then
8789
return
8890
end
91+
local buf_name
92+
vim.schedule(function()
93+
buf_name = vim.api.nvim_buf_get_name(buf_nr)
94+
logger.debug("Started lsp cacher job on :", buf_name)
95+
end)
8996
assert(client_id ~= nil)
9097
---@type VectorCode.Cache
9198
local cache = CACHE[buf_nr]
@@ -127,8 +134,14 @@ local function async_runner(query_message, buf_nr)
127134
kill_jobs(buf_nr)
128135
end
129136
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count + 1
137+
138+
logger.debug("vectorcode lsp cacher job args: ", args)
130139
local request_id = job_runner.run_async(args, function(result, err)
131140
CACHE[buf_nr].retrieval = result or CACHE[buf_nr].retrieval or {}
141+
logger.debug("vectorcode ", buf_name, " lsp cacher results: ", result)
142+
if err then
143+
logger.debug("vectorcode ", buf_name, " lsp cacher error: ", err)
144+
end
132145
end, buf_nr)
133146

134147
if request_id ~= nil then
@@ -159,8 +172,21 @@ M.register_buffer = vc_config.check_cli_wrap(
159172
if bufnr == 0 or bufnr == nil then
160173
bufnr = vim.api.nvim_get_current_buf()
161174
end
175+
176+
logger.info(
177+
("Registering buffer %s %s for lsp cacher."):format(
178+
bufnr,
179+
vim.api.nvim_buf_get_name(bufnr)
180+
)
181+
)
162182
if M.buf_is_registered(bufnr) then
163183
opts = vim.tbl_deep_extend("force", CACHE[bufnr].options, opts or {})
184+
logger.debug(
185+
("Updated `lsp` cacher opts for buffer %s:\n%s"):format(
186+
bufnr,
187+
vim.inspect(opts)
188+
)
189+
)
164190
end
165191
opts =
166192
vim.tbl_deep_extend("force", vc_config.get_user_config().async_opts, opts or {})
@@ -222,6 +248,9 @@ M.deregister_buffer = vc_config.check_cli_wrap(
222248
if bufnr == nil or bufnr == 0 then
223249
bufnr = vim.api.nvim_get_current_buf()
224250
end
251+
logger.info(
252+
("Deregistering buffer %s %s"):format(bufnr, vim.api.nvim_buf_get_name(bufnr))
253+
)
225254
if M.buf_is_registered(bufnr) then
226255
kill_jobs(bufnr)
227256
vim.api.nvim_del_augroup_by_name(("VectorCodeCacheGroup%d"):format(bufnr))
@@ -276,13 +305,11 @@ M.query_from_cache = vc_config.check_cli_wrap(
276305
opts or {}
277306
)
278307
result = CACHE[bufnr].retrieval or {}
308+
local message = ("Retrieved %d documents from cache."):format(#result)
309+
logger.info(("vectorcode cmd cacher for buf %s: %s"):format(bufnr, message))
279310
if opts.notify then
280311
vim.schedule(function()
281-
vim.notify(
282-
("Retrieved %d documents from cache."):format(#result),
283-
vim.log.levels.INFO,
284-
notify_opts
285-
)
312+
vim.notify(message, vim.log.levels.INFO, notify_opts)
286313
end)
287314
end
288315
end

lua/vectorcode/config.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,43 @@ local config = {
2424
notify = true,
2525
timeout_ms = 5000,
2626
on_setup = { update = false, lsp = false },
27+
sync_log_env_var = false,
2728
}
2829

30+
local setup_config = vim.deepcopy(config, true)
31+
2932
---@return vim.lsp.ClientConfig
3033
local lsp_configs = function()
34+
---@type vim.lsp.ClientConfig
3135
local cfg =
3236
{ cmd = { "vectorcode-server" }, root_markers = { ".vectorcode", ".git" } }
3337
if vim.lsp.config ~= nil and vim.lsp.config.vectorcode_server ~= nil then
3438
-- nvim >= 0.11.0
3539
cfg = vim.tbl_deep_extend("force", cfg, vim.lsp.config.vectorcode_server)
40+
logger.debug("Using vim.lsp.config.vectorcode_server for LSP config:\n", cfg)
3641
else
3742
-- nvim < 0.11.0
3843
local ok, lspconfig = pcall(require, "lspconfig.configs")
3944
if ok and lspconfig.vectorcode_server ~= nil then
4045
cfg = lspconfig.vectorcode_server.config_def.default_config
46+
logger.debug("Using nvim-lspconfig for LSP config:\n", cfg)
4147
end
4248
end
4349
cfg.name = "vectorcode_server"
50+
if setup_config.sync_log_env_var then
51+
local level = os.getenv("VECTORCODE_NVIM_LOG_LEVEL") or nil
52+
if level ~= nil then
53+
level = string.upper(level)
54+
if level == "TRACE" then
55+
-- there's no `TRACE` in python logging
56+
level = "DEBUG"
57+
end
58+
cfg.cmd_env["VECTORCODE_LOG_LEVEL"] = level
59+
end
60+
end
4461
return cfg
4562
end
4663

47-
local setup_config = vim.deepcopy(config, true)
4864
local notify_opts = { title = "VectorCode" }
4965

5066
---@param opts {notify:boolean}?
@@ -75,6 +91,7 @@ local startup_handler = check_cli_wrap(function(configs)
7591
if out.code == 0 then
7692
local path = string.gsub(out.stdout, "^%s*(.-)%s*$", "%1")
7793
if path ~= "" then
94+
logger.info("Running `vectorcode update` on start up.")
7895
require("vectorcode").update(path)
7996
end
8097
end
@@ -84,6 +101,7 @@ local startup_handler = check_cli_wrap(function(configs)
84101
local ok, runner = pcall(require, "vectorcode.jobrunner.lsp")
85102
if not ok or not type(runner) == "table" or runner == nil then
86103
vim.notify("Failed to start vectorcode-server.", vim.log.levels.WARN, notify_opts)
104+
logger.error("Failed to start vectorcode-server.")
87105
return
88106
end
89107
runner.init()
@@ -98,7 +116,7 @@ return {
98116
setup = check_cli_wrap(
99117
---@param opts VectorCode.Opts?
100118
function(opts)
101-
logger.info(opts)
119+
logger.info("Received setup opts:\n", opts)
102120
opts = opts or {}
103121
setup_config = vim.tbl_deep_extend("force", config, opts or {})
104122
for k, v in pairs(setup_config.async_opts) do

lua/vectorcode/init.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
local M = {}
22

33
local vc_config = require("vectorcode.config")
4+
5+
local logger = vc_config.logger
6+
47
local get_config = vc_config.get_user_config
58

69
local notify_opts = vc_config.notify_opts
@@ -15,6 +18,7 @@ M.query = vc_config.check_cli_wrap(
1518
---@param callback fun(result:VectorCode.Result[])? Use the result async style.
1619
---@return VectorCode.Result[]? An array of results.
1720
function(query_message, opts, callback)
21+
logger.info("vectorcode.query: ", query_message, opts, callback)
1822
opts = vim.tbl_deep_extend("force", vc_config.get_query_opts(), opts or {})
1923
if opts.n_query == 0 then
2024
if opts.notify then
@@ -45,8 +49,10 @@ M.query = vc_config.check_cli_wrap(
4549
vim.list_extend(args, { "--exclude", vim.api.nvim_buf_get_name(0) })
4650
end
4751

52+
logger.debug("vectorcode.query cmd args: ", args)
4853
local decoded_response = {}
4954
local job = vim.system(args, { text = true }, function(out)
55+
logger.debug("vectorcode.query cmd output: ", out)
5056
local raw_response
5157
if out.code == 124 and out.signal == 9 then
5258
-- killed due to timeout
@@ -77,6 +83,7 @@ M.query = vc_config.check_cli_wrap(
7783
if type(callback) == "function" then
7884
callback(decoded_response)
7985
end
86+
logger.info("vectorcode.query result:\n", decoded_response)
8087
end
8188
end)
8289

@@ -95,6 +102,7 @@ M.vectorise = vc_config.check_cli_wrap(
95102
---@param files string|string[] Files to vectorise.
96103
---@param project_root string? Add the `--project_root` flag and the passed argument to the command.
97104
function(files, project_root)
105+
logger.info("vectorcode.vectorise: ", files, project_root)
98106
local args = { "--pipe", "vectorise" }
99107
if project_root ~= nil then
100108
vim.list_extend(args, { "--project_root", project_root })
@@ -130,6 +138,7 @@ M.vectorise = vc_config.check_cli_wrap(
130138
)
131139
end)
132140
end
141+
logger.debug("vectorcode.vectorise cmd args: ", args)
133142
require("plenary.job")
134143
:new({
135144
command = "vectorcode",
@@ -158,14 +167,17 @@ M.vectorise = vc_config.check_cli_wrap(
158167

159168
---@param project_root string?
160169
M.update = vc_config.check_cli_wrap(function(project_root)
170+
logger.info("vectorcode.update: ", project_root)
161171
local args = { "vectorcode", "update" }
162172
if
163173
type(project_root) == "string"
164174
and vim.uv.fs_stat(vim.fs.normalize(project_root)).type == "directory"
165175
then
166176
vim.list_extend(args, { "--project_root", project_root })
167177
end
178+
logger.debug("vectorcode.update cmd args: ", args)
168179
vim.system(args, { stdout = nil, stderr = nil }, function(out)
180+
logger.debug("vectorcode.update cmd out:\n", out)
169181
if get_config().notify then
170182
vim.schedule(function()
171183
if out.code == 0 then

lua/vectorcode/jobrunner/cmd.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local runner = {}
44
local Job = require("plenary.job")
55
---@type {integer: Job}
66
local jobs = {}
7+
local logger = require("vectorcode.config").logger
78

89
function runner.run_async(args, callback, bufnr)
910
if type(callback) == "function" then
@@ -14,7 +15,9 @@ function runner.run_async(args, callback, bufnr)
1415
local cmd = { "vectorcode" }
1516
args = require("vectorcode.jobrunner").find_root(args, bufnr)
1617
vim.list_extend(cmd, args)
17-
18+
logger.debug(
19+
("cmd jobrunner for buffer %s args: %s"):format(bufnr, vim.inspect(args))
20+
)
1821
local job = Job:new({
1922
command = "vectorcode",
2023
args = args,
@@ -26,6 +29,7 @@ function runner.run_async(args, callback, bufnr)
2629
if ok then
2730
callback(decoded, self:stderr_result())
2831
else
32+
logger.info("cmd runner: failed to decode result:\n", result)
2933
callback({ result }, self:stderr_result())
3034
end
3135
end

0 commit comments

Comments
 (0)