formatStackTrace method

String? formatStackTrace({
  1. bool showPath = false,
  2. int methodCount = 30,
  3. int skipFrames = 0,
})

Outputs a formatted string of the current StackTraceImpl showing upto methodCount methods in the trace. methodCount defaults to 30.

Implementation

String? formatStackTrace({
  bool showPath = false,
  int methodCount = 30,
  int skipFrames = 0,
}) {
  var _skipFrames = skipFrames;
  final formatted = <String>[];
  var count = 0;

  for (final stackFrame in frames) {
    if (_skipFrames > 0) {
      _skipFrames--;
      continue;
    }
    String sourceFile;
    if (showPath) {
      sourceFile = stackFrame.sourceFile.path;
    } else {
      sourceFile = basename(stackFrame.sourceFile.path);
    }
    final newLine =
        '$sourceFile : ${stackFrame.details} : ${stackFrame.lineNo}';

    if (_workingDirectory != null) {
      formatted.add('file:///$_workingDirectory$newLine');
    } else {
      formatted.add(newLine);
    }
    if (++count == methodCount) {
      break;
    }
  }

  if (formatted.isEmpty) {
    return null;
  } else {
    return formatted.join('\n');
  }
}