From 6fc3aa0b31021f02d10e5a2efb12879aa64774fd Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 5 Nov 2021 13:10:15 +0100 Subject: [PATCH] taler-wallet: implement log level, use new wallet for every benchmark iteration --- packages/taler-util/src/logging.ts | 101 ++++++++++++++++++++++-- packages/taler-wallet-cli/src/bench1.ts | 17 ++-- packages/taler-wallet-cli/src/index.ts | 8 ++ 3 files changed, 112 insertions(+), 14 deletions(-) diff --git a/packages/taler-util/src/logging.ts b/packages/taler-util/src/logging.ts index 0037d95a3..8b9de1ab0 100644 --- a/packages/taler-util/src/logging.ts +++ b/packages/taler-util/src/logging.ts @@ -23,6 +23,47 @@ const isNode = typeof process.release !== "undefined" && process.release.name === "node"; +export enum LogLevel { + Trace = "trace", + Message = "message", + Info = "info", + Warn = "warn", + Error = "error", + None = "none", +} + +export let globalLogLevel = LogLevel.Info; + +export function setGlobalLogLevelFromString(logLevelStr: string) { + let level: LogLevel; + switch (logLevelStr.toLowerCase()) { + case "trace": + level = LogLevel.Trace; + break; + case "info": + level = LogLevel.Info; + break; + case "warn": + case "warning": + level = LogLevel.Warn; + break; + case "error": + level = LogLevel.Error; + break; + case "none": + level = LogLevel.None; + break; + default: + if (isNode) { + process.stderr.write(`Invalid log level, defaulting to WARNING`); + } else { + console.warn(`Invalid log level, defaulting to WARNING`); + } + level = LogLevel.Warn; + } + globalLogLevel = level; +} + function writeNodeLog( message: any, tag: string, @@ -57,21 +98,60 @@ export class Logger { constructor(private tag: string) {} shouldLogTrace() { - // FIXME: Implement logic to check loglevel - return true; + switch (globalLogLevel) { + case LogLevel.Trace: + return true; + case LogLevel.Message: + case LogLevel.Info: + case LogLevel.Warn: + case LogLevel.Error: + case LogLevel.None: + return false; + } } shouldLogInfo() { - // FIXME: Implement logic to check loglevel - return true; + switch (globalLogLevel) { + case LogLevel.Trace: + case LogLevel.Message: + case LogLevel.Info: + return true; + case LogLevel.Warn: + case LogLevel.Error: + case LogLevel.None: + return false; + } } shouldLogWarn() { - // FIXME: Implement logic to check loglevel - return true; + switch (globalLogLevel) { + case LogLevel.Trace: + case LogLevel.Message: + case LogLevel.Info: + case LogLevel.Warn: + return true; + case LogLevel.Error: + case LogLevel.None: + return false; + } + } + + shouldLogError() { + switch (globalLogLevel) { + case LogLevel.Trace: + case LogLevel.Message: + case LogLevel.Info: + case LogLevel.Warn: + case LogLevel.Error: + case LogLevel.None: + return false; + } } info(message: string, ...args: any[]): void { + if (!this.shouldLogInfo()) { + return; + } if (isNode) { writeNodeLog(message, this.tag, "INFO", args); } else { @@ -83,6 +163,9 @@ export class Logger { } warn(message: string, ...args: any[]): void { + if (!this.shouldLogWarn()) { + return; + } if (isNode) { writeNodeLog(message, this.tag, "WARN", args); } else { @@ -94,6 +177,9 @@ export class Logger { } error(message: string, ...args: any[]): void { + if (!this.shouldLogError()) { + return; + } if (isNode) { writeNodeLog(message, this.tag, "ERROR", args); } else { @@ -105,6 +191,9 @@ export class Logger { } trace(message: any, ...args: any[]): void { + if (!this.shouldLogTrace()) { + return; + } if (isNode) { writeNodeLog(message, this.tag, "TRACE", args); } else { diff --git a/packages/taler-wallet-cli/src/bench1.ts b/packages/taler-wallet-cli/src/bench1.ts index 4a2651f36..ec0430d8d 100644 --- a/packages/taler-wallet-cli/src/bench1.ts +++ b/packages/taler-wallet-cli/src/bench1.ts @@ -40,16 +40,17 @@ export async function runBench1(configJson: any): Promise { const b1conf = codecForBench1Config().decode(configJson); const myHttpLib = new NodeHttpLib(); - const wallet = await getDefaultNodeWallet({ - // No persistent DB storage. - persistentStoragePath: undefined, - httpLib: myHttpLib, - }); - await wallet.client.call(WalletApiOperation.InitWallet, {}); const numIter = b1conf.iterations ?? 1; for (let i = 0; i < numIter; i++) { + const wallet = await getDefaultNodeWallet({ + // No persistent DB storage. + persistentStoragePath: undefined, + httpLib: myHttpLib, + }); + await wallet.client.call(WalletApiOperation.InitWallet, {}); + await wallet.client.call(WalletApiOperation.WithdrawFakebank, { amount: "TESTKUDOS:10", bank: b1conf.bank, @@ -68,9 +69,9 @@ export async function runBench1(configJson: any): Promise { await wallet.runTaskLoop({ stopWhenDone: true, }); - } - wallet.stop(); + wallet.stop(); + } } /** diff --git a/packages/taler-wallet-cli/src/index.ts b/packages/taler-wallet-cli/src/index.ts index 142e98e7c..71431b5eb 100644 --- a/packages/taler-wallet-cli/src/index.ts +++ b/packages/taler-wallet-cli/src/index.ts @@ -43,6 +43,8 @@ import { Configuration, decodeCrock, rsaBlind, + LogLevel, + setGlobalLogLevelFromString, } from "@gnu-taler/taler-util"; import { NodeHttpLib, @@ -161,6 +163,12 @@ export const walletCli = clk setDangerousTimetravel(x / 1000); }, }) + .maybeOption("log", ["-L", "--log"], clk.STRING, { + help: "configure log level (NONE, ..., TRACE)", + onPresentHandler: (x) => { + setGlobalLogLevelFromString(x); + }, + }) .maybeOption("inhibit", ["--inhibit"], clk.STRING, { help: "Inhibit running certain operations, useful for debugging and testing.",