From 9f8daa8b88dfd5c16fe3ff8f23d1f878d846ff50 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 10 Sep 2025 17:14:32 +1000 Subject: [PATCH] feat: Replace most prints with logger calls to help with debugging --- account.py | 6 +++++- main.py | 36 ++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/account.py b/account.py index 4dbbcff..664c938 100644 --- a/account.py +++ b/account.py @@ -352,6 +352,7 @@ def getBalance(account: str): # Get the total balance info = hsw.getBalance('default', account) if 'error' in info: + logger.error(f"Error getting balance for account {account}: {info['error']}") return {'available': 0, 'total': 0} total = info['confirmed'] @@ -361,6 +362,7 @@ def getBalance(account: str): # Convert to HNS total = total / 1000000 available = available / 1000000 + logger.debug(f"Initial balance for account {account}: total={total}, available={available}, locked={locked}") domains = getDomains(account) domainValue = 0 @@ -404,6 +406,7 @@ def getBalance(account: str): if domain_info.get('info', {}).get('state', "") == "CLOSED": domainValue += domain_info.get('info', {}).get('value', 0) except json.JSONDecodeError: + logger.warning(f"Error parsing cached data for domain {domain_name}") # Only add for update if not already being updated with DOMAIN_UPDATE_LOCK: if domain_name not in ACTIVE_DOMAIN_UPDATES: @@ -423,9 +426,10 @@ def getBalance(account: str): daemon=True ) thread.start() - + total = total - (domainValue/1000000) locked = locked - (domainValue/1000000) + logger.debug(f"Adjusted balance for account {account}: total={total}, available={available}, locked={locked}") # Only keep 2 decimal places total = round(total, 2) diff --git a/main.py b/main.py index ee6bbc7..a0b3514 100644 --- a/main.py +++ b/main.py @@ -43,6 +43,12 @@ formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(mess handler.setFormatter(formatter) logger = logging.getLogger() logger.setLevel(logging.WARNING) + +# Disable werkzeug logging +logging.getLogger('werkzeug').setLevel(logging.INFO) +logging.getLogger("urllib3").setLevel(logging.ERROR) +logging.getLogger("requests").setLevel(logging.ERROR) + logger.addHandler(handler) @app.route('/') @@ -71,7 +77,7 @@ def index(): branch = info['refs'] commit = info['commit'] if commit != latestVersion(branch): - print("New version available",flush=True) + logger.info("New version available") plugins += render_template('components/dashboard-alert.html', name='Update', output='A new version of FireWallet is available') alerts = get_alerts(account) @@ -602,7 +608,7 @@ def finalize(domain: str): domain = domain.lower() response = account_module.finalize(request.cookies.get("account"),domain) if response['error'] is not None: - print(response) + logger.error(f"Error finalizing transfer for {domain}: {response['error']}") return redirect("/manage/" + domain + "?error=" + response['error']['message']) return redirect("/success?tx=" + response['result']['hash']) @@ -621,7 +627,7 @@ def cancelTransfer(domain: str): response = account_module.cancelTransfer(request.cookies.get("account"),domain) if 'error' in response: if response['error'] is not None: - print(response) + logger.error(f"Error canceling transfer for {domain}: {response['error']}") return redirect("/manage/" + domain + "?error=" + response['error']['message']) return redirect("/success?tx=" + response['result']['hash']) @@ -677,7 +683,7 @@ def revokeConfirm(domain: str): response = account_module.revoke(request.cookies.get("account"),domain) if 'error' in response: if response['error'] is not None: - print(response) + logger.error(f"Error revoking {domain}: {response['error']}") return redirect("/manage/" + domain + "?error=" + response['error']['message']) return redirect(f"/success?tx={response['hash']}") @@ -783,7 +789,7 @@ def editSave(domain: str): dns = urllib.parse.unquote(dns) response = account_module.setDNS(request.cookies.get("account"),domain,dns) if 'error' in response: - print(response) + logger.error(f"Error setting DNS for {domain}: {response['error']}") return redirect(f"/manage/{domain}/edit?dns={raw_dns}&error={response['error']}") return redirect(f"/success?tx={response['hash']}") @@ -929,13 +935,11 @@ def auction(domain): if state == 'CLOSED': if not domainInfo['info']['registered']: if account_module.isOwnDomain(account,domain): - print("Waiting to be registered") state = 'PENDING REGISTER' next = "Pending Register" next_action = f'Register Domain' else: - print("Not registered") state = 'AVAILABLE' next = "Available Now" next_action = f'Open Auction' @@ -1500,7 +1504,7 @@ def plugin(ptype,plugin): plugin = f"{ptype}/{plugin}" if not plugins_module.pluginExists(plugin): - print(f"Plugin {plugin} not found") + logger.warning(f"Plugin not found: {plugin}") return redirect("/plugins") data = plugins_module.getPluginData(plugin) @@ -1635,13 +1639,11 @@ def api_hsd(function): if state == 'CLOSED': if not domainInfo['info']['registered']: if account_module.isOwnDomain(account,domain): - print("Waiting to be registered") state = 'PENDING REGISTER' next = "Pending Register" next_action = f'Register Domain' else: - print("Not registered") state = 'AVAILABLE' next = "Available Now" next_action = f'Open Auction' @@ -1934,7 +1936,6 @@ def get_alerts(account:str) -> list: "name": "Wallet", "output": f"The wallet is not synced ({wallet_status}). Please wait for it to sync." }) - print(account) # Try to read from notifications sqlite database if os.path.exists("user_data/notifications.db"): try: @@ -1950,7 +1951,7 @@ def get_alerts(account:str) -> list: }) conn.close() except Exception as e: - print(f"Error reading notifications: {e}",flush=True) + logger.error(f"Error reading notifications: {e}") pass return alerts @@ -1977,7 +1978,7 @@ def add_alert(name:str,output:str,account:str="all"): conn.commit() conn.close() except Exception as e: - print(f"Error adding notification: {e}",flush=True) + logger.error(f"Error adding notification: {e}") pass def dismiss_alert(alert_id:int,account:str="all"): @@ -1997,7 +1998,7 @@ def dismiss_alert(alert_id:int,account:str="all"): conn.commit() conn.close() except Exception as e: - print(f"Error dismissing notification: {e}",flush=True) + logger.error(f"Error dismissing notification: {e}") pass @app.route('/dismiss/') @@ -2072,16 +2073,19 @@ if __name__ == '__main__': port = int(sys.argv[port_index]) except ValueError: pass - - # Print logs to console if --debug is set + print(f"Starting FireWallet on http://{host}:{port}",flush=True) + if "--debug" in sys.argv: console_handler = logging.StreamHandler(sys.stdout) # Use a simple format for console console_formatter = logging.Formatter('%(message)s') console_handler.setFormatter(console_formatter) + console_handler.setLevel(logging.WARNING) logger.addHandler(console_handler) + logger.setLevel(logging.DEBUG) app.run(debug=True, host=host, port=port) else: + app.run(host=host, port=port) def tests():