getLogLineInfo static method

LogLineInfo getLogLineInfo({
  1. int stackIndex = 4,
})

Gets LogLineInfo with stackIndex which provides data for tag and line of code

Implementation

static LogLineInfo getLogLineInfo({int stackIndex = 4}) {
  ///([a-zA-Z\<\>\s\.]*)\s\(file:\/(.*\.dart):(\d*):(\d*)
  /// group 1 = tag
  /// group 2 = filepath
  /// group 3 = line number
  /// group 4 = column
  /// "#4      main.<anonymous closure>.<anonymous closure> (file:///Users/magillus/Projects/opensource/flutter-fimber/fimber/test/fimber_test.dart:19:14)"
  final stackTraceList = StackTrace.current.toString().split('\n');
  if (stackTraceList.length > stackIndex) {
    final logline = stackTraceList[stackIndex];
    final matches = _logMatcher.allMatches(logline);

    if (matches.isNotEmpty) {
      final match = matches.first;
      return LogLineInfo(
        tag: match
                .group(1)
                ?.trim()
                .replaceAll("<anonymous closure>", "<ac>") ??
            _defaultTag,
        logFilePath: match.group(2),
        lineNumber: int.tryParse(match.group(3) ?? '-1') ?? -1,
        characterIndex: int.tryParse(match.group(4) ?? '-1') ?? -1,
      );
    } else {
      return LogLineInfo(tag: _defaultTag);
    }
  } else {
    return LogLineInfo(tag: _defaultTag);
  }
}