* Listen to any logs * @param callback * @param filterBy * @returns {Promise }
(callback, filterBy = undefined)
| 234 | * @returns {Promise<number>} |
| 235 | */ |
| 236 | async onLog(callback, filterBy = undefined) { |
| 237 | if (filterBy !== undefined && !(filterBy instanceof FilterBy)) { |
| 238 | throw Error(`Pass valid FilterBy object. Received: ${filterBy}`) |
| 239 | } |
| 240 | |
| 241 | let id |
| 242 | if (filterBy !== undefined) { |
| 243 | id = this.addCallback(LOG.TYPE_LOGS_FILTER, { callback: callback, filter: filterBy }) |
| 244 | } else { |
| 245 | id = this.addCallback(LOG.TYPE_LOGS, callback) |
| 246 | } |
| 247 | |
| 248 | this.ws = await this.bidi.socket |
| 249 | |
| 250 | this.ws.on('message', (event) => { |
| 251 | const { params } = JSON.parse(Buffer.from(event.toString())) |
| 252 | if (params?.type === 'javascript') { |
| 253 | let jsEntry = new JavascriptLogEntry( |
| 254 | params.level, |
| 255 | params.source, |
| 256 | params.text, |
| 257 | params.timestamp, |
| 258 | params.type, |
| 259 | params.stackTrace, |
| 260 | ) |
| 261 | |
| 262 | if (filterBy !== undefined) { |
| 263 | if (params?.level === filterBy.getLevel()) { |
| 264 | callback(jsEntry) |
| 265 | } |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | if (filterBy !== undefined) { |
| 270 | if (params?.level === filterBy.getLevel()) { |
| 271 | { |
| 272 | this.invokeCallbacksWithFilter(LOG.TYPE_LOGS_FILTER, jsEntry, filterBy.getLevel()) |
| 273 | } |
| 274 | return |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | this.invokeCallbacks(LOG.TYPE_LOGS, jsEntry) |
| 279 | return |
| 280 | } |
| 281 | |
| 282 | if (params?.type === 'console') { |
| 283 | let consoleEntry = new ConsoleLogEntry( |
| 284 | params.level, |
| 285 | params.source, |
| 286 | params.text, |
| 287 | params.timestamp, |
| 288 | params.type, |
| 289 | params.method, |
| 290 | params.args, |
| 291 | params.stackTrace, |
| 292 | ) |
| 293 |
no test coverage detected