stdio.dart 3.51 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
    required this.stdout,
    required this.stderr,
    required this.stdin,
73
    this.filter,
74
  });
75

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

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

86 87 88
  /// If provided, all messages will be passed through this function before being logged.
  final String Function(String)? filter;

89 90
  @override
  void printError(String message) {
91 92 93
    if (filter != null) {
      message = filter!(message);
    }
94
    super.printError(message);
95 96 97
    stderr.writeln(message);
  }

98 99 100 101 102 103 104 105 106
  @override
  void printWarning(String message) {
    if (filter != null) {
      message = filter!(message);
    }
    super.printWarning(message);
    stderr.writeln(message);
  }

107 108
  @override
  void printStatus(String message) {
109 110 111
    if (filter != null) {
      message = filter!(message);
    }
112
    super.printStatus(message);
113 114 115 116 117
    stdout.writeln(message);
  }

  @override
  void printTrace(String message) {
118 119 120
    if (filter != null) {
      message = filter!(message);
    }
121
    super.printTrace(message);
122 123 124 125 126
    stdout.writeln(message);
  }

  @override
  void write(String message) {
127 128 129
    if (filter != null) {
      message = filter!(message);
    }
130
    super.write(message);
131 132 133 134 135
    stdout.write(message);
  }

  @override
  String readLineSync() {
136
    return stdin.readLineSync()!;
137 138
  }
}