Skip to content

Commit

Permalink
feat: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Nov 12, 2022
1 parent 079d0f3 commit b6d247e
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ require("dap-vscode-js").setup({
-- debugger_path = "(runtimedir)/site/pack/packer/opt/vscode-js-debug", -- Path to vscode-js-debug installation.
-- debugger_cmd = { "js-debug-adapter" }, -- Command to use to launch the debug server. Takes precedence over `node_path` and `debugger_path`.
adapters = { 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost' }, -- which adapters to register in nvim-dap
-- log_file_path = "(stdpath cache)/dap_vscode_js.log" -- Path for file logging
-- log_file_level = false -- Logging level for output to file. Set to false to disable file logging.
-- log_console_level = vim.log.levels.ERROR -- Logging level for output to console. Set to false to disable console output.
})

for _, language in ipairs({ "typescript", "javascript" }) do
Expand Down
38 changes: 26 additions & 12 deletions doc/nvim-dap-vscode-js.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
================================================================================
*dap-vscode-js*
DAP-VSCODE-JS *dap-vscode-js*

Settings *Settings*
Plugin configuration options

Fields: ~
{node_path} (string) Path of node executable. Defaults to
$NODE_PATH, and then "node"
{debugger_path} (string) Path to vscode-js-debug. Defaults to
(runtimedir)/site/pack/packer/opt/vscode-js-debug
{adapters} (string[]) List of adapters to configure. Options are
'pwa-node', 'pwa-chrome', 'pwa-msedge',
'node-terminal', 'pwa-extensionHost'.
Defaults to all. See
https://github.com/microsoft/vscode-js-debug/blob/main/OPTIONS.md
for configuration options.
{node_path} (string) Path of node executable. Defaults to
$NODE_PATH, and then "node"
{debugger_path} (string) Path to vscode-js-debug. Defaults to
(runtimedir)/site/pack/packer/opt/vscode-js-debug
{debugger_cmd} (string[]) The command to use to launch the debug
server. This option takes precedence
over both `node_path` and
`debugger_path`.
{adapters} (string[]) List of adapters to configure. Options
are 'pwa-node', 'pwa-chrome',
'pwa-msedge', 'node-terminal',
'pwa-extensionHost'. Defaults to all.
See
https://github.com/microsoft/vscode-js-debug/blob/main/OPTIONS.md
for configuration options.
{log_file_path} (string) Log file path. Defaults to (stdpath
cache)/dap_vscode_js.log
{log_file_level} (number) Logging level for output to file. Set
to false to disable file logging.
Default is false.
{log_console_level} (number) Logging level for output to console.
Set to false to disable console
output. Default is
vim.log.levels.ERROR.


dapjs.setup({settings}) *dapjs.setup()*
Setup adapter and/or configs


Parameters: ~
{settings} (string)
{settings} (Settings)



Expand Down
20 changes: 17 additions & 3 deletions lua/dap-vscode-js/adapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ local function adapter_config(port, mode, proc, start_child)
id = mode,
reverse_request_handlers = {
attachedChildSession = function(parent, request)
logger.debug(
string.format(
"Got attachedChildSession request from port %d to start port %s",
parent.adapter.port,
request.arguments.config.__jsDebugChildServer
)
)
logger.trace("attachedChildSession request, port " .. tostring(port) .. ": " .. vim.inspect(request))

start_child(request, mode, parent, proc)
end,
},
Expand All @@ -32,6 +41,8 @@ local function start_child_session(request, mode, parent, proc)
if err then
logger.log("DAP connection failed to start: " .. err, vim.log.levels.ERROR)
else
logger.debug("Initializing child session on port " .. tostring(child_port))

session:initialize(body.config)

js_session.register_session(session, parent, proc)
Expand All @@ -47,16 +58,19 @@ function M.generate_adapter(mode, config)
local proc

proc = utils.start_debugger(config, function(port, proc)
logger.debug("Debugger process started on port " .. port)

js_session.register_port(port)
callback(adapter_config(port, mode, proc, start_child_session))
end, function(code, signal)
if code and code ~= 0 then
logger.log("JS Debugger exited with code " .. code .. "!", vim.log.levels.ERROR)
logger.error("JS Debugger exited with code " .. code .. "!")
end
end, function(err)
logger.log("Error trying to launch JS debugger: " .. err, vim.log.levels.ERROR)
logger.error("Error trying to launch JS debugger: " .. err)
end, function(chunk)
logger.log("JS Debugger stderr: " .. chunk, vim.log.levels.ERROR)
-- logger.log("JS Debugger stderr: " .. chunk, vim.log.levels.ERROR)
logger.error("JS Debugger stderr: " .. chunk)
end)
end
end
Expand Down
3 changes: 3 additions & 0 deletions lua/dap-vscode-js/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local defaults = {
node_path = os.getenv("NODE_PATH") or "node",
debugger_path = utils.join_paths(utils.get_runtime_dir(), "site/pack/packer/opt/vscode-js-debug"),
debugger_cmd = nil,
log_file_path = utils.join_paths(utils.get_cache_dir(), "dap_vscode_js.log"),
log_file_level = false,
log_console_level = vim.log.levels.WARN,
}

local config = vim.deepcopy(defaults)
Expand Down
8 changes: 7 additions & 1 deletion lua/dap-vscode-js/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
---@class Settings @Plugin configuration options
---@field node_path string: Path of node executable. Defaults to $NODE_PATH, and then "node"
---@field debugger_path string: Path to vscode-js-debug. Defaults to (runtimedir)/site/pack/packer/opt/vscode-js-debug
---@field debugger_cmd string[]?: The command to use to launch the debug server. This option takes precedence over both `node_path` and `debugger_path`.
---@field debugger_cmd string[]: The command to use to launch the debug server. This option takes precedence over both `node_path` and `debugger_path`.
---@field adapters string[]: List of adapters to configure. Options are 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost'. Defaults to all. See https://github.com/microsoft/vscode-js-debug/blob/main/OPTIONS.md for configuration options.
---@field log_file_path string: Log file path. Defaults to (stdpath cache)/dap_vscode_js.log
---@field log_file_level number: Logging level for output to file. Set to false to disable file logging. Default is false.
---@field log_console_level number: Logging level for output to console. Set to false to disable console output. Default is vim.log.levels.ERROR.

local config = require("dap-vscode-js.config")
local js_session = require("dap-vscode-js.session")
local js_dap = require("dap-vscode-js.dap")
local logger = require("dap-vscode-js.log")

local dapjs = {}

Expand All @@ -19,6 +23,8 @@ function dapjs.setup(settings, force)
config.__set_config(settings, force or true)
js_session.setup_hooks("dap-vscode-js", config)
js_dap.attach_adapters(config)

logger.debug("Plugin initialized!")
end

return dapjs
65 changes: 63 additions & 2 deletions lua/dap-vscode-js/log.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,68 @@
local M = {}

M.log = function(msg, level)
vim.notify(msg, level)
local config = require("dap-vscode-js.config")

local reverse_log_levels = {}

for key, value in pairs(vim.log.levels) do
reverse_log_levels[value] = key
end

M.msg_prefix = ""

M.log = function(msg, level, reflect_depth)
reflect_depth = reflect_depth or 2

msg = M.msg_prefix .. msg

if config.log_file_level and level >= config.log_file_level and config.log_file_path then
local fp, err = io.open(config.log_file_path, "a")
if not fp then
print(err)
return
end

local info = debug.getinfo(reflect_depth, "Sl")
local lineinfo = info.short_src .. ":" .. info.currentline

local str = string.format(
"[%-6s%s %s] %s: %s\n",
reverse_log_levels[level],
os.date(),
vim.loop.hrtime(),
lineinfo,
msg
)

fp:write(str)
fp:close()
end

if config.log_console_level and level >= config.log_console_level then
vim.schedule(function ()
vim.notify(string.format("[dap-js] %s", msg), level)
end)
end
end

M.trace = function(msg, ...)
return M.log(msg, vim.log.levels.TRACE, ...)
end

M.info = function(msg, ...)
return M.log(msg, vim.log.levels.INFO, ...)
end

M.debug = function(msg, ...)
return M.log(msg, vim.log.levels.DEBUG, ...)
end

M.error = function(msg, ...)
return M.log(msg, vim.log.levels.ERROR, ...)
end

M.warn = function(msg, ...)
return M.log(msg, vim.log.levels.WARN, ...)
end

return M
31 changes: 31 additions & 0 deletions lua/dap-vscode-js/session.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
local M = {}

local utils = require("dap-vscode-js.utils")
local dap_breakpoints = require("dap.breakpoints")
local dap = require("dap")
local dap_utils = require("dap.utils")
local dap_bp_ns = "dap_breakpoints"
local logger = require("dap-vscode-js.log")

local sessions = {}

local breakpoints = {}

local root_ports = {}

local function session_log(session, msg, level, reflect_depth)
reflect_depth = reflect_depth or 3

local port = (session.adapter and tostring(session.adapter.port)) or "???"

local is_main = dap.session() == session

logger.log(string.format("(%s%s) %s", port, (is_main and "*") or "", msg), level, reflect_depth)
end

local function session_debug(session, msg)
session_log(session, msg, vim.log.levels.DEBUG, 4)
end

local function session_trace(session, msg)
session_log(session, msg, vim.log.levels.TRACE, 4)
end

function M.register_port(port)
root_ports[port] = true
logger.debug("Registered root port " .. port)
end

function M.unregister_port(port)
root_ports[port] = false
logger.debug("Unregistered root port " .. port)
end

function M.register_session(session, parent, proc)
session_debug(session, "Registering session")

dap.set_session(session)
session_debug(session, "Set as main dap session")

sessions[session] = {
parent = parent,
Expand Down Expand Up @@ -82,6 +107,10 @@ function M.setup_hooks(plugin_id, config)
end

if not root_ports[session.adapter.port] then
session_debug(session, "Received setBreakpoints response on root port")
session_trace(session, "setBreakpoints body: " .. vim.inspect(body))
session_trace(session, "setBreakpoints request: " .. vim.inspect(request))

return
end

Expand Down Expand Up @@ -117,6 +146,8 @@ function M.setup_hooks(plugin_id, config)
register_listener("before", "event_continued", plugin_id, function(session, info, body)
for _, bp in ipairs(get_breakpoints(info.pid)) do
if bp.__verified == false then
session_debug("Rejecting breakpoint #" .. tostring(bp.id))

local bp_info = utils.dap_breakpoint_by_state(bp)

if bp_info then
Expand Down
4 changes: 4 additions & 0 deletions lua/dap-vscode-js/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function M.get_runtime_dir()
return lvim_runtime_dir
end

function M.get_cache_dir()
return vim.call("stdpath", "cache")
end

function M.join_paths(...)
local result = table.concat({ ... }, path_sep)
return result
Expand Down
9 changes: 8 additions & 1 deletion scripts/test
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
#!/bin/bash

TIMEOUT=2000
DAP_JS_ENABLE_LOGGING=false

while getopts ":t:" options; do
while getopts ":t:l" options; do
case "${options}" in
t)
if [ -n "${OPTARG}" ]; then
TIMEOUT=${OPTARG}
fi
;;
l)
LOG=true
;;
esac
done

export PLENARY_TEST_TIMEOUT=$TIMEOUT
export DAP_JS_ENABLE_LOGGING=$LOG

shift $(($OPTIND - 1))

rm "./lib/dap-vscode-js.log"

tempfile=".test_output.tmp"

if [[ -n $1 ]]; then
Expand Down
1 change: 1 addition & 0 deletions tests/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ set rtp+=./tests

runtime! plugin/plenary.vim
lua DEBUGGER_PATH="./lib/vscode-js-debug"
lua LOG_PATH="./lib/dap-vscode-js.log"
Loading

0 comments on commit b6d247e

Please sign in to comment.