Fix RNS initialization and logging setup

- Moved RNS initialization and logging setup to the main application to ensure proper logging capture before RNS is used.
- Updated AnnounceService and PageFetcher to remove redundant RNS initialization, assuming it is handled in the main app.
- Enhanced settings interface with a refresh button for better user experience and updated error and Reticulum log displays.
This commit is contained in:
2025-09-20 14:17:41 -05:00
parent 66bcf0d25c
commit 7c8e8e41cb
6 changed files with 26 additions and 25 deletions

View File

@@ -8,7 +8,6 @@ from dataclasses import dataclass
import RNS import RNS
from ren_browser.storage.storage import get_rns_config_directory
@dataclass @dataclass
@@ -39,13 +38,7 @@ class AnnounceService:
self.receive_path_responses = True self.receive_path_responses = True
self.announces: list[Announce] = [] self.announces: list[Announce] = []
self.update_callback = update_callback self.update_callback = update_callback
config_dir = get_rns_config_directory() # RNS should already be initialized by main app
try:
RNS.Reticulum(str(config_dir))
from ren_browser.logs import setup_rns_logging
setup_rns_logging()
except (OSError, ValueError):
pass
RNS.Transport.register_announce_handler(self) RNS.Transport.register_announce_handler(self)
RNS.log("AnnounceService: registered announce handler") RNS.log("AnnounceService: registered announce handler")

View File

@@ -43,9 +43,10 @@ async def main(page: Page):
else: else:
config_dir = storage.get_reticulum_config_path() config_dir = storage.get_reticulum_config_path()
try: try:
RNS.Reticulum(str(config_dir)) # Set up logging capture first, before RNS init
import ren_browser.logs import ren_browser.logs
ren_browser.logs.setup_rns_logging() ren_browser.logs.setup_rns_logging()
RNS.Reticulum(str(config_dir))
except (OSError, ValueError): except (OSError, ValueError):
pass pass
page.controls.clear() page.controls.clear()

View File

@@ -28,7 +28,8 @@ def log_ret(msg, *args, **kwargs):
def setup_rns_logging(): def setup_rns_logging():
"""Set up RNS log replacement. Call this after RNS.Reticulum initialization.""" """Set up RNS log replacement. Call this after RNS.Reticulum initialization."""
global _original_rns_log global _original_rns_log
if _original_rns_log != log_ret: # Prevent recursion # Only set up if not already done and if RNS.log is not already our function
if RNS.log != log_ret and _original_rns_log != log_ret:
_original_rns_log = RNS.log _original_rns_log = RNS.log
RNS.log = log_ret RNS.log = log_ret

View File

@@ -9,7 +9,6 @@ from dataclasses import dataclass
import RNS import RNS
from ren_browser.storage.storage import get_rns_config_directory
@dataclass @dataclass
@@ -28,13 +27,7 @@ class PageFetcher:
def __init__(self): def __init__(self):
"""Initialize the page fetcher and Reticulum connection.""" """Initialize the page fetcher and Reticulum connection."""
config_dir = get_rns_config_directory() # RNS should already be initialized by main app
try:
RNS.Reticulum(str(config_dir))
from ren_browser.logs import setup_rns_logging
setup_rns_logging()
except (OSError, ValueError):
pass
def fetch_page(self, req: PageRequest) -> str: def fetch_page(self, req: PageRequest) -> str:
"""Download page content for the given PageRequest. """Download page content for the given PageRequest.

View File

@@ -74,7 +74,7 @@ class StorageManager:
return pathlib.Path(RNS_CONFIG_DIR) return pathlib.Path(RNS_CONFIG_DIR)
except ImportError: except ImportError:
pass pass
# Default to standard RNS config directory # Default to standard RNS config directory
return pathlib.Path.home() / ".reticulum" return pathlib.Path.home() / ".reticulum"
@@ -96,7 +96,7 @@ class StorageManager:
reticulum_config_path = self.get_reticulum_config_path() / "config" reticulum_config_path = self.get_reticulum_config_path() / "config"
reticulum_config_path.parent.mkdir(parents=True, exist_ok=True) reticulum_config_path.parent.mkdir(parents=True, exist_ok=True)
reticulum_config_path.write_text(config_content, encoding="utf-8") reticulum_config_path.write_text(config_content, encoding="utf-8")
# Also save to local config path as backup # Also save to local config path as backup
config_path = self.get_config_path() config_path = self.get_config_path()
config_path.write_text(config_content, encoding="utf-8") config_path.write_text(config_content, encoding="utf-8")

View File

@@ -42,18 +42,16 @@ def open_settings_tab(page: ft.Page, tab_manager):
page.snack_bar = ft.SnackBar(ft.Text(f"Error saving config: {ex}"), open=True) page.snack_bar = ft.SnackBar(ft.Text(f"Error saving config: {ex}"), open=True)
page.update() page.update()
save_btn = ft.ElevatedButton("Save and Restart", on_click=on_save_config) save_btn = ft.ElevatedButton("Save and Restart", on_click=on_save_config)
error_text = "\n".join(ERROR_LOGS) or "No errors logged."
error_field = ft.TextField( error_field = ft.TextField(
label="Error Logs", label="Error Logs",
value=error_text, value="",
expand=True, expand=True,
multiline=True, multiline=True,
read_only=True, read_only=True,
) )
ret_text = "\n".join(RET_LOGS) or "No Reticulum logs."
ret_field = ft.TextField( ret_field = ft.TextField(
label="Reticulum logs", label="Reticulum logs",
value=ret_text, value="",
expand=True, expand=True,
multiline=True, multiline=True,
read_only=True, read_only=True,
@@ -75,19 +73,34 @@ def open_settings_tab(page: ft.Page, tab_manager):
content_placeholder.content = config_field content_placeholder.content = config_field
page.update() page.update()
def show_errors(ev): def show_errors(ev):
error_field.value = "\n".join(ERROR_LOGS) or "No errors logged."
content_placeholder.content = error_field content_placeholder.content = error_field
page.update() page.update()
def show_ret_logs(ev): def show_ret_logs(ev):
ret_field.value = "\n".join(RET_LOGS) or "No Reticulum logs."
content_placeholder.content = ret_field content_placeholder.content = ret_field
page.update() page.update()
def show_storage_info(ev): def show_storage_info(ev):
storage_info = storage.get_storage_info()
storage_field.value = "\n".join([f"{key}: {value}" for key, value in storage_info.items()])
content_placeholder.content = storage_field content_placeholder.content = storage_field
page.update() page.update()
def refresh_current_view(ev):
# Refresh the currently displayed content
if content_placeholder.content == error_field:
show_errors(ev)
elif content_placeholder.content == ret_field:
show_ret_logs(ev)
elif content_placeholder.content == storage_field:
show_storage_info(ev)
elif content_placeholder.content == config_field:
show_config(ev)
btn_config = ft.ElevatedButton("Config", on_click=show_config) btn_config = ft.ElevatedButton("Config", on_click=show_config)
btn_errors = ft.ElevatedButton("Errors", on_click=show_errors) btn_errors = ft.ElevatedButton("Errors", on_click=show_errors)
btn_ret = ft.ElevatedButton("Ret Logs", on_click=show_ret_logs) btn_ret = ft.ElevatedButton("Ret Logs", on_click=show_ret_logs)
btn_storage = ft.ElevatedButton("Storage", on_click=show_storage_info) btn_storage = ft.ElevatedButton("Storage", on_click=show_storage_info)
button_row = ft.Row(controls=[btn_config, btn_errors, btn_ret, btn_storage]) btn_refresh = ft.ElevatedButton("Refresh", on_click=refresh_current_view)
button_row = ft.Row(controls=[btn_config, btn_errors, btn_ret, btn_storage, btn_refresh])
content_placeholder.content = config_field content_placeholder.content = config_field
settings_content = ft.Column( settings_content = ft.Column(
expand=True, expand=True,