Skip to content

Commit

Permalink
Add log messages to the pdb proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sakhnik committed Aug 2, 2023
1 parent 5988f59 commit 767df8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions lib/proxy/base.lua → lib/proxy/impl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ local Proxy = { }
Proxy.__index = Proxy

-- TODO: comments and docs
-- TODO: logging

-- Get rid of the script, leave the arguments only
arg[0] = nil

function Proxy.new(prompt)
log.info({"Proxy.new", promp = prompt})
log.info({"Proxy.new", promp = prompt, arg = arg})
local self = {}
setmetatable(self, Proxy)
self.prompt = prompt
Expand All @@ -39,12 +38,14 @@ function Proxy.new(prompt)
end

function Proxy:start()
log.debug({"Proxy:start"})
self:start_job()
self:start_stdin()
self:start_socket()
end

function Proxy:start_job()
log.debug({"Proxy:start_job"})
local width = self.stdin:get_winsize()

local opts = {
Expand Down Expand Up @@ -73,7 +74,9 @@ function Proxy:start_job()
end

function Proxy:start_stdin()
log.debug({"Proxy:start_stdin"})
self.stdin:read_start(vim.schedule_wrap(function(err, chunk)
log.debug({"stdin:read", err = err, chunk = chunk})
assert(not err, err)

if chunk then
Expand All @@ -83,10 +86,12 @@ function Proxy:start_stdin()
if start_index then
if start_index == 1 then
-- Send previous command
log.debug({"send previous", command = self.last_command})
vim.fn.chansend(self.job_id, self.last_command)
else
-- Remember the command
self.last_command = self.command_buffer:sub(1, start_index - 1)
log.debug({"remember command", self.last_command})
end
-- Reset the command buffer
self.command_buffer = self.command_buffer:sub(end_index + 1)
Expand All @@ -100,12 +105,14 @@ function Proxy:start_stdin()
end

function Proxy:init_socket()
log.debug({"Proxy:init_socket"})
if arg[1] ~= '-a' then
return
end
self.sock = assert(uv.new_udp())
assert(self.sock:bind("127.0.0.1", 0))
local sockname = self.sock:getsockname()
log.info({"Socket port", port = sockname.port})
local f = assert(io.open(arg[2], 'w'))
f:write(sockname.port)
f:close()
Expand All @@ -116,13 +123,16 @@ function Proxy:init_socket()
end
arg[#arg] = nil
arg[#arg] = nil
log.debug({"shift arg", arg = arg})
end

function Proxy:start_socket()
log.debug({"Proxy:start_socket"})
if self.sock == nil then
return
end
self.sock:recv_start(function(err, data, addr)
log.debug({"recv request", err = err, data = data, addr = addr})
assert(not err, err)
if data then
self.request_queue[self.request_queue_tail] = {data, addr}
Expand All @@ -132,13 +142,15 @@ function Proxy:start_socket()
end

function Proxy:on_stdout(data1, data2)
log.debug({"Proxy:on_stdout", data1 = data1, data2 = data2})
if self.current_request ~= nil then
self.buffer = self.buffer .. data1 .. data2
local plain_buffer = self.buffer:gsub('%[[^a-zA-Z]*[a-zA-Z]', '')
local start_index = plain_buffer:find(self.prompt)
if start_index then
local req_id, cmd, addr = unpack(self.current_request)
local response = plain_buffer:sub(#cmd + 1, start_index):match('^%s*(.-)%s*$')
log.info({"Collected response", response = response})
self.request_timer:stop()
self:send_response(req_id, response, addr)
self.buffer = ''
Expand All @@ -155,6 +167,7 @@ function Proxy:on_stdout(data1, data2)
end

function Proxy:process_command()
log.debug({"Proxy:process_command"})
if self.current_request ~= nil or self.request_queue_tail == self.request_queue_head then
return
end
Expand All @@ -165,6 +178,7 @@ function Proxy:process_command()
self.request_queue_head = self.request_queue_head + 1
local req_id, _, cmd = command[1]:match('(%d+) ([a-z-]+) (.+)')
self.current_request = {tonumber(req_id), cmd, addr}
log.info({"Send request", cmd = cmd})
-- \r\n for win32
vim.fn.chansend(self.job_id, cmd .. cmd_nl)
self.request_timer:start(500, 0, vim.schedule_wrap(function()
Expand All @@ -180,6 +194,7 @@ function Proxy:process_command()
end

function Proxy:send_response(req_id, response, addr)
log.debug({"Proxy:send_response", req_id = req_id, response = response, addr = addr})
local data = vim.fn.json_encode({request = req_id, response = response})
self.sock:send(data, addr.ip, addr.port, function(err)
assert(not err, err)
Expand Down
2 changes: 1 addition & 1 deletion lib/proxy/pdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package.path = dir .. '/?.lua;' .. dir .. '/../../lua/?.lua;' .. package.path

local log = require'nvimgdb.log'
log.set_filename('pdb.log')
local Proxy = require'base'
local Proxy = require'impl'

local proxy = Proxy.new('[\n\r]%(Pdb%+*%) *')
proxy:start()
Expand Down

0 comments on commit 767df8d

Please sign in to comment.