Skip to content

Commit cec5d46

Browse files
knightburtonyichoi
authored andcommitted
Add debug session request, response and event logs (#39)
IoT.js-Debug-DCO-1.0-Signed-off-by: Imre Kiss [email protected]
1 parent 52c4809 commit cec5d46

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

src/IotjsDebugger.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { DebugProtocol } from 'vscode-debugprotocol';
2424
import * as Fs from 'fs';
2525
import * as Path from 'path';
26+
import * as Util from 'util';
2627
import { IAttachRequestArguments, SourceSendingOptions, TemporaryBreakpoint } from './IotjsDebuggerInterfaces';
2728
import { JerryDebuggerClient, JerryDebuggerOptions } from './JerryDebuggerClient';
2829
import {
@@ -69,8 +70,6 @@ class IotjsDebugSession extends DebugSession {
6970
protected initializeRequest(
7071
response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments
7172
): void {
72-
this.log('initializeRequest', LOG_LEVEL.SESSION);
73-
7473
// This debug adapter implements the configurationDoneRequest.
7574
response.body.supportsConfigurationDoneRequest = true;
7675
response.body.supportsFunctionBreakpoints = false;
@@ -90,8 +89,6 @@ class IotjsDebugSession extends DebugSession {
9089
protected configurationDoneRequest(
9190
response: DebugProtocol.ConfigurationDoneResponse, args: DebugProtocol.ConfigurationDoneArguments
9291
): void {
93-
this.log('configurationDoneRequest', LOG_LEVEL.SESSION);
94-
9592
super.configurationDoneRequest(response, args);
9693
}
9794

@@ -118,7 +115,6 @@ class IotjsDebugSession extends DebugSession {
118115
} else {
119116
this.sendErrorResponse(response, new Error('No log level given'));
120117
}
121-
this.log('attachRequest');
122118

123119
const protocolDelegate = <JerryDebugProtocolDelegate>{
124120
onBreakpointHit: (ref: JerryMessageBreakpointHit, type: string) => this.onBreakpointHit(ref, type),
@@ -128,7 +124,8 @@ class IotjsDebugSession extends DebugSession {
128124
};
129125

130126
this._protocolhandler = new JerryDebugProtocolHandler(
131-
protocolDelegate, message => this.log(message, LOG_LEVEL.SESSION));
127+
protocolDelegate, (message: any, level: number = LOG_LEVEL.VERBOSE) => this.log(message, level)
128+
);
132129
this._debuggerClient = new JerryDebuggerClient(<JerryDebuggerOptions>{
133130
delegate: {
134131
onMessage: (message: Uint8Array) => this._protocolhandler.onMessage(message),
@@ -154,16 +151,12 @@ class IotjsDebugSession extends DebugSession {
154151
}
155152

156153
protected launchRequest(response: DebugProtocol.LaunchResponse, args: DebugProtocol.LaunchRequestArguments): void {
157-
this.log('launchRequest', LOG_LEVEL.SESSION);
158-
159154
this.sendErrorResponse(response, new Error('Launching is not supported. Use Attach.'));
160155
}
161156

162157
protected disconnectRequest(
163158
response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments
164159
): void {
165-
this.log('disconnectRequest', LOG_LEVEL.SESSION);
166-
167160
this._debuggerClient.disconnect();
168161

169162
this.sendEvent(new TerminatedEvent());
@@ -177,8 +170,6 @@ class IotjsDebugSession extends DebugSession {
177170
}
178171

179172
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
180-
this.log('continueRequest', LOG_LEVEL.SESSION);
181-
182173
this._protocolhandler.resume()
183174
.then(() => {
184175
this.sendResponse(response);
@@ -187,8 +178,6 @@ class IotjsDebugSession extends DebugSession {
187178
}
188179

189180
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
190-
this.log('nextRequest', LOG_LEVEL.SESSION);
191-
192181
this._protocolhandler.stepOver()
193182
.then(() => {
194183
this.sendResponse(response);
@@ -197,8 +186,6 @@ class IotjsDebugSession extends DebugSession {
197186
}
198187

199188
protected stepInRequest(response: DebugProtocol.StepInResponse, args: DebugProtocol.StepInArguments): void {
200-
this.log('stepInRequest', LOG_LEVEL.SESSION);
201-
202189
this._protocolhandler.stepInto()
203190
.then(() => {
204191
this.sendResponse(response);
@@ -207,8 +194,6 @@ class IotjsDebugSession extends DebugSession {
207194
}
208195

209196
protected stepOutRequest(response: DebugProtocol.StepOutResponse, args: DebugProtocol.StepOutArguments): void {
210-
this.log('stepOutRequest', LOG_LEVEL.SESSION);
211-
212197
this._protocolhandler.stepOut()
213198
.then(() => {
214199
this.sendResponse(response);
@@ -217,8 +202,6 @@ class IotjsDebugSession extends DebugSession {
217202
}
218203

219204
protected pauseRequest(response: DebugProtocol.PauseResponse, args: DebugProtocol.PauseArguments): void {
220-
this.log('pauseRequest', LOG_LEVEL.SESSION);
221-
222205
this._protocolhandler.pause()
223206
.then(() => {
224207
this.sendResponse(response);
@@ -229,8 +212,6 @@ class IotjsDebugSession extends DebugSession {
229212
protected async setBreakPointsRequest(
230213
response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments
231214
): Promise<void> {
232-
this.log('setBreakPointsRequest', LOG_LEVEL.SESSION);
233-
234215
const filename: string = args.source.name;
235216
const vscodeBreakpoints: DebugProtocol.Breakpoint[] = args.breakpoints!.map(b => ({verified: false, line: b.line}));
236217

@@ -281,8 +262,6 @@ class IotjsDebugSession extends DebugSession {
281262
protected async evaluateRequest(
282263
response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments
283264
): Promise<void> {
284-
this.log('evaluateRequest', LOG_LEVEL.SESSION);
285-
286265
try {
287266
const result: JerryEvalResult = await this._protocolhandler.evaluate(args.expression);
288267
const value: string = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK
@@ -303,8 +282,7 @@ class IotjsDebugSession extends DebugSession {
303282
protected async stackTraceRequest(
304283
response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments
305284
): Promise<void> {
306-
this.log('stackTraceRequest', LOG_LEVEL.SESSION);
307-
285+
this.log(args, LOG_LEVEL.SESSION);
308286
try {
309287
const backtrace = await this._protocolhandler.requestBacktrace();
310288
const stk = backtrace.map((f, i) => new StackFrame(
@@ -328,8 +306,6 @@ class IotjsDebugSession extends DebugSession {
328306
}
329307

330308
protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
331-
this.log('customRequest', LOG_LEVEL.SESSION);
332-
333309
switch (command) {
334310
case 'sendSource': {
335311
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.IN_PROGRESS;
@@ -352,6 +328,28 @@ class IotjsDebugSession extends DebugSession {
352328
}
353329

354330
// Overrides.
331+
protected dispatchRequest(request: DebugProtocol.Request): void {
332+
const log = `-> ${request.command}Request\n${Util.inspect(request, { depth: Infinity })}\n`;
333+
this.log(log, LOG_LEVEL.SESSION);
334+
335+
super.dispatchRequest(request);
336+
}
337+
338+
public sendResponse(response: DebugProtocol.Response): void {
339+
const log = `<- ${response.command}Response\n${Util.inspect(response, { depth: Infinity })}\n`;
340+
this.log(log, LOG_LEVEL.SESSION);
341+
342+
super.sendResponse(response);
343+
}
344+
345+
public sendEvent(event: DebugProtocol.Event, bypassLog: boolean = false): void {
346+
if (!bypassLog) {
347+
const log = `<- ${event.event}Event\n${Util.inspect(event, { depth: Infinity })}\n`;
348+
this.log(log, LOG_LEVEL.SESSION);
349+
}
350+
351+
super.sendEvent(event);
352+
}
355353

356354
protected sendErrorResponse(
357355
response: DebugProtocol.Response,
@@ -391,25 +389,25 @@ class IotjsDebugSession extends DebugSession {
391389
// Helper functions for event handling
392390

393391
private onBreakpointHit(breakpointRef: JerryMessageBreakpointHit, stopType: string): void {
394-
this.log('onBreakpointHit');
392+
this.log('onBreakpointHit', LOG_LEVEL.SESSION);
395393

396394
this.sendEvent(new StoppedEvent(stopType, IotjsDebugSession.THREAD_ID));
397395
}
398396

399397
private onExceptionHit(data: JerryMessageExceptionHit): void {
400-
this.log('onExceptionHit');
398+
this.log('onExceptionHit', LOG_LEVEL.SESSION);
401399

402400
this.sendEvent(new StoppedEvent('exception', IotjsDebugSession.THREAD_ID, data.message));
403401
}
404402

405403
private onScriptParsed(data: JerryMessageScriptParsed): void {
406-
this.log('onScriptParsed');
404+
this.log('onScriptParsed', LOG_LEVEL.SESSION);
407405

408406
this.handleSource(data);
409407
}
410408

411409
private async onWaitForSource(): Promise<void> {
412-
this.log('onWaitForSource');
410+
this.log('onWaitForSource', LOG_LEVEL.SESSION);
413411

414412
if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.NOP) {
415413
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.WAITING;
@@ -423,7 +421,7 @@ class IotjsDebugSession extends DebugSession {
423421
}
424422

425423
private onClose(): void {
426-
this.log('onClose');
424+
this.log('onClose', LOG_LEVEL.SESSION);
427425

428426
this.sendEvent(new TerminatedEvent());
429427
}
@@ -463,14 +461,14 @@ class IotjsDebugSession extends DebugSession {
463461
if (level === this._debugLog || this._debugLog === LOG_LEVEL.VERBOSE) {
464462
switch (typeof message) {
465463
case 'object':
466-
message = JSON.stringify(message, null, 2);
464+
message = Util.inspect(message, { depth: Infinity });
467465
break;
468466
default:
469467
message = message.toString();
470468
break;
471469
}
472470

473-
this.sendEvent(new OutputEvent(`[DS] ${message}\n`, 'console'));
471+
this.sendEvent(new OutputEvent(`[${LOG_LEVEL[level]}] ${message}\n`, 'console'), true);
474472
}
475473
}
476474
}

src/JerryProtocolHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ export class JerryDebugProtocolHandler {
727727
logPacket(description: string, ignorable: boolean = false) {
728728
// certain packets are ignored while evals are pending
729729
const ignored = (ignorable && this.evalsPending) ? 'Ignored: ' : '';
730-
this.log(`[Protocol Handler] ${ignored}${description}`, LOG_LEVEL.PROTOCOL);
730+
this.log(`${ignored}${description}`, LOG_LEVEL.PROTOCOL);
731731
}
732732

733733
private abort(message: string) {

0 commit comments

Comments
 (0)