startLogFile static method

void startLogFile(
  1. List<String> arguments
)

Starts a fresh log file and writes the run header.

The header carries the full date, the CLI version, the working directory and the exact invocation, which is what turns a pasted log into a reproducible bug report. Timestamps on the following lines are time-only, so the date has to live here.

Refuses to touch anything that is not a regular file: --log-file pointing at a directory must never delete it.

Implementation

static void startLogFile(List<String> arguments) {
  if (!fileLoggingEnabled) return;
  final file = File(logFilePath);

  if (FileSystemEntity.isDirectorySync(logFilePath)) {
    stderr.writeln(
      'Refusing to use "$logFilePath" as a log file: it is a directory.',
    );
    return;
  }

  try {
    if (file.existsSync()) file.deleteSync();
    file.parent.createSync(recursive: true);
    file.writeAsStringSync(
      '# distribute_cli $packageVersion\n'
      '# ${DateTime.now().toIso8601String()}\n'
      '# cwd: ${Directory.current.path}\n'
      '# args: ${maskSecretArguments(arguments)}\n',
    );
  } on FileSystemException {
    // Logging to a file is best-effort; never abort the run over it.
  }
}