diff options
author | Florian Dold <florian@dold.me> | 2023-10-23 16:16:52 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-10-23 16:16:52 +0200 |
commit | 8ba62f7e0e0e6208738ccff2c9da99e6353ec961 (patch) | |
tree | 2a8d6d388b326aa90970c35a0e8041de59692c23 /packages/taler-util/src/logging.ts | |
parent | 2200de4e35766457c965b1a169737f71f051d516 (diff) | |
download | wallet-core-8ba62f7e0e0e6208738ccff2c9da99e6353ec961.tar.gz wallet-core-8ba62f7e0e0e6208738ccff2c9da99e6353ec961.tar.bz2 wallet-core-8ba62f7e0e0e6208738ccff2c9da99e6353ec961.zip |
wallet-core: native logging support
Diffstat (limited to 'packages/taler-util/src/logging.ts')
-rw-r--r-- | packages/taler-util/src/logging.ts | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/packages/taler-util/src/logging.ts b/packages/taler-util/src/logging.ts index 79fc49cdd..9e6a265b8 100644 --- a/packages/taler-util/src/logging.ts +++ b/packages/taler-util/src/logging.ts @@ -14,6 +14,7 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ + /** * Check if we are running under nodejs. */ @@ -35,6 +36,8 @@ export enum LogLevel { let globalLogLevel = LogLevel.Info; const byTagLogLevel: Record<string, LogLevel> = {}; +let nativeLogging: boolean = false; + export function getGlobalLogLevel(): string { return globalLogLevel; } @@ -47,6 +50,10 @@ export function setLogLevelFromString(tag: string, logLevelStr: string): void { byTagLogLevel[tag] = getLevelForString(logLevelStr); } +export function enableNativeLogging() { + nativeLogging = true; +} + function getLevelForString(logLevelStr: string): LogLevel { switch (logLevelStr.toLowerCase()) { case "trace": @@ -70,6 +77,24 @@ function getLevelForString(logLevelStr: string): LogLevel { } } +function writeNativeLog( + message: any, + tag: string, + level: number, + args: any[], +): void { + const logFn = (globalThis as any).__nativeLog; + if (logFn) { + let m: string; + if (args.length == 0) { + m = message; + } else { + m = message + " " + args.toString(); + } + logFn(level, tag, message); + } +} + function writeNodeLog( message: any, tag: string, @@ -102,7 +127,7 @@ function writeNodeLog( * and uses the corresponding console.* method to log in the browser. */ export class Logger { - constructor(private tag: string) { } + constructor(private tag: string) {} shouldLogTrace(): boolean { const level = byTagLogLevel[this.tag] ?? globalLogLevel; @@ -164,6 +189,10 @@ export class Logger { if (!this.shouldLogInfo()) { return; } + if (nativeLogging) { + writeNativeLog(message, this.tag, 2, args); + return; + } if (isNode) { writeNodeLog(message, this.tag, "INFO", args); } else { @@ -178,6 +207,10 @@ export class Logger { if (!this.shouldLogWarn()) { return; } + if (nativeLogging) { + writeNativeLog(message, this.tag, 3, args); + return; + } if (isNode) { writeNodeLog(message, this.tag, "WARN", args); } else { @@ -192,6 +225,10 @@ export class Logger { if (!this.shouldLogError()) { return; } + if (nativeLogging) { + writeNativeLog(message, this.tag, 4, args); + return; + } if (isNode) { writeNodeLog(message, this.tag, "ERROR", args); } else { @@ -206,6 +243,10 @@ export class Logger { if (!this.shouldLogTrace()) { return; } + if (nativeLogging) { + writeNativeLog(message, this.tag, 1, args); + return; + } if (isNode) { writeNodeLog(message, this.tag, "TRACE", args); } else { |