Skip to content

Commit b12b45a

Browse files
Yuyupoyichoi
authored andcommitted
Log levels set (#38)
IoT.js-Debug-DCO-1.0-Signed-off-by: Daniella Barsony [email protected]
1 parent ac6c767 commit b12b45a

File tree

6 files changed

+83
-51
lines changed

6 files changed

+83
-51
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@
111111
"default": true
112112
},
113113
"debugLog": {
114-
"type": "boolean",
115-
"description": "Allowes to log debug messages to the console.",
116-
"default": false
114+
"type": "number",
115+
"description": "Set log level for the debugger: 0 none, 1 error, 2 session, 3 protocol, 4 verbose",
116+
"default": 0
117117
}
118118
}
119119
}
@@ -130,7 +130,7 @@
130130
"port": 5001,
131131
"localRoot": "${workspaceRoot}",
132132
"stopOnEntry": false,
133-
"debugLog": false
133+
"debugLog": 0
134134
}
135135
}
136136
]

src/IotjsDebugger.ts

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,15 @@ import {
3131
} from './JerryProtocolHandler';
3232
import { EVAL_RESULT_SUBTYPE, CLIENT as CLIENT_PACKAGE } from './JerryProtocolConstants';
3333
import { Breakpoint } from './JerryBreakpoints';
34-
35-
enum SOURCE_SENDING_STATES {
36-
NOP = 0,
37-
WAITING = 1,
38-
IN_PROGRESS = 2,
39-
LAST_SENT = 3
40-
}
34+
import { SOURCE_SENDING_STATES, LOG_LEVEL } from './IotjsDebuggerConstants';
4135

4236
class IotjsDebugSession extends DebugSession {
4337

4438
// We don't support multiple threads, so we can use a hardcoded ID for the default thread
4539
private static THREAD_ID = 1;
4640

4741
private _args: IAttachRequestArguments;
48-
private _debugLog: boolean = false;
42+
private _debugLog: number = 0;
4943
private _debuggerClient: JerryDebuggerClient;
5044
private _protocolhandler: JerryDebugProtocolHandler;
5145
private _sourceSendingOptions: SourceSendingOptions;
@@ -75,7 +69,7 @@ class IotjsDebugSession extends DebugSession {
7569
protected initializeRequest(
7670
response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments
7771
): void {
78-
this.log('initializeRequest');
72+
this.log('initializeRequest', LOG_LEVEL.SESSION);
7973

8074
// This debug adapter implements the configurationDoneRequest.
8175
response.body.supportsConfigurationDoneRequest = true;
@@ -95,14 +89,13 @@ class IotjsDebugSession extends DebugSession {
9589
protected configurationDoneRequest(
9690
response: DebugProtocol.ConfigurationDoneResponse, args: DebugProtocol.ConfigurationDoneArguments
9791
): void {
98-
this.log('configurationDoneRequest');
92+
this.log('configurationDoneRequest', LOG_LEVEL.SESSION);
9993

10094
super.configurationDoneRequest(response, args);
10195
this.sendResponse(response);
10296
}
10397

10498
protected attachRequest(response: DebugProtocol.AttachResponse, args: IAttachRequestArguments): void {
105-
this.log('attachRequest');
10699

107100
if (!args.address || args.address === '') {
108101
this.sendErrorResponse(response, new Error('Must specify an address'));
@@ -120,36 +113,41 @@ class IotjsDebugSession extends DebugSession {
120113
}
121114

122115
this._args = args;
123-
this._debugLog = args.debugLog || false;
116+
if (args.debugLog in LOG_LEVEL) {
117+
this._debugLog = args.debugLog;
118+
} else {
119+
this.sendErrorResponse(response, new Error('No log level given'));
120+
}
121+
this.log('attachRequest');
124122

125123
const onBreakpointHit = (breakpointRef, stopType) => {
126-
this.log('onBreakpointHit');
124+
this.log('onBreakpointHit', LOG_LEVEL.SESSION);
127125
this.sendEvent(new StoppedEvent(stopType, IotjsDebugSession.THREAD_ID));
128126
};
129127

130128
const onExceptionHit = (data: JerryMessageExceptionHit) => {
131-
this.log('onExceptionHit');
129+
this.log('onExceptionHit', LOG_LEVEL.SESSION);
132130
this.sendEvent(new StoppedEvent('exception', IotjsDebugSession.THREAD_ID, data.message));
133131
};
134132

135133
const onResume = () => {
136-
this.log('onResume');
134+
this.log('onResume', LOG_LEVEL.SESSION);
137135

138136
this.sendEvent(new ContinuedEvent(IotjsDebugSession.THREAD_ID));
139137
};
140138

141139
const onScriptParsed = data => {
142-
this.log('onScriptParsed');
140+
this.log('onScriptParsed', LOG_LEVEL.SESSION);
143141
this.handleSource(data);
144142
};
145143

146144
const onClose = () => {
147-
this.log('onClose');
145+
this.log('onClose', LOG_LEVEL.SESSION);
148146
this.sendEvent(new TerminatedEvent());
149147
};
150148

151149
const onWaitForSource = async () => {
152-
this.log('onWaitForSource');
150+
this.log('onWaitForSource', LOG_LEVEL.SESSION);
153151
if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.NOP) {
154152
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.WAITING;
155153
this.sendEvent(new Event('waitForSource'));
@@ -169,7 +167,8 @@ class IotjsDebugSession extends DebugSession {
169167
onWaitForSource
170168
};
171169

172-
this._protocolhandler = new JerryDebugProtocolHandler(protocolDelegate, message => this.log(message));
170+
this._protocolhandler = new JerryDebugProtocolHandler(
171+
protocolDelegate, message => this.log(message, LOG_LEVEL.SESSION));
173172
this._debuggerClient = new JerryDebuggerClient(<JerryDebuggerOptions>{
174173
delegate: {
175174
onMessage: (message: Uint8Array) => this._protocolhandler.onMessage(message),
@@ -182,11 +181,11 @@ class IotjsDebugSession extends DebugSession {
182181

183182
this._debuggerClient.connect()
184183
.then(() => {
185-
this.log(`Connected to: ${args.address}:${args.port}`);
184+
this.log(`Connected to: ${args.address}:${args.port}`, LOG_LEVEL.SESSION);
186185
this.sendResponse(response);
187186
})
188187
.catch(error => {
189-
this.log(error);
188+
this.log(error, LOG_LEVEL.ERROR);
190189
this.sendErrorResponse(response, error);
191190
})
192191
.then(() => {
@@ -195,15 +194,15 @@ class IotjsDebugSession extends DebugSession {
195194
}
196195

197196
protected launchRequest(response: DebugProtocol.LaunchResponse, args: DebugProtocol.LaunchRequestArguments): void {
198-
this.log('launchRequest');
197+
this.log('launchRequest', LOG_LEVEL.SESSION);
199198

200199
this.sendErrorResponse(response, new Error('Launching is not supported. Use Attach.'));
201200
}
202201

203202
protected disconnectRequest(
204203
response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments
205204
): void {
206-
this.log('disconnectRequest');
205+
this.log('disconnectRequest', LOG_LEVEL.SESSION);
207206

208207
this._debuggerClient.disconnect();
209208

@@ -212,13 +211,13 @@ class IotjsDebugSession extends DebugSession {
212211
}
213212

214213
protected restartRequest(response: DebugProtocol.RestartResponse, args: DebugProtocol.RestartArguments): void {
215-
this.log('restartRequest: Not implemented yet');
214+
this.log('restartRequest: Not implemented yet', LOG_LEVEL.SESSION);
216215

217216
this.sendResponse(response);
218217
}
219218

220219
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
221-
this.log('continueRequest');
220+
this.log('continueRequest', LOG_LEVEL.SESSION);
222221

223222
this._protocolhandler.resume()
224223
.then(() => {
@@ -228,7 +227,7 @@ class IotjsDebugSession extends DebugSession {
228227
}
229228

230229
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
231-
this.log('nextRequest');
230+
this.log('nextRequest', LOG_LEVEL.SESSION);
232231

233232
this._protocolhandler.stepOver()
234233
.then(() => {
@@ -238,7 +237,7 @@ class IotjsDebugSession extends DebugSession {
238237
}
239238

240239
protected stepInRequest(response: DebugProtocol.StepInResponse, args: DebugProtocol.StepInArguments): void {
241-
this.log('stepInRequest');
240+
this.log('stepInRequest', LOG_LEVEL.SESSION);
242241

243242
this._protocolhandler.stepInto()
244243
.then(() => {
@@ -248,7 +247,7 @@ class IotjsDebugSession extends DebugSession {
248247
}
249248

250249
protected stepOutRequest(response: DebugProtocol.StepOutResponse, args: DebugProtocol.StepOutArguments): void {
251-
this.log('stepOutRequest');
250+
this.log('stepOutRequest', LOG_LEVEL.SESSION);
252251

253252
this._protocolhandler.stepOut()
254253
.then(() => {
@@ -258,7 +257,7 @@ class IotjsDebugSession extends DebugSession {
258257
}
259258

260259
protected pauseRequest(response: DebugProtocol.PauseResponse, args: DebugProtocol.PauseArguments): void {
261-
this.log('pauseRequest');
260+
this.log('pauseRequest', LOG_LEVEL.SESSION);
262261

263262
this._protocolhandler.pause()
264263
.then(() => {
@@ -270,7 +269,7 @@ class IotjsDebugSession extends DebugSession {
270269
protected async setBreakPointsRequest(
271270
response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments
272271
): Promise<void> {
273-
this.log('setBreakPointsRequest');
272+
this.log('setBreakPointsRequest', LOG_LEVEL.SESSION);
274273

275274
const filename: string = args.source.name;
276275
const vscodeBreakpoints: DebugProtocol.Breakpoint[] = args.breakpoints!.map(b => ({verified: false, line: b.line}));
@@ -289,7 +288,7 @@ class IotjsDebugSession extends DebugSession {
289288
await this._protocolhandler.updateBreakpoint(jerryBreakpoint, true);
290289
return <TemporaryBreakpoint>{verified: true, line: breakpoint.line};
291290
} catch (error) {
292-
this.log(error);
291+
this.log(error, LOG_LEVEL.ERROR);
293292
return <TemporaryBreakpoint>{verified: false, line: breakpoint.line, message: (<Error>error).message};
294293
}
295294
}));
@@ -311,7 +310,7 @@ class IotjsDebugSession extends DebugSession {
311310

312311
response.body = { breakpoints: [...persistingBreakpoints, ...newBreakpoints] };
313312
} catch (error) {
314-
this.log(error);
313+
this.log(error, LOG_LEVEL.ERROR);
315314
this.sendErrorResponse(response, <Error>error);
316315
return;
317316
}
@@ -320,7 +319,7 @@ class IotjsDebugSession extends DebugSession {
320319
}
321320

322321
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {
323-
this.log('evaluateRequest');
322+
this.log('evaluateRequest', LOG_LEVEL.SESSION);
324323

325324
this._protocolhandler.evaluate(args.expression)
326325
.then((result: JerryEvalResult) => {
@@ -339,7 +338,7 @@ class IotjsDebugSession extends DebugSession {
339338
protected stackTraceRequest(
340339
response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments
341340
): void {
342-
this.log('stackTraceRequest');
341+
this.log('stackTraceRequest', LOG_LEVEL.SESSION);
343342

344343
this._protocolhandler.requestBacktrace()
345344
.then(backtrace => {
@@ -363,19 +362,19 @@ class IotjsDebugSession extends DebugSession {
363362
}
364363

365364
protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
366-
this.log('customRequest');
365+
this.log('customRequest', LOG_LEVEL.SESSION);
367366

368367
switch (command) {
369368
case 'sendSource': {
370369
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.IN_PROGRESS;
371370
this._protocolhandler.sendClientSource(args.program.name, args.program.source)
372371
.then(() => {
373-
this.log('Source has been sent to the engine.');
372+
this.log('Source has been sent to the engine.', LOG_LEVEL.SESSION);
374373
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.LAST_SENT;
375374
this.sendResponse(response);
376375
})
377376
.catch(error => {
378-
this.log(error);
377+
this.log(error, LOG_LEVEL.ERROR);
379378
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.NOP;
380379
this.sendErrorResponse(response, <Error>error, ErrorDestination.User);
381380
});
@@ -455,8 +454,8 @@ class IotjsDebugSession extends DebugSession {
455454

456455
}
457456

458-
private log(message: any): void {
459-
if (this._debugLog) {
457+
private log(message: any, level: number = LOG_LEVEL.VERBOSE): void {
458+
if (level === this._debugLog || this._debugLog === LOG_LEVEL.VERBOSE) {
460459
switch (typeof message) {
461460
case 'object':
462461
message = JSON.stringify(message, null, 2);

src/IotjsDebuggerConstants.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
export enum LOG_LEVEL {
20+
NONE = 0,
21+
ERROR = 1,
22+
SESSION = 2,
23+
PROTOCOL = 3,
24+
VERBOSE = 4
25+
}
26+
27+
export enum SOURCE_SENDING_STATES {
28+
NOP = 0,
29+
WAITING = 1,
30+
IN_PROGRESS = 2,
31+
LAST_SENT = 3
32+
}

src/IotjsDebuggerInterfaces.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface IAttachRequestArguments extends DebugProtocol.AttachRequestArgu
2626
// Automatically stop target after launch.
2727
stopOnEntry?: boolean;
2828
// Allows to log debug messages to console.
29-
debugLog?: boolean;
29+
debugLog?: number;
3030
// Filename.
3131
program?: string;
3232
// Ask for filename if in wait-for-source mode.

src/JerryProtocolHandler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import {
2222
decodeMessage, encodeMessage, stringToCesu8, setUint32
2323
} from './JerryUtils';
2424
import { JerryDebuggerClient } from './JerryDebuggerClient';
25+
import { LOG_LEVEL } from './IotjsDebuggerConstants';
2526

2627
export type CompressedPointer = number;
2728
export type ByteCodeOffset = number;
28-
export type LoggerFunction = (message: any) => void;
29+
export type LoggerFunction = (message: any, level: number) => void;
2930

3031
export interface ParserStackFrame {
3132
isFunc: boolean;
@@ -513,12 +514,12 @@ export class JerryDebugProtocolHandler {
513514
}
514515

515516
const atAround = breakpointRef.exact ? 'at' : 'around';
516-
this.log(`Stopped ${atAround} ${breakpointInfo}${breakpoint}`);
517+
this.log(`Stopped ${atAround} ${breakpointInfo}${breakpoint}`, LOG_LEVEL.PROTOCOL);
517518

518519
if (data[0] === SP.SERVER.JERRY_DEBUGGER_EXCEPTION_HIT) {
519-
this.log('Exception throw detected');
520+
this.log('Exception throw detected', LOG_LEVEL.ERROR);
520521
if (this.exceptionString) {
521-
this.log(`Exception hint: ${this.exceptionString}`);
522+
this.log(`Exception hint: ${this.exceptionString}`, LOG_LEVEL.ERROR);
522523
}
523524

524525
if (this.delegate.onExceptionHit) {
@@ -726,12 +727,12 @@ export class JerryDebugProtocolHandler {
726727
logPacket(description: string, ignorable: boolean = false) {
727728
// certain packets are ignored while evals are pending
728729
const ignored = (ignorable && this.evalsPending) ? 'Ignored: ' : '';
729-
this.log(`[Protocol Handler] ${ignored}${description}`);
730+
this.log(`[Protocol Handler] ${ignored}${description}`, LOG_LEVEL.PROTOCOL);
730731
}
731732

732733
private abort(message: string) {
733734
if (this.delegate.onError) {
734-
this.log(`Abort: ${message}`);
735+
this.log(`Abort: ${message}`, LOG_LEVEL.ERROR);
735736
this.delegate.onError(0, message);
736737
}
737738
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const initialConfigurations = [{
2828
port: 5001,
2929
localRoot: '${workspaceRoot}',
3030
stopOnEntry: false,
31-
debugLog: false
31+
debugLog: 0
3232
}];
3333

3434
const provideInitialConfigurations = (): string => {

0 commit comments

Comments
 (0)