import 'dart:io'; import 'package:flutter/material.dart'; class DebugConsoleLog { /// Message of the log. final String message; /// Log level, used to determine the color and importance of the log. final DebugConsoleLevel level; /// Time of when the log was created. final DateTime timestamp; final StackTrace? stackTrace; DebugConsoleLog({ Object? message, this.level = DebugConsoleLevel.normal, DateTime? timestamp, this.stackTrace, }) : message = '$message', timestamp = timestamp ?? DateTime.now(); @override String toString() { return '[$level] $timestamp\n$message\n---${stackTrace != null ? ':' : ''}\n${stackTrace != null ? '${stackTrace ?? ''}---\n' : ''}'; } static List fromFile(String filePath) { final List logs = []; final File file = File(filePath); if (!file.existsSync()) return logs; final RegExp regex = RegExp( r'\[(?.*)\] (?.*)\n(?(?:.|\n)*?)\n---(?::\n(?(?:.|\n)*?)---\n)?', multiLine: true, ); final matches = regex.allMatches(file.readAsStringSync()); for (final match in matches) { final String level = match.namedGroup('level') ?? 'normal'; final String timestamp = match.namedGroup('timestamp') ?? ''; final String message = match.namedGroup('message') ?? ''; final String stackTrace = match.namedGroup('stackTrace') ?? ''; logs.add(DebugConsoleLog( message: message, level: DebugConsoleLevel.values.firstWhere( (DebugConsoleLevel findingLevel) => level.toString() == findingLevel.toString(), orElse: () => DebugConsoleLevel.normal, ), timestamp: DateTime.parse(timestamp), stackTrace: stackTrace.isNotEmpty ? StackTrace.fromString(stackTrace) : null, )); } return logs; } } class DebugConsoleLevel { static const DebugConsoleLevel normal = DebugConsoleLevel('Normal'); static const DebugConsoleLevel info = DebugConsoleLevel('Info', Colors.lightBlueAccent); static const DebugConsoleLevel warning = DebugConsoleLevel('Warning', Colors.deepOrange); static const DebugConsoleLevel error = DebugConsoleLevel('Error', Colors.redAccent); static const DebugConsoleLevel fatal = DebugConsoleLevel('Fatal', Colors.red); static const DebugConsoleLevel debug = DebugConsoleLevel('Debug', Colors.blue); static const List values = [ normal, info, warning, error, fatal, debug, ]; final String name; final Color? color; const DebugConsoleLevel(this.name, [this.color]); @override String toString() => name; }