@itwin/core-frontend
Version:
iTwin.js frontend components
78 lines • 3.31 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module NativeApp
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NativeAppLogger = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const IpcApp_1 = require("./IpcApp");
/**
* NativeAppLogger send log message from frontend to backend. It works on native app only.
* @internal
*/
class NativeAppLogger {
static _messages = [];
static _onFlushed;
static flushToBackend() {
if (!this._onFlushed && this._messages.length > 0) {
this._onFlushed = new Promise(() => this._onFlushed = undefined);
const messages = this._messages;
this._messages = [];
setTimeout(async () => this.flushBucket(messages));
}
}
static async flushBucket(messages) {
try {
while (messages.length > 0) {
const msg = messages.shift();
await IpcApp_1.IpcApp.appFunctionIpc.log(msg.timestamp, msg.level, msg.category, msg.message, { ...msg.metaData });
}
}
finally {
// Put back unsent messages.
this._messages.unshift(...messages);
if (this._messages.length > 0) {
this.flushToBackend();
}
else {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Promise.resolve(this._onFlushed);
}
}
}
static log(level, category, message, metaData) {
this._messages.push({ timestamp: Date.now(), level, category, message, metaData: core_bentley_1.BentleyError.getMetaData(metaData) });
this.flushToBackend();
}
static logError(category, message, metaData) {
this.log(core_bentley_1.LogLevel.Error, category, message, metaData);
}
static logInfo(category, message, metaData) {
this.log(core_bentley_1.LogLevel.Info, category, message, metaData);
}
static logTrace(category, message, metaData) {
this.log(core_bentley_1.LogLevel.Trace, category, message, metaData);
}
static logWarning(category, message, metaData) {
this.log(core_bentley_1.LogLevel.Warning, category, message, metaData);
}
static async flush() {
this.flushToBackend();
if (this._onFlushed) {
return this._onFlushed;
}
}
static initialize() {
const errCb = (category, message, metaData) => this.logError(category, message, metaData);
const warnCb = (category, message, metaData) => this.logWarning(category, message, metaData);
const infoCb = (category, message, metaData) => this.logInfo(category, message, metaData);
const traceCb = (category, message, metaData) => this.logTrace(category, message, metaData);
core_bentley_1.Logger.initialize(errCb, warnCb, infoCb, traceCb);
}
}
exports.NativeAppLogger = NativeAppLogger;
//# sourceMappingURL=NativeAppLogger.js.map
;