stdio.dart 2.56 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:io' as io;
6 7 8 9

import 'package:meta/meta.dart';

abstract class Stdio {
10 11
  final List<String> logs = <String>[];

12 13 14 15 16
  /// Error messages printed to STDERR.
  ///
  /// Display an error `message` to the user on stderr. Print errors if the code
  /// fails in some way. Errors are typically followed shortly by exiting the
  /// app with a non-zero exit status.
17 18 19 20
  @mustCallSuper
  void printError(String message) {
    logs.add('[error] $message');
  }
21

22 23 24 25 26 27 28 29 30
  /// Warning messages printed to STDERR.
  ///
  /// Display a warning `message` to the user on stderr. Print warnings if there
  /// is important information to convey to the user that is not fatal.
  @mustCallSuper
  void printWarning(String message) {
    logs.add('[warning] $message');
  }

31
  /// Ordinary STDOUT messages.
32 33 34
  ///
  /// Displays normal output on stdout. This should be used for things like
  /// progress messages, success messages, or just normal command output.
35 36 37 38
  @mustCallSuper
  void printStatus(String message) {
    logs.add('[status] $message');
  }
39 40

  /// Debug messages that are only printed in verbose mode.
41 42 43
  ///
  /// Use this for verbose tracing output. Users can turn this output on in order
  /// to help diagnose issues.
44 45 46 47
  @mustCallSuper
  void printTrace(String message) {
    logs.add('[trace] $message');
  }
48

49
  /// Write the `message` string to STDOUT without a trailing newline.
50 51 52 53
  @mustCallSuper
  void write(String message) {
    logs.add('[write] $message');
  }
54 55 56 57 58 59 60 61

  /// Read a line of text from STDIN.
  String readLineSync();
}

/// A logger that will print out trace messages.
class VerboseStdio extends Stdio {
  VerboseStdio({
62 63 64 65
    required this.stdout,
    required this.stderr,
    required this.stdin,
  });
66

67
  factory VerboseStdio.local() => VerboseStdio(
68 69 70 71
        stdout: io.stdout,
        stderr: io.stderr,
        stdin: io.stdin,
      );
72 73 74 75

  final io.Stdout stdout;
  final io.Stdout stderr;
  final io.Stdin stdin;
76 77 78

  @override
  void printError(String message) {
79
    super.printError(message);
80 81 82 83 84
    stderr.writeln(message);
  }

  @override
  void printStatus(String message) {
85
    super.printStatus(message);
86 87 88 89 90
    stdout.writeln(message);
  }

  @override
  void printTrace(String message) {
91
    super.printTrace(message);
92 93 94 95 96
    stdout.writeln(message);
  }

  @override
  void write(String message) {
97
    super.write(message);
98 99 100 101 102
    stdout.write(message);
  }

  @override
  String readLineSync() {
103
    return stdin.readLineSync()!;
104 105
  }
}