formatAndPrint method

void formatAndPrint(
  1. int level,
  2. dynamic message, {
  3. dynamic title,
})

以当前配置对象格式化打印日志

Implementation

void formatAndPrint(int level, dynamic message, {dynamic title}) {
  List<String> buffer = [];
  final stacktrace = formatStackTrace();
  final color =
      this.color ?? Log.levelColors[level] ?? const AnsiColor.none();

  if (lineLength == null || lineLength! <= 0) {
    void addBuffer(String content) {
      for (var line in content.split('\n')) {
        buffer.add(color(line));
      }
    }

    if (printTitle == true && title != null) addBuffer(title.toString());
    if (stacktrace != null) addBuffer(stacktrace);
    addBuffer(stringifyMessage(message));
  } else {
    final verticalLineAtLevel = this.verticalLineAtLevel;

    void addBuffer(String content) {
      for (var line in content.split('\n')) {
        buffer.add(color('$verticalLineAtLevel$line'));
      }
    }

    buffer.add(color(topBorder));

    if (printTitle == true && title != null) {
      addBuffer(title.toString());
      buffer.add(color(middleBorder));
    }

    if (stacktrace != null) {
      addBuffer(stacktrace);
      buffer.add(color(middleBorder));
    }

    addBuffer(stringifyMessage(message));
    buffer.add(color(bottomBorder));
  }

  buffer.forEach(print);
}