logs.dart 2.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import '../application_package.dart';
8
import '../base/common.dart';
9
import '../base/io.dart';
10
import '../device.dart';
11
import '../globals.dart' as globals;
12
import '../runner/flutter_command.dart';
13

14 15
class LogsCommand extends FlutterCommand {
  LogsCommand() {
16
    argParser.addFlag('clear',
Devon Carew's avatar
Devon Carew committed
17 18
      negatable: false,
      abbr: 'c',
19
      help: 'Clear log history before reading from logs.',
Devon Carew's avatar
Devon Carew committed
20
    );
21
    usesDeviceTimeoutOption();
22 23
  }

24 25 26 27 28 29
  @override
  final String name = 'logs';

  @override
  final String description = 'Show log output for running Flutter apps.';

30 31 32
  @override
  final String category = FlutterCommandCategory.tools;

33 34 35
  @override
  bool get refreshWirelessDevices => true;

36 37 38
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};

39
  Device? device;
40

41
  @override
42
  Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) async {
43
    device = await findTargetDevice(includeDevicesUnsupportedByProject: true);
44
    if (device == null) {
45
      throwToolExit(null);
46 47
    }
    return super.verifyThenRunCommand(commandPath);
48
  }
49

50
  @override
51
  Future<FlutterCommandResult> runCommand() async {
52
    final Device cachedDevice = device!;
53
    if (boolArg('clear')) {
54
      cachedDevice.clearLogs();
55
    }
56

57 58 59 60 61
    final ApplicationPackage? app = await applicationPackages?.getPackageForPlatform(
      await cachedDevice.targetPlatform,
    );

    final DeviceLogReader logReader = await cachedDevice.getLogReader(app: app);
62

63
    globals.printStatus('Showing $logReader logs:');
64

65
    final Completer<int> exitCompleter = Completer<int>();
Devon Carew's avatar
Devon Carew committed
66

67
    // Start reading.
68
    final StreamSubscription<String> subscription = logReader.logLines.listen(
69
      (String message) => globals.printStatus(message, wrap: false),
Devon Carew's avatar
Devon Carew committed
70 71 72 73 74
      onDone: () {
        exitCompleter.complete(0);
      },
      onError: (dynamic error) {
        exitCompleter.complete(error is int ? error : 1);
75
      },
Devon Carew's avatar
Devon Carew committed
76
    );
Devon Carew's avatar
Devon Carew committed
77

Devon Carew's avatar
Devon Carew committed
78
    // When terminating, close down the log reader.
79
    ProcessSignal.sigint.watch().listen((ProcessSignal signal) {
Devon Carew's avatar
Devon Carew committed
80
      subscription.cancel();
81
      globals.printStatus('');
Devon Carew's avatar
Devon Carew committed
82 83
      exitCompleter.complete(0);
    });
84
    ProcessSignal.sigterm.watch().listen((ProcessSignal signal) {
85 86 87
      subscription.cancel();
      exitCompleter.complete(0);
    });
88

89
    // Wait for the log reader to be finished.
90
    final int result = await exitCompleter.future;
91
    await subscription.cancel();
92
    if (result != 0) {
93
      throwToolExit('Error listening to $logReader logs.');
94
    }
95

96
    return FlutterCommandResult.success();
97 98
  }
}