fromFile static method

List<DebugConsoleLog> fromFile(
  1. String filePath
)

Implementation

static List<DebugConsoleLog> fromFile(String filePath) {
  final List<DebugConsoleLog> logs = [];

  final File file = File(filePath);
  if (!file.existsSync()) return logs;

  final RegExp regex = RegExp(
    r'\[(?<level>.*)\] (?<timestamp>.*)\n(?<message>(?:.|\n)*?)\n---(?::\n(?<stackTrace>(?:.|\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;
}