Skip to content

Commit 1d545a0

Browse files
knightburtonyichoi
authored andcommitted
Improve logging functionality (#17)
- Make available the debug log inside the protocol handler - Add option to log objects and arrays IoT.js-Debug-DCO-1.0-Signed-off-by: Imre Kiss [email protected]
1 parent 8004621 commit 1d545a0

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/IotjsDebugger.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class IotjsDebugSession extends LoggingDebugSession {
145145
onScriptParsed
146146
};
147147

148-
this._protocolhandler = new JerryDebugProtocolHandler(protocolDelegate);
148+
this._protocolhandler = new JerryDebugProtocolHandler(protocolDelegate, message => this.log(message));
149149
this._debuggerClient = new JerryDebuggerClient(<JerryDebuggerOptions>{
150150
delegate: {
151151
onMessage: (message: Uint8Array) => this._protocolhandler.onMessage(message),
@@ -287,8 +287,17 @@ class IotjsDebugSession extends LoggingDebugSession {
287287

288288
}
289289

290-
private log(message: string): void {
290+
private log(message: any): void {
291291
if (this._debugLog) {
292+
switch (typeof message) {
293+
case 'object':
294+
message = JSON.stringify(message, null, 2);
295+
break;
296+
default:
297+
message = message.toString();
298+
break;
299+
}
300+
292301
this.sendEvent(new OutputEvent(`[DS] ${message}\n`, 'console'));
293302
}
294303
}

src/JerryProtocolHandler.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { JerryDebuggerClient } from './JerryDebuggerClient';
2525

2626
export type CompressedPointer = number;
2727
export type ByteCodeOffset = number;
28+
export type LoggerFunction = (message: any) => void;
2829

2930
export interface ParserStackFrame {
3031
isFunc: boolean;
@@ -121,8 +122,11 @@ export class JerryDebugProtocolHandler {
121122
private nextBreakpointIndex: number = 0;
122123
private waitForSourceEnabled: boolean = false;
123124

124-
constructor(delegate: JerryDebugProtocolDelegate) {
125+
private log: LoggerFunction;
126+
127+
constructor(delegate: JerryDebugProtocolDelegate, log?: LoggerFunction) {
125128
this.delegate = delegate;
129+
this.log = log || <any>(() => {});
126130

127131
this.byteConfig = {
128132
cpointerSize: 0,
@@ -448,9 +452,9 @@ export class JerryDebugProtocolHandler {
448452
const breakpoint = breakpointRef.breakpoint;
449453

450454
if (data[0] === SP.SERVER.JERRY_DEBUGGER_EXCEPTION_HIT) {
451-
console.log('Exception throw detected');
455+
this.log('Exception throw detected');
452456
if (this.exceptionData) {
453-
console.log('Exception hint:', cesu8ToString(this.exceptionData));
457+
this.log(`Exception hint: ${cesu8ToString(this.exceptionData)}`);
454458
this.exceptionData = undefined;
455459
}
456460
}
@@ -464,7 +468,7 @@ export class JerryDebugProtocolHandler {
464468
}
465469

466470
const atAround = breakpointRef.exact ? 'at' : 'around';
467-
console.log(`Stopped ${atAround} ${breakpointInfo}${breakpoint}`);
471+
this.log(`Stopped ${atAround} ${breakpointInfo}${breakpoint}`);
468472

469473
// TODO: handle exception case differently
470474
if (this.delegate.onBreakpointHit) {
@@ -615,12 +619,12 @@ export class JerryDebugProtocolHandler {
615619
logPacket(description: string, ignorable: boolean = false) {
616620
// certain packets are ignored while evals are pending
617621
const ignored = (ignorable && this.evalsPending) ? 'Ignored: ' : '';
618-
console.log(`[${ignored}${description}]`);
622+
this.log(`[Protocol Handler] ${ignored}${description}`);
619623
}
620624

621625
private abort(message: string) {
622626
if (this.delegate.onError) {
623-
console.log('Abort:', message);
627+
this.log(`Abort: ${message}`);
624628
this.delegate.onError(0, message);
625629
}
626630
}

0 commit comments

Comments
 (0)