Skip to content

Commit b6d247e

Browse files
committed
feat: add logging
1 parent 079d0f3 commit b6d247e

12 files changed

Lines changed: 250 additions & 20 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ require("dap-vscode-js").setup({
5555
-- debugger_path = "(runtimedir)/site/pack/packer/opt/vscode-js-debug", -- Path to vscode-js-debug installation.
5656
-- debugger_cmd = { "js-debug-adapter" }, -- Command to use to launch the debug server. Takes precedence over `node_path` and `debugger_path`.
5757
adapters = { 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost' }, -- which adapters to register in nvim-dap
58+
-- log_file_path = "(stdpath cache)/dap_vscode_js.log" -- Path for file logging
59+
-- log_file_level = false -- Logging level for output to file. Set to false to disable file logging.
60+
-- log_console_level = vim.log.levels.ERROR -- Logging level for output to console. Set to false to disable console output.
5861
})
5962

6063
for _, language in ipairs({ "typescript", "javascript" }) do

doc/nvim-dap-vscode-js.txt

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
================================================================================
2-
*dap-vscode-js*
2+
DAP-VSCODE-JS *dap-vscode-js*
33

44
Settings *Settings*
55
Plugin configuration options
66

77
Fields: ~
8-
{node_path} (string) Path of node executable. Defaults to
9-
$NODE_PATH, and then "node"
10-
{debugger_path} (string) Path to vscode-js-debug. Defaults to
11-
(runtimedir)/site/pack/packer/opt/vscode-js-debug
12-
{adapters} (string[]) List of adapters to configure. Options are
13-
'pwa-node', 'pwa-chrome', 'pwa-msedge',
14-
'node-terminal', 'pwa-extensionHost'.
15-
Defaults to all. See
16-
https://github.com/microsoft/vscode-js-debug/blob/main/OPTIONS.md
17-
for configuration options.
8+
{node_path} (string) Path of node executable. Defaults to
9+
$NODE_PATH, and then "node"
10+
{debugger_path} (string) Path to vscode-js-debug. Defaults to
11+
(runtimedir)/site/pack/packer/opt/vscode-js-debug
12+
{debugger_cmd} (string[]) The command to use to launch the debug
13+
server. This option takes precedence
14+
over both `node_path` and
15+
`debugger_path`.
16+
{adapters} (string[]) List of adapters to configure. Options
17+
are 'pwa-node', 'pwa-chrome',
18+
'pwa-msedge', 'node-terminal',
19+
'pwa-extensionHost'. Defaults to all.
20+
See
21+
https://github.com/microsoft/vscode-js-debug/blob/main/OPTIONS.md
22+
for configuration options.
23+
{log_file_path} (string) Log file path. Defaults to (stdpath
24+
cache)/dap_vscode_js.log
25+
{log_file_level} (number) Logging level for output to file. Set
26+
to false to disable file logging.
27+
Default is false.
28+
{log_console_level} (number) Logging level for output to console.
29+
Set to false to disable console
30+
output. Default is
31+
vim.log.levels.ERROR.
1832

1933

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

2337

2438
Parameters: ~
25-
{settings} (string)
39+
{settings} (Settings)
2640

2741

2842

lua/dap-vscode-js/adapter.lua

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ local function adapter_config(port, mode, proc, start_child)
1414
id = mode,
1515
reverse_request_handlers = {
1616
attachedChildSession = function(parent, request)
17+
logger.debug(
18+
string.format(
19+
"Got attachedChildSession request from port %d to start port %s",
20+
parent.adapter.port,
21+
request.arguments.config.__jsDebugChildServer
22+
)
23+
)
24+
logger.trace("attachedChildSession request, port " .. tostring(port) .. ": " .. vim.inspect(request))
25+
1726
start_child(request, mode, parent, proc)
1827
end,
1928
},
@@ -32,6 +41,8 @@ local function start_child_session(request, mode, parent, proc)
3241
if err then
3342
logger.log("DAP connection failed to start: " .. err, vim.log.levels.ERROR)
3443
else
44+
logger.debug("Initializing child session on port " .. tostring(child_port))
45+
3546
session:initialize(body.config)
3647

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

4960
proc = utils.start_debugger(config, function(port, proc)
61+
logger.debug("Debugger process started on port " .. port)
62+
5063
js_session.register_port(port)
5164
callback(adapter_config(port, mode, proc, start_child_session))
5265
end, function(code, signal)
5366
if code and code ~= 0 then
54-
logger.log("JS Debugger exited with code " .. code .. "!", vim.log.levels.ERROR)
67+
logger.error("JS Debugger exited with code " .. code .. "!")
5568
end
5669
end, function(err)
57-
logger.log("Error trying to launch JS debugger: " .. err, vim.log.levels.ERROR)
70+
logger.error("Error trying to launch JS debugger: " .. err)
5871
end, function(chunk)
59-
logger.log("JS Debugger stderr: " .. chunk, vim.log.levels.ERROR)
72+
-- logger.log("JS Debugger stderr: " .. chunk, vim.log.levels.ERROR)
73+
logger.error("JS Debugger stderr: " .. chunk)
6074
end)
6175
end
6276
end

lua/dap-vscode-js/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local defaults = {
44
node_path = os.getenv("NODE_PATH") or "node",
55
debugger_path = utils.join_paths(utils.get_runtime_dir(), "site/pack/packer/opt/vscode-js-debug"),
66
debugger_cmd = nil,
7+
log_file_path = utils.join_paths(utils.get_cache_dir(), "dap_vscode_js.log"),
8+
log_file_level = false,
9+
log_console_level = vim.log.levels.WARN,
710
}
811

912
local config = vim.deepcopy(defaults)

lua/dap-vscode-js/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
---@class Settings @Plugin configuration options
44
---@field node_path string: Path of node executable. Defaults to $NODE_PATH, and then "node"
55
---@field debugger_path string: Path to vscode-js-debug. Defaults to (runtimedir)/site/pack/packer/opt/vscode-js-debug
6-
---@field debugger_cmd string[]?: The command to use to launch the debug server. This option takes precedence over both `node_path` and `debugger_path`.
6+
---@field debugger_cmd string[]: The command to use to launch the debug server. This option takes precedence over both `node_path` and `debugger_path`.
77
---@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.
8+
---@field log_file_path string: Log file path. Defaults to (stdpath cache)/dap_vscode_js.log
9+
---@field log_file_level number: Logging level for output to file. Set to false to disable file logging. Default is false.
10+
---@field log_console_level number: Logging level for output to console. Set to false to disable console output. Default is vim.log.levels.ERROR.
811

912
local config = require("dap-vscode-js.config")
1013
local js_session = require("dap-vscode-js.session")
1114
local js_dap = require("dap-vscode-js.dap")
15+
local logger = require("dap-vscode-js.log")
1216

1317
local dapjs = {}
1418

@@ -19,6 +23,8 @@ function dapjs.setup(settings, force)
1923
config.__set_config(settings, force or true)
2024
js_session.setup_hooks("dap-vscode-js", config)
2125
js_dap.attach_adapters(config)
26+
27+
logger.debug("Plugin initialized!")
2228
end
2329

2430
return dapjs

lua/dap-vscode-js/log.lua

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,68 @@
11
local M = {}
22

3-
M.log = function(msg, level)
4-
vim.notify(msg, level)
3+
local config = require("dap-vscode-js.config")
4+
5+
local reverse_log_levels = {}
6+
7+
for key, value in pairs(vim.log.levels) do
8+
reverse_log_levels[value] = key
9+
end
10+
11+
M.msg_prefix = ""
12+
13+
M.log = function(msg, level, reflect_depth)
14+
reflect_depth = reflect_depth or 2
15+
16+
msg = M.msg_prefix .. msg
17+
18+
if config.log_file_level and level >= config.log_file_level and config.log_file_path then
19+
local fp, err = io.open(config.log_file_path, "a")
20+
if not fp then
21+
print(err)
22+
return
23+
end
24+
25+
local info = debug.getinfo(reflect_depth, "Sl")
26+
local lineinfo = info.short_src .. ":" .. info.currentline
27+
28+
local str = string.format(
29+
"[%-6s%s %s] %s: %s\n",
30+
reverse_log_levels[level],
31+
os.date(),
32+
vim.loop.hrtime(),
33+
lineinfo,
34+
msg
35+
)
36+
37+
fp:write(str)
38+
fp:close()
39+
end
40+
41+
if config.log_console_level and level >= config.log_console_level then
42+
vim.schedule(function ()
43+
vim.notify(string.format("[dap-js] %s", msg), level)
44+
end)
45+
end
46+
end
47+
48+
M.trace = function(msg, ...)
49+
return M.log(msg, vim.log.levels.TRACE, ...)
50+
end
51+
52+
M.info = function(msg, ...)
53+
return M.log(msg, vim.log.levels.INFO, ...)
54+
end
55+
56+
M.debug = function(msg, ...)
57+
return M.log(msg, vim.log.levels.DEBUG, ...)
58+
end
59+
60+
M.error = function(msg, ...)
61+
return M.log(msg, vim.log.levels.ERROR, ...)
62+
end
63+
64+
M.warn = function(msg, ...)
65+
return M.log(msg, vim.log.levels.WARN, ...)
566
end
667

768
return M

lua/dap-vscode-js/session.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
11
local M = {}
2+
23
local utils = require("dap-vscode-js.utils")
34
local dap_breakpoints = require("dap.breakpoints")
45
local dap = require("dap")
56
local dap_utils = require("dap.utils")
67
local dap_bp_ns = "dap_breakpoints"
8+
local logger = require("dap-vscode-js.log")
79

810
local sessions = {}
911

1012
local breakpoints = {}
1113

1214
local root_ports = {}
1315

16+
local function session_log(session, msg, level, reflect_depth)
17+
reflect_depth = reflect_depth or 3
18+
19+
local port = (session.adapter and tostring(session.adapter.port)) or "???"
20+
21+
local is_main = dap.session() == session
22+
23+
logger.log(string.format("(%s%s) %s", port, (is_main and "*") or "", msg), level, reflect_depth)
24+
end
25+
26+
local function session_debug(session, msg)
27+
session_log(session, msg, vim.log.levels.DEBUG, 4)
28+
end
29+
30+
local function session_trace(session, msg)
31+
session_log(session, msg, vim.log.levels.TRACE, 4)
32+
end
33+
1434
function M.register_port(port)
1535
root_ports[port] = true
36+
logger.debug("Registered root port " .. port)
1637
end
1738

1839
function M.unregister_port(port)
1940
root_ports[port] = false
41+
logger.debug("Unregistered root port " .. port)
2042
end
2143

2244
function M.register_session(session, parent, proc)
45+
session_debug(session, "Registering session")
46+
2347
dap.set_session(session)
48+
session_debug(session, "Set as main dap session")
2449

2550
sessions[session] = {
2651
parent = parent,
@@ -82,6 +107,10 @@ function M.setup_hooks(plugin_id, config)
82107
end
83108

84109
if not root_ports[session.adapter.port] then
110+
session_debug(session, "Received setBreakpoints response on root port")
111+
session_trace(session, "setBreakpoints body: " .. vim.inspect(body))
112+
session_trace(session, "setBreakpoints request: " .. vim.inspect(request))
113+
85114
return
86115
end
87116

@@ -117,6 +146,8 @@ function M.setup_hooks(plugin_id, config)
117146
register_listener("before", "event_continued", plugin_id, function(session, info, body)
118147
for _, bp in ipairs(get_breakpoints(info.pid)) do
119148
if bp.__verified == false then
149+
session_debug("Rejecting breakpoint #" .. tostring(bp.id))
150+
120151
local bp_info = utils.dap_breakpoint_by_state(bp)
121152

122153
if bp_info then

lua/dap-vscode-js/utils.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function M.get_runtime_dir()
1313
return lvim_runtime_dir
1414
end
1515

16+
function M.get_cache_dir()
17+
return vim.call("stdpath", "cache")
18+
end
19+
1620
function M.join_paths(...)
1721
local result = table.concat({ ... }, path_sep)
1822
return result

scripts/test

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
#!/bin/bash
22

33
TIMEOUT=2000
4+
DAP_JS_ENABLE_LOGGING=false
45

5-
while getopts ":t:" options; do
6+
while getopts ":t:l" options; do
67
case "${options}" in
78
t)
89
if [ -n "${OPTARG}" ]; then
910
TIMEOUT=${OPTARG}
1011
fi
1112
;;
13+
l)
14+
LOG=true
15+
;;
1216
esac
1317
done
1418

1519
export PLENARY_TEST_TIMEOUT=$TIMEOUT
20+
export DAP_JS_ENABLE_LOGGING=$LOG
1621

1722
shift $(($OPTIND - 1))
1823

24+
rm "./lib/dap-vscode-js.log"
25+
1926
tempfile=".test_output.tmp"
2027

2128
if [[ -n $1 ]]; then

tests/init.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ set rtp+=./tests
1010

1111
runtime! plugin/plenary.vim
1212
lua DEBUGGER_PATH="./lib/vscode-js-debug"
13+
lua LOG_PATH="./lib/dap-vscode-js.log"

0 commit comments

Comments
 (0)