formatStackTrace method

String? formatStackTrace({
  1. bool showPath = false,
  2. int methodCount = 10,
})

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

Implementation

String? formatStackTrace({bool showPath = false, int methodCount = 10}) {
  List<String> formatted = <String>[];
  int count = 0;

  for (Stackframe stackFrame in frames!) {
    // if (stackFrame.sourceFile.contains('log.dart') ||
    //     stackFrame.sourceFile.contains('package:logger')) {
    //   continue;
    // }

    String sourceFile;
    if (showPath) {
      sourceFile = stackFrame.sourceFile.path;
    } else {
      sourceFile = basename(stackFrame.sourceFile.path);
    }
    String newLine = '$sourceFile:${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');
  }
}