bootstrap function

void bootstrap(
  1. BodyFunc body, {
  2. List<String> args = const [],
  3. ExitFunc? onExit,
  4. Iterable<ProcessSignal> signals = allSignals,
  5. bool exitAfterBody = true,
  6. bool? enableChildProcess = ifNotDebugging,
  7. bool? enableLogFiles = ifNotDebugging,
  8. String fileOut = 'logs/out.log',
  9. String fileErr = 'logs/err.log',
  10. Logger formatterStd = logPassthrough,
  11. Logger formatterFile = logTimestamp,
})

Allows running this program with a custom exiting function and optional file logging.

This bootstrapper normally starts a new child process to make sure your program doesn't exit unexpectedly. Child process spawning is disabled in debug mode, as breakpoints are not properly triggered. Pass enableChildProcess: true to enable it anyway.

Log files are disabled in debug mode by default and can be enabled with enableLogFiles: true.

It's recommended to return bootstrap() directly from your main function.

// Dart entry
void main(List<String> args) {
  return bootstrap(run, args: args, onExit: onExit);
}

void run(List<String> args) {
  // Your program...
}

Future<int> onExit() async {
  // Perform cleanup...
  return 0;
}

Implementation

void bootstrap(
  BodyFunc body, {
  List<String> args = const [],
  ExitFunc? onExit,
  Iterable<ProcessSignal> signals = allSignals,
  bool exitAfterBody = true,
  bool? enableChildProcess = ifNotDebugging,
  bool? enableLogFiles = ifNotDebugging,
  String fileOut = 'logs/out.log',
  String fileErr = 'logs/err.log',
  Logger formatterStd = logPassthrough,
  Logger formatterFile = logTimestamp,
}) async {
  var bootstrapper = Bootstrapper(
    body: body,
    args: args,
    signals: signals,
  );

  enableChildProcess ??= !isDebugMode;
  enableLogFiles ??= !isDebugMode;

  if (!enableChildProcess || Platform.environment.containsKey(argUnlock)) {
    bootstrapper.runAsWorker(
      enableLogFiles: enableLogFiles,
      outLog: fileOut,
      errLog: fileErr,
      loggerStd: formatterStd,
      loggerFile: formatterFile,
      exitAfterBody: exitAfterBody,
      isChildProcess: enableChildProcess,
      onExit: onExit,
    );
  } else {
    bootstrapper.runAsWrapper();
  }
}