debug.dart 2.51 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium 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 6
import 'dart:async';

7
import 'assertions.dart';
8
import 'platform.dart';
9
import 'print.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22 23

/// Returns true if none of the foundation library debug variables have been
/// changed.
///
/// This function is used by the test framework to ensure that debug variables
/// haven't been inadvertently changed.
///
/// The `debugPrintOverride` argument can be specified to indicate the expected
/// value of the [debugPrint] variable. This is useful for test frameworks that
/// override [debugPrint] themselves and want to check that their own custom
/// value wasn't overridden by a test.
///
/// See [https://docs.flutter.io/flutter/foundation/foundation-library.html] for
/// a complete list.
24
bool debugAssertAllFoundationVarsUnset(String reason, { DebugPrintCallback debugPrintOverride = debugPrintThrottled }) {
25
  assert(() {
26 27
    if (debugPrint != debugPrintOverride ||
        debugDefaultTargetPlatformOverride != null)
28
      throw FlutterError(reason);
29
    return true;
30
  }());
31 32
  return true;
}
33

34 35 36 37 38 39 40 41 42 43 44
/// Boolean value indicating whether [debugInstrumentAction] will instrument
/// actions in debug builds.
bool debugInstrumentationEnabled = false;

/// Runs the specified [action], timing how long the action takes in debug
/// builds when [debugInstrumentationEnabled] is true.
///
/// The instrumentation will be printed to the logs using [debugPrint]. In
/// non-debug builds, or when [debugInstrumentationEnabled] is false, this will
/// run [action] without any instrumentation.
///
45 46 47 48 49 50 51 52 53 54 55
/// Returns the result of running [action].
///
/// See also:
///
///   * [Timeline], which is used to record synchronous tracing events for
///     visualization in Chrome's tracing format. This method does not
///     implicitly add any timeline events.
Future<T> debugInstrumentAction<T>(String description, Future<T> action()) {
  bool instrument = false;
  assert(() { instrument = debugInstrumentationEnabled; return true; }());
  if (instrument) {
56
    final Stopwatch stopwatch = Stopwatch()..start();
57
    return action().whenComplete(() {
58 59
      stopwatch.stop();
      debugPrint('Action "$description" took ${stopwatch.elapsed}');
60 61 62
    });
  } else {
    return action();
63 64 65
  }
}

66 67
/// Arguments to whitelist [Timeline] events in order to be shown in the
/// developer centric version of the Observatory Timeline.
68
const Map<String, String> timelineWhitelistArguments = <String, String>{
69 70
  'mode': 'basic'
};