| 1 | import 'dart:convert'; |
| 2 | import 'package:cake_wallet/core/utilities.dart'; |
| 3 | import 'package:cake_wallet/exchange/exchange_provider_description.dart'; |
| 4 | |
| 5 | class ExchangeProviderLogEntry { |
| 6 | final DateTime timestamp; |
| 7 | final ExchangeProviderDescription? provider; |
| 8 | final String function; |
| 9 | final String? error; |
| 10 | final String? stackTrace; |
| 11 | final String? callStack; |
| 12 | final Map<String, dynamic>? requestData; |
| 13 | final Map<String, dynamic>? responseData; |
| 14 | final bool isSuccess; |
| 15 | |
| 16 | ExchangeProviderLogEntry({ |
| 17 | required this.timestamp, |
| 18 | required this.function, |
| 19 | this.provider, |
| 20 | this.error, |
| 21 | this.stackTrace, |
| 22 | this.callStack, |
| 23 | this.requestData, |
| 24 | this.responseData, |
| 25 | required this.isSuccess, |
| 26 | }); |
| 27 | |
| 28 | String toLogString() { |
| 29 | final buffer = StringBuffer(); |
| 30 | buffer.writeln('Provider: ${provider?.title ?? 'Unknown/Empty'}'); |
| 31 | buffer.writeln('Function: $function'); |
| 32 | buffer.writeln('Timestamp: ${timestamp.toIso8601String()}'); |
| 33 | buffer.writeln('Success: $isSuccess'); |
| 34 | |
| 35 | if (error != null) { |
| 36 | buffer.writeln('Error: $error'); |
| 37 | } |
| 38 | |
| 39 | if (stackTrace != null) { |
| 40 | buffer.writeln('StackTrace: $stackTrace'); |
| 41 | } |
| 42 | |
| 43 | if (callStack != null) { |
| 44 | buffer.writeln('CallStack: $callStack'); |
| 45 | } |
| 46 | |
| 47 | if (requestData != null) { |
| 48 | buffer.writeln('Request: ${json.encode(requestData)}'); |
| 49 | } |
| 50 | |
| 51 | if (responseData != null) { |
| 52 | buffer.writeln('Response: ${json.encode(responseData)}'); |
| 53 | } |
| 54 | |
| 55 | buffer.writeln('---'); |
| 56 | return buffer.toString(); |
| 57 | } |
| 58 | |
| 59 | Map<String, dynamic> toJson() { |
| 60 | return { |
| 61 | 'timestamp': timestamp.toIso8601String(), |
| 62 | 'provider': provider?.title, |
| 63 | 'function': function, |
| 64 | 'error': error, |
| 65 | 'stackTrace': stackTrace, |
| 66 | 'callStack': callStack, |
| 67 | 'requestData': requestData, |
| 68 | 'responseData': responseData, |
| 69 | 'isSuccess': isSuccess, |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | factory ExchangeProviderLogEntry.fromJson(Map<String, dynamic> json) { |
| 74 | final allProviders = [ |
| 75 | ExchangeProviderDescription.xmrto, |
| 76 | ExchangeProviderDescription.changeNow, |
| 77 | ExchangeProviderDescription.morphToken, |
| 78 | ExchangeProviderDescription.sideShift, |
| 79 | ExchangeProviderDescription.simpleSwap, |
| 80 | ExchangeProviderDescription.trocador, |
| 81 | ExchangeProviderDescription.exolix, |
| 82 | ExchangeProviderDescription.all, |
| 83 | ExchangeProviderDescription.thorChain, |
| 84 | ExchangeProviderDescription.swapTrade, |
| 85 | ExchangeProviderDescription.letsExchange, |
| 86 | ExchangeProviderDescription.stealthEx, |
| 87 | ExchangeProviderDescription.chainflip, |
| 88 | ExchangeProviderDescription.xoSwap, |
| 89 | ExchangeProviderDescription.swapsXyz, |
| 90 | ExchangeProviderDescription.nearIntents, |
| 91 | ExchangeProviderDescription.jupiter, |
| 92 | ]; |
| 93 | |
| 94 | return ExchangeProviderLogEntry( |
| 95 | timestamp: DateTime.parse(json['timestamp'] as String), |
| 96 | provider: allProviders.firstWhereOrNull((p) => p.title == json['provider'] as String), |
| 97 | function: json['function'] as String, |
| 98 | error: json['error'] as String?, |
| 99 | stackTrace: json['stackTrace'] as String?, |
| 100 | callStack: json['callStack'] as String?, |
| 101 | requestData: json['requestData'] as Map<String, dynamic>?, |
| 102 | responseData: json['responseData'] as Map<String, dynamic>?, |
| 103 | isSuccess: json['isSuccess'] as bool, |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | class ExchangeProviderLogger { |
| 109 | static final List<ExchangeProviderLogEntry> _logs = []; |
| 110 | static const int maxLogs = 100; |
| 111 | |
| 112 | static List<ExchangeProviderLogEntry> get logs => List.unmodifiable(_logs); |
| 113 | |
| 114 | static void logSuccess({ |
| 115 | required ExchangeProviderDescription provider, |
| 116 | required String function, |
| 117 | Map<String, dynamic>? requestData, |
| 118 | Map<String, dynamic>? responseData, |
| 119 | String? callStack, |
| 120 | }) { |
| 121 | final entry = ExchangeProviderLogEntry( |
| 122 | timestamp: DateTime.now(), |
| 123 | provider: provider, |
| 124 | function: function, |
| 125 | requestData: requestData, |
| 126 | responseData: responseData, |
| 127 | callStack: callStack, |
| 128 | isSuccess: true, |
| 129 | ); |
| 130 | |
| 131 | _addLog(entry); |
| 132 | } |
| 133 | |
| 134 | static void logError({ |
| 135 | required String function, |
| 136 | required dynamic error, |
| 137 | ExchangeProviderDescription? provider, |
| 138 | StackTrace? stackTrace, |
| 139 | Map<String, dynamic>? requestData, |
| 140 | String? callStack, |
| 141 | }) { |
| 142 | final entry = ExchangeProviderLogEntry( |
| 143 | timestamp: DateTime.now(), |
| 144 | provider: provider, |
| 145 | function: function, |
| 146 | error: error.toString(), |
| 147 | stackTrace: stackTrace?.toString(), |
| 148 | requestData: requestData, |
| 149 | callStack: callStack, |
| 150 | isSuccess: false, |
| 151 | ); |
| 152 | |
| 153 | _addLog(entry); |
| 154 | } |
| 155 | |
| 156 | static void _addLog(ExchangeProviderLogEntry entry) { |
| 157 | _logs.insert(0, entry); |
| 158 | |
| 159 | if (_logs.length > maxLogs * 2) { |
| 160 | final excessCount = _logs.length - maxLogs; |
| 161 | _logs.removeRange(0, excessCount); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void clearLogs() { |
| 166 | _logs.clear(); |
| 167 | } |
| 168 | |
| 169 | static String getLogsAsText() { |
| 170 | if (_logs.isEmpty) return 'No exchange provider logs available'; |
| 171 | |
| 172 | final buffer = StringBuffer(); |
| 173 | buffer.writeln('Exchange Provider Logs'); |
| 174 | buffer.writeln('Generated: ${DateTime.now().toIso8601String()}'); |
| 175 | buffer.writeln('Total logs: ${_logs.length}'); |
| 176 | buffer.writeln('Success logs: ${_logs.where((log) => log.isSuccess).length}'); |
| 177 | buffer.writeln('Error logs: ${_logs.where((log) => !log.isSuccess).length}'); |
| 178 | buffer.writeln(''); |
| 179 | |
| 180 | for (final log in _logs) { |
| 181 | buffer.writeln(log.toLogString()); |
| 182 | } |
| 183 | |
| 184 | return buffer.toString(); |
| 185 | } |
| 186 | |
| 187 | static String getLogsAsJson() { |
| 188 | return json.encode(_logs.map((log) => log.toJson()).toList()); |
| 189 | } |
| 190 | } |