Skip to content

Commit

Permalink
Remove console.logs (#426)
Browse files Browse the repository at this point in the history
* Remove console.logs

* Add .js
  • Loading branch information
Duddino authored Oct 15, 2024
1 parent 09c6aff commit e573fba
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"no-empty": ["error", { "allowEmptyCatch": true }],
"no-constant-condition": "off",
"@typescript-eslint/no-empty-function": "off"
"@typescript-eslint/no-empty-function": "off",
"no-console": "error"
}
}
8 changes: 7 additions & 1 deletion debug_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
"net": true,
"global": true,
"wallet": true,
"mempool": true
"mempool": true,
"aes_gcm": true,
"database": true,
"governance": true,
"i18n": true,
"ledger": true,
"masternode": true
}
4 changes: 3 additions & 1 deletion scripts/__mocks__/network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { vi } from 'vitest';
import { Transaction } from '../transaction.js';
import { DebugTopics, debugWarn } from '../debug.js';

export const getNetwork = vi.fn(() => {
return globalNetwork;
Expand Down Expand Up @@ -62,7 +63,8 @@ class TestNetwork {
'0100000001458de14f4f4fecfdeebfef09fb16e761bbd15029f37bec0a63b86808cbb8a512010000006b483045022100ef4f4364aea7604d749aaff7a2609e3a51a12f49500b7910b34ced0d0837e1db022012d153d96ebcb94e9b905a609c0ea97cdc99ae961be2848e0e8f2f695379c21201210371eca6799221b82cbba9e880a8a5a0f47d811f3ff5cad346931406ab0a0469eeffffffff0200e1f505000000001976a9148952bf31104625a7b3e6fcf4c79b35c6849ef74d88ac905cfb2f010000001976a9144e8d2fcf6d909c62597e4defd1c26d50842d73df88ac00000000';
await wallet.addTransaction(Transaction.fromHex(tx_1));
} else {
console.warn(
debugWarn(
DebugTopics.NET,
'getLatestTxs did not find any txs this wallet! ' +
wallet.getKeyToExport()
);
Expand Down
8 changes: 6 additions & 2 deletions scripts/aes-gcm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { debugError, DebugTopics } from './debug.js';

const buff_to_base64 = (buff) => btoa(String.fromCharCode.apply(null, buff));

const base64_to_buf = (b64) =>
Expand Down Expand Up @@ -73,7 +75,8 @@ async function encryptData(secretData, password) {
buff.set(encryptedContentArr, salt.byteLength + iv.byteLength);
return buff_to_base64(buff);
} catch (e) {
console.log(`Error - ${e}`);
debugError(DebugTopics.AES_GCM, `Error while encrypting`);
debugError(DebugTopics.AES_GCM, e);
return '';
}
}
Expand All @@ -96,7 +99,8 @@ async function decryptData(encryptedData, password) {
);
return dec.decode(decryptedContent);
} catch (e) {
console.log(`Error - ${e}`);
debugError(DebugTopics.AES_GCM, `Error while decrypting`);
debugError(DebugTopics.AES_GCM, e);
return '';
}
}
33 changes: 20 additions & 13 deletions scripts/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PromoWallet } from './promos.js';
import { ALERTS, translation } from './i18n.js';

Check warning on line 13 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'ALERTS' is defined but never used

Check warning on line 13 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'translation' is defined but never used
import { Account } from './accounts.js';
import { COutpoint, CTxIn, CTxOut, Transaction } from './transaction.js';
import { debugError, debugLog, DebugTopics } from './debug.js';

export class Database {
/**
Expand Down Expand Up @@ -118,11 +119,11 @@ export class Database {
async addAccount(account) {
// Critical: Ensure the input is an Account instance
if (!(account instanceof Account)) {
console.error(
debugError(
'---- addAccount() called with invalid input, input dump below ----'
);
console.error(account);
console.error('---- end of account dump ----');
debugError(DebugTopics.DATABASE, account);
debugError(DebugTopics.DATABASE, '---- end of account dump ----');
createAlert(
'warning',
'<b>Account Creation Error</b><br>Logs were dumped in your Browser Console<br>Please submit these privately to PIVX Labs Developers!'
Expand All @@ -141,7 +142,8 @@ export class Database {
for (const strKey of Object.keys(cDBAccount)) {
// Ensure the Type is correct for the Key against the Account class
if (!isSameType(account[strKey], cDBAccount[strKey])) {
console.error(
debugError(
DebugTopics.DATABASE,
'DB: addAccount() key "' +
strKey +
'" does NOT match the correct class type, likely data mismatch, please report!'
Expand Down Expand Up @@ -183,11 +185,12 @@ export class Database {
async updateAccount(account, allowDeletion = false) {
// Critical: Ensure the input is an Account instance
if (!(account instanceof Account)) {
console.error(
debugError(
DebugTopics.DATABASE,
'---- updateAccount() called with invalid input, input dump below ----'
);
console.error(account);
console.error('---- end of account dump ----');
debugError(DebugTopics.DATABASE, account);
debugError(DebugTopics.DATABASE, '---- end of account dump ----');
createAlert(
'warning',
'<b>DB Update Error</b><br>Your wallet is safe, logs were dumped in your Browser Console<br>Please submit these privately to PIVX Labs Developers!'
Expand All @@ -203,11 +206,12 @@ export class Database {
// If none exists; we should throw an error, as there's no reason for MPW to call `updateAccount` before an account was added using `addAccount`
// Note: This is mainly to force "good standards" in which we don't lazily use `updateAccount` to create NEW accounts.
if (!cDBAccount) {
console.error(
debugError(
DebugTopics.DATABASE,
'---- updateAccount() called without an account existing, input dump below ----'
);
console.error(account);
console.error('---- end of input dump ----');
debugError(DebugTopics.DATABASE, account);
debugError(DebugTopics.DATABASE, '---- end of input dump ----');
createAlert(
'warning',
'<b>DB Update Error</b><br>Logs were dumped in your Browser Console<br>Please submit these privately to PIVX Labs Developers!'
Expand All @@ -223,7 +227,8 @@ export class Database {
for (const strKey of Object.keys(cDBAccount)) {
// Ensure the Type is correct for the Key against the Account class
if (!isSameType(account[strKey], cDBAccount[strKey])) {
console.error(
debugError(
DebugTopics.DATABASE,
'DB: updateAccount() key "' +
strKey +
'" does NOT match the correct class type, likely data mismatch, please report!'
Expand Down Expand Up @@ -283,7 +288,8 @@ export class Database {

// Ensure the Type is correct for the Key against the Account class (with instanceof to also check Class validity)
if (!isSameType(cDBAccount[strKey], cAccount[strKey])) {
console.error(
debugError(
DebugTopics.DATABASE,
'DB: getAccount() key "' +
strKey +
'" does NOT match the correct class type, likely bad data saved, please report!'
Expand Down Expand Up @@ -425,7 +431,8 @@ export class Database {
const database = new Database({ db: null });
const db = await openDB(`MPW-${name}`, Database.version, {
upgrade: (db, oldVersion, _, transaction) => {
console.log(
debugLog(
DebugTopics.DATABASE,
'DB: Upgrading from ' +
oldVersion +
' to ' +
Expand Down
39 changes: 21 additions & 18 deletions scripts/debug.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
/* eslint-disable no-console */

import { debug } from './settings.js';
import debugParams from '../debug_config.json';

/**
* Execute a given function if we are in debug mode
* @param {Function} func - a function we want to execute
* @param {...any} args - The arguments to pass to the function
*/
function debugEval(func, ...args) {
if (debug) {
func(...args);
}
}

/**
* Execute a given function if we are in debug mode and the provided topic is in the filtered list
* Execute a given function if the provided topic is in the filtered list
* @param {Function} func - a function we want to execute
* @param {DebugTopic} topic - the topic of the debug
* @param {...any} args - The arguments to pass to the function
*/
function debugEvalFilter(func, topic, ...args) {
if (topic.value & enabledDebug) {
debugEval(func, ...args);
func(...args);
}
}
/**
Expand All @@ -29,11 +20,13 @@ function debugEvalFilter(func, topic, ...args) {
* @param args - arguments to print
*/
export function debugLog(topic, ...args) {
debugEvalFilter(console.log, topic, topic.name, ...args);
if (debug) {
debugEvalFilter(console.log, topic, topic.name, ...args);
}
}

/**
* call console.warn if we are in debug mode
* call console.warn
* @param {DebugTopic} topic - topic of the debug
* @param args - arguments to print
*/
Expand All @@ -42,7 +35,7 @@ export function debugWarn(topic, ...args) {
}

/**
* call console.error if we are in debug mode
* call console.error
* @param {DebugTopic} topic - topic of the debug
* @param args - arguments to print
*/
Expand All @@ -56,7 +49,9 @@ export function debugError(topic, ...args) {
* @param {String} label - the label of the timer
*/
export function debugTimerStart(topic, label) {
debugEvalFilter(console.time, topic, topic.name + ' ' + label);
if (debug) {
debugEvalFilter(console.time, topic, topic.name + ' ' + label);
}
}

/**
Expand All @@ -65,7 +60,9 @@ export function debugTimerStart(topic, label) {
* @param {String} label - the label of the timer
*/
export function debugTimerEnd(topic, label) {
debugEvalFilter(console.timeEnd, topic, topic.name + ' ' + label);
if (debug) {
debugEvalFilter(console.timeEnd, topic, topic.name + ' ' + label);
}
}

class DebugTopic {
Expand All @@ -79,6 +76,12 @@ export const DebugTopics = {
GLOBAL: new DebugTopic('[GLOBAL]', 1 << 1),
WALLET: new DebugTopic('[WALLET]', 1 << 2),
MEMPOOL: new DebugTopic('[MEMPOOL]', 1 << 3),
AES_GCM: new DebugTopic('[AES_GMC]', 1 << 4),
DATABASE: new DebugTopic('[DATABASE]', 1 << 5),
GOVERNANCE: new DebugTopic('[GOVERNANCE]', 1 << 6),
I18N: new DebugTopic('[I18N]', 1 << 7),
LEDGER: new DebugTopic('[LEDGER]', 1 << 8),
MASTERNODE: new DebugTopic('[MASTERNODE]', 1 << 9),
};

let enabledDebug = 0;
Expand Down
8 changes: 4 additions & 4 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { checkForUpgrades } from './changelog.js';
import { FlipDown } from './flipdown.js';
import { createApp } from 'vue';
import Dashboard from './dashboard/Dashboard.vue';
import { loadDebug, debugLog, DebugTopics } from './debug.js';
import { loadDebug, debugLog, DebugTopics, debugError } from './debug.js';
import Stake from './stake/Stake.vue';
import { createPinia } from 'pinia';
import { cOracle } from './prices.js';
Expand Down Expand Up @@ -306,8 +306,8 @@ function subscribeToNetworkEvents() {
result ? 1250 + result.length * 50 : 3000
);
} else {
console.error('Error sending transaction:');
console.error(result);
debugError(DebugTopics.NET, 'Error sending transaction:');
debugError(DebugTopics.NET, result);
createAlert('warning', ALERTS.TX_FAILED, 2500);
}
});
Expand Down Expand Up @@ -498,7 +498,7 @@ export async function govVote(hash, voteCode) {
createAlert('warning', ALERTS.VOTE_SIG_BAD, 6000);
} else {
//this could be everything
console.error(result);
debugError(DebugTopics.GOVERNANCE, result);
createAlert('warning', ALERTS.INTERNAL_ERROR, 6000);
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion scripts/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { wallet } from './wallet.js';
import { cReceiveType, guiToggleReceiveType } from './contacts-book.js';
import { reactive } from 'vue';
import { negotiateLanguages } from '@fluent/langneg';
import { debugLog, DebugTopics } from './debug.js';

/**
* @type {translation_template}
Expand Down Expand Up @@ -127,7 +128,8 @@ export async function switchTranslation(langName) {
}
return true;
} else {
console.log(
debugLog(
DebugTopics.I18N,
'i18n: The language (' +
langName +
") is not supported yet, if you'd like to contribute translations (for rewards!) contact us on GitHub or Discord!"
Expand Down
7 changes: 5 additions & 2 deletions scripts/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Transaction } from './transaction.js';
import { COIN, cChainParams } from './chain_params.js';
import { hexToBytes, bytesToHex } from './utils.js';
import { OP } from './script.js';
import { debugError, DebugTopics } from './debug.js';

/**
* @type{import('@ledgerhq/hw-transport-webusb').default}
Expand Down Expand Up @@ -111,13 +112,15 @@ export async function getHardwareWalletKeys(path, xpub = false, verify = true) {
createAlert('warning', ALERTS.WALLET_HARDWARE_NO_ACCESS, 5500);
}

console.error(e);
debugError(DebugTopics.LEDGER, e);
return;
}

// Check if this is an expected error
if (!e.statusCode || !LEDGER_ERRS.has(e.statusCode)) {
console.error(
debugError(DebugTopics.LEDGER, e);
debugError(
DebugTopics.LEDGER,
'MISSING LEDGER ERROR-CODE TRANSLATION! - Please report this below error on our GitHub so we can handle it more nicely!'
);
throw e;
Expand Down
5 changes: 3 additions & 2 deletions scripts/masternode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import bs58 from 'bs58';
import base32 from 'base32';
import { isStandardAddress } from './misc.js';
import { getNetwork } from './network.js';
import { debugError, DebugTopics } from './debug.js';

/**
* Construct a Masternode
Expand Down Expand Up @@ -61,7 +62,7 @@ export default class Masternode {
}
} catch (e) {
//this is the unfortunate state in which the node is not reachable
console.error(e);
debugError(DebugTopics.MASTERNODE, e);
return 'EXPLORER_DOWN';
}
}
Expand Down Expand Up @@ -537,7 +538,7 @@ export default class Masternode {
return { ok: false, err: 'other' };
}
} catch (e) {
console.error(e);
debugError(DebugTopics.MASTERNODE, e);
return { ok: false, err: e };
}
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/native-worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-console */

// Listen for native worker installs
self.addEventListener('install', function (_event) {
console.log('[ServiceWorker] Install');
Expand Down
5 changes: 3 additions & 2 deletions scripts/network.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cChainParams } from './chain_params.js';
import { createAlert } from './misc.js';

Check warning on line 2 in scripts/network.js

View workflow job for this annotation

GitHub Actions / Run linters

'createAlert' is defined but never used
import {
debugError,
debugLog,
debugTimerEnd,
debugTimerStart,
Expand Down Expand Up @@ -279,7 +280,7 @@ export class ExplorerNetwork extends Network {
).json();
return arrUTXOs;
} catch (e) {
console.error(e);
debugError(DebugTopics.NET, e);
}
}

Expand All @@ -306,7 +307,7 @@ export class ExplorerNetwork extends Network {
// Throw and catch if the data is not a TXID
if (!data.result || data.result.length !== 64) throw data;

console.log('Transaction sent! ' + data.result);
debugLog(DebugTopics.NET, 'Transaction sent! ' + data.result);
getEventEmitter().emit('transaction-sent', true, data.result);
return data.result;
} catch (e) {
Expand Down
Loading

0 comments on commit e573fba

Please sign in to comment.