Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enable logger in webRtcClient #738

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
}
],
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/return-await": 0
"@typescript-eslint/return-await": 0,
"@typescript-eslint/no-non-null-assertion": 0
},
"settings": {
"import/resolver": {
Expand Down
37 changes: 21 additions & 16 deletions src/simple/Phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,7 @@ class Phone extends Emitter implements IPhone {
options.log.builtinEnabled = false;
options.log.logLevel = 'debug';

options.log.connector = (level: any, className: string, label: any, content: string) => {
const protocolIndex = content && content.indexOf ? protocolDebugMessages.findIndex(prefix => content.indexOf(prefix) !== -1) : -1;

if (className === 'sip.Transport' && protocolIndex !== -1) {
const direction = protocolIndex === 0 ? 'receiving' : 'sending';
const message = content.replace(`${protocolDebugMessages[protocolIndex]}\n\n`, '').replace('\r\n', '\n');
protocolLogger.trace(message, {
className,
direction,
});
} else {
sipLogger.trace(content, {
className,
});
}
};
options.log.connector = this._logConnector;
}

this.client = new WazoWebRTCClient({
Expand Down Expand Up @@ -609,6 +594,10 @@ class Phone extends Emitter implements IPhone {
}
}

enableLogger(): void {
this.client.enableLogger(this._logConnector);
}

_transferEvents() {
this.unbind();
[...clientEvents, ...transportEvents].forEach(event => {
Expand All @@ -623,6 +612,22 @@ class Phone extends Emitter implements IPhone {
});
}

_logConnector(level: any, className: string, label: any, content: string): void {
const protocolIndex = content && content.indexOf ? protocolDebugMessages.findIndex(prefix => content.indexOf(prefix) !== -1) : -1;

if (className === 'sip.Transport' && protocolIndex !== -1) {
const direction = protocolIndex === 0 ? 'receiving' : 'sending';
const message = content.replace(`${protocolDebugMessages[protocolIndex]}\n\n`, '').replace('\r\n', '\n');
protocolLogger.trace(message, {
className,
direction,
});
} else {
sipLogger.trace(content, {
className,
});
}
}
}

if (!global.wazoTelephonyInstance) {
Expand Down
20 changes: 19 additions & 1 deletion src/web-rtc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export default class WebRTCClient extends Emitter {

return this._connectIfNeeded().then(() => {
// Avoid race condition with the close method called just before register and setting userAgent to null
// during the resolution of the primise.
// during the resolution of the promise.
if (!this.userAgent) {
logger.info('sdk webrtc recreating User Agent after connection');
this.userAgent = this.createUserAgent(this.uaConfigOverrides);
Expand Down Expand Up @@ -1878,6 +1878,24 @@ export default class WebRTCClient extends Emitter {
this.attemptReconnection();
}

enableLogger(logConnector: (level: any, className: string, label: any, content: string) => void): void {
if (!this.userAgent) {
return;
}

if (this.userAgent?.transport) {
// @ts-ignore
this.userAgent.transport.configuration.traceSip = true;
}

// @ts-ignore
this.userAgent.loggerFactory.builtinEnabled = true;
// @ts-ignore
this.userAgent.loggerFactory.level = 3; // debug
// @ts-ignore
this.userAgent.loggerFactory.connector = logConnector;
}

_onHeartbeat(message: string | Record<string, any>): void {
const body = message && typeof message === 'object' ? message.data : message;

Expand Down