|
| 1 | +// Dart imports: |
| 2 | +import 'dart:collection'; |
| 3 | +import 'dart:io'; |
| 4 | + |
| 5 | +// Flutter imports: |
| 6 | +import 'package:flutter/foundation.dart'; |
| 7 | + |
| 8 | +// Package imports: |
| 9 | +import 'package:path_provider/path_provider.dart'; |
| 10 | +import 'package:share_plus/share_plus.dart'; |
| 11 | + |
| 12 | +/// Log entry class to store individual log messages |
| 13 | +class LogEntry { |
| 14 | + final DateTime timestamp; |
| 15 | + final String level; |
| 16 | + final String message; |
| 17 | + final String? tag; |
| 18 | + final dynamic error; |
| 19 | + final StackTrace? stackTrace; |
| 20 | + |
| 21 | + LogEntry({ |
| 22 | + required this.timestamp, |
| 23 | + required this.level, |
| 24 | + required this.message, |
| 25 | + this.tag, |
| 26 | + this.error, |
| 27 | + this.stackTrace, |
| 28 | + }); |
| 29 | + |
| 30 | + @override |
| 31 | + String toString() { |
| 32 | + final buffer = StringBuffer(); |
| 33 | + buffer.write('[${timestamp.toIso8601String()}] '); |
| 34 | + buffer.write('[$level] '); |
| 35 | + if (tag != null) { |
| 36 | + buffer.write('[$tag] '); |
| 37 | + } |
| 38 | + buffer.write(message); |
| 39 | + if (error != null) { |
| 40 | + buffer.write('\nError: $error'); |
| 41 | + } |
| 42 | + if (stackTrace != null) { |
| 43 | + buffer.write('\nStack trace:\n$stackTrace'); |
| 44 | + } |
| 45 | + return buffer.toString(); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Logger service to capture and export app logs |
| 50 | +class AppLogger { |
| 51 | + static final AppLogger _instance = AppLogger._internal(); |
| 52 | + factory AppLogger() => _instance; |
| 53 | + AppLogger._internal(); |
| 54 | + |
| 55 | + // Store logs for the past 5 minutes |
| 56 | + final Queue<LogEntry> _logs = Queue<LogEntry>(); |
| 57 | + static const Duration _logRetentionDuration = Duration(minutes: 5); |
| 58 | + static const int _maxLogEntries = 1000; // Limit to prevent memory issues |
| 59 | + |
| 60 | + /// Add a log entry |
| 61 | + void _addLog(String level, String message, {String? tag, dynamic error, StackTrace? stackTrace}) { |
| 62 | + final entry = LogEntry( |
| 63 | + timestamp: DateTime.now(), |
| 64 | + level: level, |
| 65 | + message: message, |
| 66 | + tag: tag, |
| 67 | + error: error, |
| 68 | + stackTrace: stackTrace, |
| 69 | + ); |
| 70 | + |
| 71 | + _logs.addLast(entry); |
| 72 | + |
| 73 | + // Also print to console in debug mode |
| 74 | + if (kDebugMode) { |
| 75 | + debugPrint(entry.toString()); |
| 76 | + } |
| 77 | + |
| 78 | + // Remove old logs |
| 79 | + _cleanOldLogs(); |
| 80 | + |
| 81 | + // Limit log size |
| 82 | + while (_logs.length > _maxLogEntries) { |
| 83 | + _logs.removeFirst(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Remove logs older than 5 minutes |
| 88 | + void _cleanOldLogs() { |
| 89 | + final cutoffTime = DateTime.now().subtract(_logRetentionDuration); |
| 90 | + while (_logs.isNotEmpty && _logs.first.timestamp.isBefore(cutoffTime)) { |
| 91 | + _logs.removeFirst(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// Log debug message |
| 96 | + void debug(String message, {String? tag}) { |
| 97 | + _addLog('DEBUG', message, tag: tag); |
| 98 | + } |
| 99 | + |
| 100 | + /// Log info message |
| 101 | + void info(String message, {String? tag}) { |
| 102 | + _addLog('INFO', message, tag: tag); |
| 103 | + } |
| 104 | + |
| 105 | + /// Log warning message |
| 106 | + void warning(String message, {String? tag, dynamic error}) { |
| 107 | + _addLog('WARNING', message, tag: tag, error: error); |
| 108 | + } |
| 109 | + |
| 110 | + /// Log error message |
| 111 | + void error(String message, {String? tag, dynamic error, StackTrace? stackTrace}) { |
| 112 | + _addLog('ERROR', message, tag: tag, error: error, stackTrace: stackTrace); |
| 113 | + } |
| 114 | + |
| 115 | + /// Get all logs as a formatted string |
| 116 | + String getAllLogs() { |
| 117 | + _cleanOldLogs(); |
| 118 | + |
| 119 | + final buffer = StringBuffer(); |
| 120 | + buffer.writeln('=== Openlib App Logs ==='); |
| 121 | + buffer.writeln('Generated: ${DateTime.now().toIso8601String()}'); |
| 122 | + buffer.writeln('Log retention: Last ${_logRetentionDuration.inMinutes} minutes'); |
| 123 | + buffer.writeln('Total entries: ${_logs.length}'); |
| 124 | + buffer.writeln(''); |
| 125 | + buffer.writeln('=== System Information ==='); |
| 126 | + buffer.writeln('Platform: ${Platform.operatingSystem} ${Platform.operatingSystemVersion}'); |
| 127 | + buffer.writeln('Dart version: ${Platform.version}'); |
| 128 | + buffer.writeln(''); |
| 129 | + buffer.writeln('=== Log Entries ==='); |
| 130 | + |
| 131 | + for (final log in _logs) { |
| 132 | + buffer.writeln(log.toString()); |
| 133 | + buffer.writeln(''); |
| 134 | + } |
| 135 | + |
| 136 | + return buffer.toString(); |
| 137 | + } |
| 138 | + |
| 139 | + /// Export logs to a file and share |
| 140 | + Future<void> exportLogs() async { |
| 141 | + try { |
| 142 | + final logsContent = getAllLogs(); |
| 143 | + |
| 144 | + // Get temporary directory |
| 145 | + final tempDir = await getTemporaryDirectory(); |
| 146 | + final timestamp = DateTime.now().toIso8601String().replaceAll(':', '-').replaceAll('.', '-'); |
| 147 | + final file = File('${tempDir.path}/openlib_logs_$timestamp.txt'); |
| 148 | + |
| 149 | + // Write logs to file |
| 150 | + await file.writeAsString(logsContent); |
| 151 | + |
| 152 | + // Share the file |
| 153 | + await Share.shareXFiles( |
| 154 | + [XFile(file.path)], |
| 155 | + subject: 'Openlib App Logs - $timestamp', |
| 156 | + text: 'Openlib app logs for the past ${_logRetentionDuration.inMinutes} minutes', |
| 157 | + ); |
| 158 | + |
| 159 | + info('Logs exported successfully', tag: 'AppLogger'); |
| 160 | + } catch (e, stackTrace) { |
| 161 | + error('Failed to export logs', tag: 'AppLogger', error: e, stackTrace: stackTrace); |
| 162 | + rethrow; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// Clear all logs |
| 167 | + void clearLogs() { |
| 168 | + _logs.clear(); |
| 169 | + info('Logs cleared', tag: 'AppLogger'); |
| 170 | + } |
| 171 | + |
| 172 | + /// Get log count |
| 173 | + int get logCount { |
| 174 | + _cleanOldLogs(); |
| 175 | + return _logs.length; |
| 176 | + } |
| 177 | +} |
0 commit comments