UNPKG

@itwin/presentation-frontend

Version:

Frontend of iModel.js Presentation library

62 lines 2.27 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Core */ import { DiagnosticsLogEntry, } from "@itwin/presentation-common"; /** * A function which logs messages to the console. * @public */ export function consoleDiagnosticsHandler(diagnostics) { // eslint-disable-next-line no-console diagnostics.backendVersion && console.log(`Backend version: ${diagnostics.backendVersion}`); diagnostics.logs && handleDiagnosticLogs(diagnostics.logs, (msg, stack) => { /* note: we're duplicating the message if it's logged at both editor and dev severity levels */ const str = buildLogMessageString(msg, stack); if (msg.severity.editor) { getConsoleLogFunc(msg.severity.editor)(str); } if (msg.severity.dev) { getConsoleLogFunc(msg.severity.dev)(str); } }); } /** * A function which calls all diagnostics handlers passed to it. * @public */ export function createCombinedDiagnosticsHandler(handlers) { return (diagnostics) => { handlers.forEach((handler) => handler(diagnostics)); }; } function handleDiagnosticLogs(logs, messageHandler, stack = []) { logs.forEach((log) => { if (DiagnosticsLogEntry.isMessage(log)) { messageHandler(log, stack); } else if (log.logs) { handleDiagnosticLogs(log.logs, messageHandler, [...stack, log]); } }); } function buildLogMessageString(msg, _stack) { return msg.message; } function getConsoleLogFunc(severity) { switch (severity) { case "error": return console.error; // eslint-disable-line no-console case "warning": return console.warn; // eslint-disable-line no-console case "info": case "debug": case "trace": return console.log; // eslint-disable-line no-console } } //# sourceMappingURL=Diagnostics.js.map