stdio.dart 2.93 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

import 'package:meta/meta.dart';

9 10 11 12 13 14 15 16
/// An interface for presenting text output to the user.
///
/// Although this could have been simplified by calling `print()`
/// from the tool, this abstraction allows unit tests to verify output
/// and allows a GUI frontend to provide an alternative implementation.
///
/// User input probably should be part of this class–however it is currently
/// part of context.dart.
17
abstract class Stdio {
18 19
  final List<String> logs = <String>[];

20 21 22 23 24
  /// 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.
25 26 27 28
  @mustCallSuper
  void printError(String message) {
    logs.add('[error] $message');
  }
29

30 31 32 33 34 35 36 37 38
  /// 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');
  }

39
  /// Ordinary STDOUT messages.
40 41 42
  ///
  /// Displays normal output on stdout. This should be used for things like
  /// progress messages, success messages, or just normal command output.
43 44 45 46
  @mustCallSuper
  void printStatus(String message) {
    logs.add('[status] $message');
  }
47 48

  /// Debug messages that are only printed in verbose mode.
49 50 51
  ///
  /// Use this for verbose tracing output. Users can turn this output on in order
  /// to help diagnose issues.
52 53 54 55
  @mustCallSuper
  void printTrace(String message) {
    logs.add('[trace] $message');
  }
56

57
  /// Write the `message` string to STDOUT without a trailing newline.
58 59 60 61
  @mustCallSuper
  void write(String message) {
    logs.add('[write] $message');
  }
62 63 64 65 66 67 68 69

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

/// A logger that will print out trace messages.
class VerboseStdio extends Stdio {
  VerboseStdio({
70 71 72 73
    required this.stdout,
    required this.stderr,
    required this.stdin,
  });
74

75
  factory VerboseStdio.local() => VerboseStdio(
76 77 78 79
        stdout: io.stdout,
        stderr: io.stderr,
        stdin: io.stdin,
      );
80 81 82 83

  final io.Stdout stdout;
  final io.Stdout stderr;
  final io.Stdin stdin;
84 85 86

  @override
  void printError(String message) {
87
    super.printError(message);
88 89 90 91 92
    stderr.writeln(message);
  }

  @override
  void printStatus(String message) {
93
    super.printStatus(message);
94 95 96 97 98
    stdout.writeln(message);
  }

  @override
  void printTrace(String message) {
99
    super.printTrace(message);
100 101 102 103 104
    stdout.writeln(message);
  }

  @override
  void write(String message) {
105
    super.write(message);
106 107 108 109 110
    stdout.write(message);
  }

  @override
  String readLineSync() {
111
    return stdin.readLineSync()!;
112 113
  }
}