|
| 1 | +// This file is for web platform only - uses dart:html |
| 2 | +// For non-web, see debug_logger_stub.dart |
| 3 | +// Export via debug_logger_io.dart for conditional import |
| 4 | + |
| 5 | +// ignore: avoid_web_libraries_in_flutter |
| 6 | +import 'dart:html' as html show window; |
| 7 | +// ignore: avoid_web_libraries_in_flutter |
| 8 | +import 'dart:js_util' as js_util; |
| 9 | + |
| 10 | +import 'package:flutter/foundation.dart'; |
| 11 | + |
| 12 | +/// Debug logging utility that mirrors MeshMapper_WebClient debug system. |
| 13 | +/// |
| 14 | +/// Logs are only output when DEBUG_ENABLED is true (set via `?debug=1` URL param). |
| 15 | +/// All log messages should use tagged format: `[TAG] message` |
| 16 | +/// |
| 17 | +/// Common tags: [BLE], [GPS], [PING], [API], [RX], [UI], [CONN] |
| 18 | +class DebugLogger { |
| 19 | + static bool _debugEnabled = false; |
| 20 | + static bool _initialized = false; |
| 21 | + |
| 22 | + /// Initialize the debug logger by checking URL parameters. |
| 23 | + /// Call this early in app startup (e.g., in main.dart). |
| 24 | + static void initialize() { |
| 25 | + if (_initialized) return; |
| 26 | + _initialized = true; |
| 27 | + |
| 28 | + if (kIsWeb) { |
| 29 | + try { |
| 30 | + // Parse URL parameters from browser |
| 31 | + final uri = Uri.base; |
| 32 | + final debugParam = uri.queryParameters['debug']; |
| 33 | + _debugEnabled = debugParam == '1' || debugParam == 'true'; |
| 34 | + |
| 35 | + if (_debugEnabled) { |
| 36 | + _consoleLog('[DEBUG] Debug logging ENABLED via URL param'); |
| 37 | + } |
| 38 | + } catch (e) { |
| 39 | + // Fallback - URL parsing failed |
| 40 | + _debugEnabled = false; |
| 41 | + } |
| 42 | + } else { |
| 43 | + // On mobile, check for debug mode compile flag |
| 44 | + _debugEnabled = kDebugMode; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Check if debug logging is enabled |
| 49 | + static bool get isEnabled => _debugEnabled; |
| 50 | + |
| 51 | + /// Manually enable/disable debug logging (for testing) |
| 52 | + static void setEnabled(bool enabled) { |
| 53 | + _debugEnabled = enabled; |
| 54 | + } |
| 55 | + |
| 56 | + /// Log a general info message to the console. |
| 57 | + /// Use tagged format: debugLog('[BLE] Connected to device'); |
| 58 | + static void log(String message, [Object? arg1, Object? arg2, Object? arg3]) { |
| 59 | + if (!_debugEnabled) return; |
| 60 | + |
| 61 | + final args = [message, if (arg1 != null) arg1, if (arg2 != null) arg2, if (arg3 != null) arg3]; |
| 62 | + |
| 63 | + if (kIsWeb) { |
| 64 | + _consoleLog(args.join(' ')); |
| 65 | + } else { |
| 66 | + debugPrint(args.join(' ')); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Log a warning message to the console. |
| 71 | + /// Use tagged format: debugWarn('[GPS] Position stale, re-acquiring'); |
| 72 | + static void warn(String message, [Object? arg1, Object? arg2, Object? arg3]) { |
| 73 | + if (!_debugEnabled) return; |
| 74 | + |
| 75 | + final args = ['⚠️', message, if (arg1 != null) arg1, if (arg2 != null) arg2, if (arg3 != null) arg3]; |
| 76 | + |
| 77 | + if (kIsWeb) { |
| 78 | + _consoleWarn(args.join(' ')); |
| 79 | + } else { |
| 80 | + debugPrint('WARN: ${args.join(' ')}'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// Log an error message to the console. |
| 85 | + /// Use tagged format: debugError('[API] Failed to post queue', error); |
| 86 | + static void error(String message, [Object? arg1, Object? arg2, Object? arg3]) { |
| 87 | + if (!_debugEnabled) return; |
| 88 | + |
| 89 | + final args = ['❌', message, if (arg1 != null) arg1, if (arg2 != null) arg2, if (arg3 != null) arg3]; |
| 90 | + |
| 91 | + if (kIsWeb) { |
| 92 | + _consoleError(args.join(' ')); |
| 93 | + } else { |
| 94 | + debugPrint('ERROR: ${args.join(' ')}'); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Web console methods using dart:js_util for proper browser console output |
| 99 | + static void _consoleLog(String message) { |
| 100 | + try { |
| 101 | + js_util.callMethod(html.window.console, 'log', [message]); |
| 102 | + } catch (e) { |
| 103 | + // Fallback |
| 104 | + // ignore: avoid_print |
| 105 | + print(message); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static void _consoleWarn(String message) { |
| 110 | + try { |
| 111 | + js_util.callMethod(html.window.console, 'warn', [message]); |
| 112 | + } catch (e) { |
| 113 | + // ignore: avoid_print |
| 114 | + print('WARN: $message'); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + static void _consoleError(String message) { |
| 119 | + try { |
| 120 | + js_util.callMethod(html.window.console, 'error', [message]); |
| 121 | + } catch (e) { |
| 122 | + // ignore: avoid_print |
| 123 | + print('ERROR: $message'); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Convenience global functions matching MeshMapper_WebClient API |
| 129 | +void debugLog(String message, [Object? arg1, Object? arg2, Object? arg3]) { |
| 130 | + DebugLogger.log(message, arg1, arg2, arg3); |
| 131 | +} |
| 132 | + |
| 133 | +void debugWarn(String message, [Object? arg1, Object? arg2, Object? arg3]) { |
| 134 | + DebugLogger.warn(message, arg1, arg2, arg3); |
| 135 | +} |
| 136 | + |
| 137 | +void debugError(String message, [Object? arg1, Object? arg2, Object? arg3]) { |
| 138 | + DebugLogger.error(message, arg1, arg2, arg3); |
| 139 | +} |
0 commit comments