|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async' show runZoned; |
| 6 | +import 'dart:io' as io show |
| 7 | + IOSink, |
| 8 | + stderr, |
| 9 | + stdout; |
| 10 | + |
| 11 | +import 'package:logging/logging.dart' as log; |
| 12 | +import 'package:meta/meta.dart'; |
| 13 | + |
| 14 | +// This is where a flutter_tool style progress spinner, color output, |
| 15 | +// ascii art, terminal control for clearing lines or the whole screen, etc. |
| 16 | +// can go. We can just add more methods to Logger using the flutter_tool's |
| 17 | +// Logger as a guide: |
| 18 | +// |
| 19 | +// https://github.com/flutter/flutter/blob/c530276f7806c77da2541c518a0e103c9bb44f10/packages/flutter_tools/lib/src/base/logger.dart#L422 |
| 20 | + |
| 21 | +/// A simplified wrapper around the [Logger] from package:logging. |
| 22 | +/// |
| 23 | +/// The default log level is [Logger.status]. A --quiet flag might change it to |
| 24 | +/// [Logger.warning] or [Logger.error]. A --verbose flag might change it to |
| 25 | +/// [Logger.info]. |
| 26 | +/// |
| 27 | +/// Log messages at [Logger.warning] and higher will be written to stderr, and |
| 28 | +/// to stdout otherwise. [Logger.test] records all log messages to a buffer, |
| 29 | +/// which can be inspected by unit tetss. |
| 30 | +class Logger { |
| 31 | + /// Constructs a logger for use in the tool. |
| 32 | + Logger() : _logger = log.Logger.detached('et') { |
| 33 | + _logger.level = statusLevel; |
| 34 | + _logger.onRecord.listen(_handler); |
| 35 | + _setupIoSink(io.stderr); |
| 36 | + _setupIoSink(io.stdout); |
| 37 | + } |
| 38 | + |
| 39 | + /// A logger for tests. |
| 40 | + @visibleForTesting |
| 41 | + Logger.test() : _logger = log.Logger.detached('et') { |
| 42 | + _logger.level = statusLevel; |
| 43 | + _logger.onRecord.listen((log.LogRecord r) => _testLogs.add(r)); |
| 44 | + } |
| 45 | + |
| 46 | + /// The logging level for error messages. These go to stderr. |
| 47 | + static const log.Level errorLevel = log.Level('ERROR', 100); |
| 48 | + |
| 49 | + /// The logging level for warning messages. These go to stderr. |
| 50 | + static const log.Level warningLevel = log.Level('WARNING', 75); |
| 51 | + |
| 52 | + /// The logging level for normal status messages. These go to stdout. |
| 53 | + static const log.Level statusLevel = log.Level('STATUS', 25); |
| 54 | + |
| 55 | + /// The logging level for verbose informational messages. These go to stdout. |
| 56 | + static const log.Level infoLevel = log.Level('INFO', 10); |
| 57 | + |
| 58 | + static void _handler(log.LogRecord r) { |
| 59 | + final io.IOSink sink = r.level >= warningLevel ? io.stderr : io.stdout; |
| 60 | + final String prefix = r.level >= warningLevel |
| 61 | + ? '[${r.time}] ${r.level}: ' |
| 62 | + : ''; |
| 63 | + _ioSinkWrite(sink, '$prefix${r.message}'); |
| 64 | + } |
| 65 | + |
| 66 | + // Status of the global io.stderr and io.stdout is shared across all |
| 67 | + // Logger instances. |
| 68 | + static bool _stdioDone = false; |
| 69 | + |
| 70 | + // stdout and stderr might already be closed, and when not already closed, |
| 71 | + // writing can still fail by throwing either a sync or async exception. |
| 72 | + // This function handles all three cases. |
| 73 | + static void _ioSinkWrite(io.IOSink sink, String message) { |
| 74 | + if (_stdioDone) { |
| 75 | + return; |
| 76 | + } |
| 77 | + runZoned<void>(() { |
| 78 | + try { |
| 79 | + sink.writeln(message); |
| 80 | + } catch (_) { // ignore: avoid_catches_without_on_clauses |
| 81 | + _stdioDone = true; |
| 82 | + } |
| 83 | + }, onError: (Object e, StackTrace s) { |
| 84 | + _stdioDone = true; |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + static void _setupIoSink(io.IOSink sink) { |
| 89 | + sink.done.then( |
| 90 | + (void _) { _stdioDone = true; }, |
| 91 | + onError: (Object err, StackTrace st) { _stdioDone = true; }, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + final log.Logger _logger; |
| 96 | + final List<log.LogRecord> _testLogs = <log.LogRecord>[]; |
| 97 | + |
| 98 | + /// Get the current logging level. |
| 99 | + log.Level get level => _logger.level; |
| 100 | + |
| 101 | + /// Set the current logging level. |
| 102 | + set level(log.Level l) { |
| 103 | + _logger.level = l; |
| 104 | + } |
| 105 | + |
| 106 | + /// Record a log message at level [Logger.error]. |
| 107 | + void error(Object? message, {int indent = 0}) { |
| 108 | + _emitLog(errorLevel, message, indent); |
| 109 | + } |
| 110 | + |
| 111 | + /// Record a log message at level [Logger.warning]. |
| 112 | + void warning(Object? message, {int indent = 0}) { |
| 113 | + _emitLog(warningLevel, message, indent); |
| 114 | + } |
| 115 | + |
| 116 | + /// Record a log message at level [Logger.warning]. |
| 117 | + void status(Object? message, {int indent = 0}) { |
| 118 | + _emitLog(statusLevel, message, indent); |
| 119 | + } |
| 120 | + |
| 121 | + /// Record a log message at level [Logger.info]. |
| 122 | + void info(Object? message, {int indent = 0}) { |
| 123 | + _emitLog(infoLevel, message, indent); |
| 124 | + } |
| 125 | + |
| 126 | + void _emitLog(log.Level level, Object? message, int indent) { |
| 127 | + final String m = '${' ' * indent}$message'; |
| 128 | + _logger.log(level, m); |
| 129 | + } |
| 130 | + |
| 131 | + /// In a [Logger] constructed by [Logger.test], this list will contain all of |
| 132 | + /// the [LogRecord]s emitted by the test. |
| 133 | + @visibleForTesting |
| 134 | + List<log.LogRecord> get testLogs => _testLogs; |
| 135 | +} |
0 commit comments