"""Diagnostics mirrored to a file, since this device's logcat surfaces neither Chaquopy stdout nor android_utils.log output. The file lives under `get_files_dir()` (app external storage), so it is readable over adb: adb shell cat "/sdcard/Android/data/com.exteragram.messenger/files/exteraGram/exteraGram Files/message_logger/diag.log" """ import os import time import traceback _MAX_BYTES = 256 * 1024 _path = None def _file(): global _path if _path is None: try: from file_utils import get_files_dir directory = os.path.join(get_files_dir(), "message_logger") os.makedirs(directory, exist_ok=True) _path = os.path.join(directory, "diag.log") except Exception: _path = "" return _path def log(message): line = time.strftime("%H:%M:%S ") + str(message) try: from android_utils import log as android_log android_log("[message_logger] " + line) except Exception: pass path = _file() if not path: return try: mode = "a" if os.path.exists(path) and os.path.getsize(path) > _MAX_BYTES: mode = "w" with open(path, mode, encoding="utf-8") as handle: handle.write(line + "\n") except Exception: pass def error(where, exc=None): detail = traceback.format_exc() if exc is not None else "" log(f"ERROR {where}: {exc!r}\n{detail}")