graceful 1.2.0 copy "graceful: ^1.2.0" to clipboard
graceful: ^1.2.0 copied to clipboard

Handle exit signals gracefully and pipe console output to log files.

example/graceful_example.dart

import 'package:graceful/graceful.dart';

// You can use a single file for both `out` and `err` messages.
const exampleLogFile = 'example/logs/out.log';

void main(List<String> args) {
  return bootstrap(
    run,
    args: args,
    onExit: onExit,
    fileOut: exampleLogFile,
    fileErr: exampleLogFile,

    // Set this to false if your `run` method starts an indefinite process,
    // for example a server socket listening to incoming events.
    exitAfterBody: true,
  );
}

void run(List<String> args) async {
  print('Program start');
  print('Waiting for 5 seconds...');
  await Future.delayed(Duration(seconds: 5));

  print('Done.');

  while (true) {
    await Future.delayed(Duration(seconds: 1));
    print('bump');
  }
}

Future<int> onExit() async {
  print('Cleaning up...');
  await Future.delayed(Duration(seconds: 1));
  print('Program exit');

  return 0; // Exit code
}
3
likes
160
points
42
downloads

Publisher

verified publishertheoretically.online

Weekly Downloads

Handle exit signals gracefully and pipe console output to log files.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

path

More

Packages that depend on graceful