StringBuffer colorLog()

in build_runner/lib/src/logging/std_io_logging.dart [17:58]


StringBuffer colorLog(LogRecord record, {required bool verbose}) {
  AnsiCode color;
  if (record.level < Level.WARNING) {
    color = cyan;
  } else if (record.level < Level.SEVERE) {
    color = yellow;
  } else {
    color = red;
  }
  final level = color.wrap('[${record.level}]');
  final eraseLine = ansiOutputEnabled && !verbose ? '\x1b[2K\r' : '';
  var lines = <Object>[
    '$eraseLine$level ${_recordHeader(record, verbose)}${record.message}'
  ];

  if (record.error != null) {
    lines.add(record.error!);
  }

  if (record.stackTrace != null && verbose) {
    var trace = Trace.from(record.stackTrace!);
    const buildSystem = {'build_runner', 'build_runner_core', 'build'};
    if (trace.frames.isNotEmpty &&
        !buildSystem.contains(trace.frames.first.package)) {
      trace =
          trace.foldFrames((f) => buildSystem.contains(f.package), terse: true);
    }
    lines.add(trace);
  }

  var message = StringBuffer(lines.join('\n'));

  // We always add an extra newline at the end of each message, so it
  // isn't multiline unless we see > 2 lines.
  var multiLine = LineSplitter.split(message.toString()).length > 2;

  if (record.level > Level.INFO || !ansiOutputEnabled || multiLine || verbose) {
    // Add an extra line to the output so the last line isn't written over.
    message.writeln('');
  }
  return message;
}