From f3ec27b95fcb56d521952c708fdc0229f6d6f7f3 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 27 Dec 2018 15:48:38 -0500 Subject: [PATCH] Added customizable log levels to quiet warnings (#379). --- src.ts/contract.ts | 3 +-- src.ts/errors.ts | 25 +++++++++++++++++++++++++ src.ts/providers/base-provider.ts | 2 +- src.ts/providers/infura-provider.ts | 2 +- src.ts/utils/abi-coder.ts | 4 ++-- src.ts/utils/interface.ts | 12 ++++++++---- src.ts/utils/transaction.ts | 4 ++-- 7 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src.ts/contract.ts b/src.ts/contract.ts index 39459fe8..cc824dcf 100644 --- a/src.ts/contract.ts +++ b/src.ts/contract.ts @@ -411,7 +411,6 @@ export class Contract { if (address == null) { throw new Error('name not found'); } return address; }).catch((error: Error) => { - console.log('ERROR: Cannot find Contract - ' + addressOrName); throw error; })); } else { @@ -429,7 +428,7 @@ export class Contract { if ((this)[name] == null) { defineReadOnly(this, name, run); } else { - console.log('WARNING: Multiple definitions for ' + name); + errors.warn('WARNING: Multiple definitions for ' + name); } if (this.functions[name] == null) { diff --git a/src.ts/errors.ts b/src.ts/errors.ts index 91765230..531b7ef5 100644 --- a/src.ts/errors.ts +++ b/src.ts/errors.ts @@ -141,3 +141,28 @@ export function checkNormalize(): void { throwError('platform missing String.prototype.normalize', UNSUPPORTED_OPERATION, { operation: 'String.prototype.normalize', form: error.message }); } } + +const LogLevels: { [ name: string ]: number } = { debug: 1, "default": 2, info: 2, warn: 3, error: 4, off: 5 }; +let LogLevel = LogLevels["default"]; + +export function setLogLevel(logLevel: string): void { + let level = LogLevels[logLevel]; + if (level == null) { + warn("invliad log level - " + logLevel); + return; + } + LogLevel = level; +} + +function log(logLevel: string, args: Array): void { + if (LogLevel > LogLevels[logLevel]) { return; } + console.log.apply(console, args); +} + +export function warn(...args: Array): void { + log("warn", args); +} + +export function info(...args: Array): void { + log("info", args); +} diff --git a/src.ts/providers/base-provider.ts b/src.ts/providers/base-provider.ts index 4c680a4b..0857f888 100644 --- a/src.ts/providers/base-provider.ts +++ b/src.ts/providers/base-provider.ts @@ -1224,7 +1224,7 @@ export class BaseProvider extends Provider { } protected _startPending(): void { - console.log('WARNING: this provider does not support pending events'); + errors.warn('WARNING: this provider does not support pending events'); } protected _stopPending(): void { diff --git a/src.ts/providers/infura-provider.ts b/src.ts/providers/infura-provider.ts index 312315b3..58b75f6f 100644 --- a/src.ts/providers/infura-provider.ts +++ b/src.ts/providers/infura-provider.ts @@ -41,7 +41,7 @@ export class InfuraProvider extends JsonRpcProvider { } protected _startPending(): void { - console.log('WARNING: INFURA does not support pending filters'); + errors.warn('WARNING: INFURA does not support pending filters'); } getSigner(address?: string): JsonRpcSigner { diff --git a/src.ts/utils/abi-coder.ts b/src.ts/utils/abi-coder.ts index 8f2c2d80..a631bc1c 100644 --- a/src.ts/utils/abi-coder.ts +++ b/src.ts/utils/abi-coder.ts @@ -266,7 +266,7 @@ function parseSignatureEvent(fragment: string): EventFragment { case '': break; default: - console.log('unknown modifier: ' + modifier); + errors.info('unknown modifier: ' + modifier); } }); @@ -336,7 +336,7 @@ function parseSignatureFunction(fragment: string): FunctionFragment { case '': break; default: - console.log('unknown modifier: ' + modifier); + errors.info('unknown modifier: ' + modifier); } }); diff --git a/src.ts/utils/interface.ts b/src.ts/utils/interface.ts index a703336b..a4e5790f 100644 --- a/src.ts/utils/interface.ts +++ b/src.ts/utils/interface.ts @@ -325,9 +325,13 @@ function addMethod(method: any): void { sighash: sighash, }); - // Expose the first (and hopefully unique named function - if (method.name && this.functions[method.name] == null) { - defineReadOnly(this.functions, method.name, description); + // Expose the first (and hopefully unique named function) + if (method.name) { + if (this.functions[method.name] == null) { + defineReadOnly(this.functions, method.name, description); + } else { + errors.warn('WARNING: Multiple definitions for ' + method.name); + } } // Expose all methods by their signature, for overloaded functions @@ -368,7 +372,7 @@ function addMethod(method: any): void { break; default: - console.log('WARNING: unsupported ABI type - ' + method.type); + errors.warn('WARNING: unsupported ABI type - ' + method.type); break; } } diff --git a/src.ts/utils/transaction.ts b/src.ts/utils/transaction.ts index 0e822183..b5c9d468 100644 --- a/src.ts/utils/transaction.ts +++ b/src.ts/utils/transaction.ts @@ -162,7 +162,7 @@ export function parse(rawTransaction: Arrayish): Transaction { tx.v = bigNumberify(transaction[6]).toNumber(); } catch (error) { - console.log(error); + errors.info(error); return tx; } @@ -195,7 +195,7 @@ export function parse(rawTransaction: Arrayish): Transaction { try { tx.from = recoverAddress(digest, { r: hexlify(tx.r), s: hexlify(tx.s), recoveryParam: recoveryParam }); } catch (error) { - console.log(error); + errors.info(error); } tx.hash = keccak256(rawTransaction);