parseConsoleLogLine function

({String message, int? stepIndex, DateTime? timestamp}) parseConsoleLogLine(
  1. String line
)

Parses a console log line produced by TestRuntimeState.formatConsoleLine.

Expected forms:

  • [iso8601] message
  • [iso8601][step=N] message

Implementation

({DateTime? timestamp, int? stepIndex, String message}) parseConsoleLogLine(
  String line,
) {
  if (!line.startsWith('[')) {
    return (timestamp: null, stepIndex: null, message: line);
  }
  final firstClose = line.indexOf(']');
  if (firstClose <= 1) {
    return (timestamp: null, stepIndex: null, message: line);
  }
  DateTime? timestamp;
  try {
    timestamp = DateTime.parse(line.substring(1, firstClose));
  } catch (_) {
    return (timestamp: null, stepIndex: null, message: line);
  }

  var rest = line.substring(firstClose + 1);
  int? stepIndex;
  if (rest.startsWith('[step=')) {
    final stepClose = rest.indexOf(']');
    if (stepClose > 6) {
      stepIndex = int.tryParse(rest.substring(6, stepClose));
      rest = rest.substring(stepClose + 1);
    }
  }
  final message = rest.startsWith(' ') ? rest.substring(1) : rest;
  return (timestamp: timestamp, stepIndex: stepIndex, message: message);
}