taler-wallet: implement log level, use new wallet for every benchmark iteration

This commit is contained in:
Florian Dold 2021-11-05 13:10:15 +01:00
parent e627f65f3c
commit 6fc3aa0b31
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
3 changed files with 112 additions and 14 deletions

View File

@ -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 {

View File

@ -40,16 +40,17 @@ export async function runBench1(configJson: any): Promise<void> {
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<void> {
await wallet.runTaskLoop({
stopWhenDone: true,
});
}
wallet.stop();
wallet.stop();
}
}
/**

View File

@ -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.",