Skip to content

Commit 58708aa

Browse files
committed
Move all printing during run to flow through logging
Tests are no longer noisy on the console Provides a common entry point to provide alternate (JSON) output formats Centralized new-line handling Standardize on stdout for non-fatal output (use of stderr was not consistent)
1 parent a9da731 commit 58708aa

9 files changed

+111
-42
lines changed

bin/dartdoc.dart

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer/src/dart/sdk/sdk.dart';
1212
import 'package:analyzer/src/generated/sdk.dart';
1313
import 'package:args/args.dart';
1414
import 'package:dartdoc/dartdoc.dart';
15+
import 'package:dartdoc/src/logging.dart';
1516
import 'package:path/path.dart' as path;
1617
import 'package:stack_trace/stack_trace.dart';
1718

@@ -47,9 +48,9 @@ main(List<String> arguments) async {
4748

4849
final bool sdkDocs = args['sdk-docs'];
4950

50-
var showProgress = args['show-progress'] as bool;
51+
final showProgress = args['show-progress'] as bool;
5152

52-
var readme = args['sdk-readme'];
53+
var readme = args['sdk-readme'] as String;
5354
if (readme != null && !(new File(readme).existsSync())) {
5455
stderr.writeln(
5556
" fatal error: unable to locate the SDK description file at $readme.");
@@ -121,6 +122,42 @@ main(List<String> arguments) async {
121122
_printUsageAndExit(parser, exitCode: 1);
122123
}
123124

125+
final stopwatch = new Stopwatch()..start();
126+
127+
// Used to track if we're printing `...` to show progress.
128+
// Allows unified new-line tracking
129+
var writingProgress = false;
130+
131+
logEvents.listen((record) {
132+
if (record.level == LogLevels.progress) {
133+
if (showProgress &&
134+
writingProgress &&
135+
stopwatch.elapsed.inMilliseconds > 500) {
136+
stdout.write('.');
137+
stopwatch.reset();
138+
}
139+
return;
140+
}
141+
142+
if (writingProgress) {
143+
// print a new line after progress dots...
144+
print('');
145+
writingProgress = false;
146+
}
147+
var message = record.message.toString();
148+
assert(message == message.trimRight());
149+
assert(message.isNotEmpty);
150+
151+
if (message.endsWith('...')) {
152+
// Assume there may be more progress to print, so omit the trailing
153+
// newline
154+
writingProgress = true;
155+
stdout.write(message);
156+
} else {
157+
print(message);
158+
}
159+
});
160+
124161
PackageMeta packageMeta = sdkDocs
125162
? new PackageMeta.fromSdk(sdkDir,
126163
sdkReadmePath: readme, useCategories: args['use-categories'])
@@ -142,9 +179,8 @@ main(List<String> arguments) async {
142179
}
143180
}
144181

145-
print("Generating documentation for '${packageMeta}' into "
182+
logInfo("Generating documentation for '${packageMeta}' into "
146183
"${outputDir.absolute.path}${Platform.pathSeparator}");
147-
print('');
148184

149185
var generators = await initGenerators(url, args['rel-canonical-prefix'],
150186
headerFilePaths: headerFilePaths,
@@ -154,13 +190,8 @@ main(List<String> arguments) async {
154190
useCategories: args['use-categories'],
155191
prettyIndexJson: args['pretty-index-json']);
156192

157-
var progressCounter = 0;
158-
159193
void onProgress(Object file) {
160-
if (showProgress && progressCounter % 5 == 0) {
161-
stdout.write('.');
162-
}
163-
progressCounter += 1;
194+
logProgress(file);
164195
}
165196

166197
for (var generator in generators) {
@@ -211,9 +242,9 @@ main(List<String> arguments) async {
211242
includeExternals: includeExternals);
212243

213244
dartdoc.onCheckProgress.listen(onProgress);
214-
Chain.capture(() async {
245+
await Chain.capture(() async {
215246
DartDocResults results = await dartdoc.generateDocs();
216-
print('\nSuccess! Docs generated into ${results.outDir.absolute.path}');
247+
logInfo('Success! Docs generated into ${results.outDir.absolute.path}');
217248
}, onError: (e, Chain chain) {
218249
if (e is DartDocFailure) {
219250
stderr.writeln('\nGeneration failed: ${e}.');
@@ -237,7 +268,8 @@ ArgParser _createArgsParser() {
237268
defaultsTo: false);
238269
parser.addFlag('sdk-docs',
239270
help: 'Generate ONLY the docs for the Dart SDK.', negatable: false);
240-
parser.addFlag('show-warnings', help: 'Display warnings.', negatable: false);
271+
parser.addFlag('show-warnings',
272+
help: 'Display warnings.', negatable: false, defaultsTo: false);
241273
parser.addFlag('show-progress',
242274
help: 'Display progress indications to console stdout', negatable: false);
243275
parser.addOption('sdk-readme',

lib/dartdoc.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import 'src/config.dart';
3333
import 'src/generator.dart';
3434
import 'src/html/html_generator.dart';
3535
import 'src/io_utils.dart';
36+
import 'src/logging.dart';
3637
import 'src/model.dart';
3738
import 'src/model_utils.dart';
3839
import 'src/package_meta.dart';
@@ -192,15 +193,15 @@ class DartDoc {
192193
int warnings = package.packageWarningCounter.warningCount;
193194
int errors = package.packageWarningCounter.errorCount;
194195
if (warnings == 0 && errors == 0) {
195-
print("\nno issues found");
196+
logInfo("no issues found");
196197
} else {
197-
print("\nfound ${warnings} ${pluralize('warning', warnings)} "
198+
logWarning("found ${warnings} ${pluralize('warning', warnings)} "
198199
"and ${errors} ${pluralize('error', errors)}");
199200
}
200201

201202
double seconds = _stopwatch.elapsedMilliseconds / 1000.0;
202-
print(
203-
"\ndocumented ${package.libraries.length} librar${package.libraries.length == 1 ? 'y' : 'ies'} "
203+
logInfo(
204+
"Documented ${package.libraries.length} librar${package.libraries.length == 1 ? 'y' : 'ies'} "
204205
"in ${seconds.toStringAsFixed(1)} seconds");
205206

206207
if (package.libraries.isEmpty) {
@@ -420,7 +421,7 @@ class DartDoc {
420421

421422
final Set<String> visited = new Set();
422423
final String start = 'index.html';
423-
stdout.write('\nvalidating docs...');
424+
logInfo('Validating docs...');
424425
_doCheck(package, origin, visited, start);
425426
_doOrphanCheck(package, origin, visited);
426427
_doSearchIndexCheck(package, origin, visited);
@@ -486,7 +487,7 @@ class DartDoc {
486487
name = name.substring(Directory.current.path.length);
487488
if (name.startsWith(Platform.pathSeparator)) name = name.substring(1);
488489
}
489-
print('parsing ${name}...');
490+
logInfo('parsing ${name}...');
490491
JavaFile javaFile = new JavaFile(filePath).getAbsoluteFile();
491492
Source source = new FileBasedSource(javaFile);
492493

@@ -578,12 +579,12 @@ class DartDoc {
578579
..sort();
579580

580581
double seconds = _stopwatch.elapsedMilliseconds / 1000.0;
581-
print("parsed ${libraries.length} ${pluralize('file', libraries.length)} "
582+
logInfo("parsed ${libraries.length} ${pluralize('file', libraries.length)} "
582583
"in ${seconds.toStringAsFixed(1)} seconds");
583584
_stopwatch.reset();
584585

585586
if (errors.isNotEmpty) {
586-
errors.forEach(print);
587+
errors.forEach(logWarning);
587588
int len = errors.length;
588589
throw new DartDocFailure(
589590
"encountered ${len} analysis error${len == 1 ? '' : 's'}");

lib/src/html/html_generator_instance.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
import 'dart:async' show Future, StreamController;
66
import 'dart:convert' show JsonEncoder;
7-
import 'dart:io' show Directory, File, stdout;
7+
import 'dart:io' show Directory, File;
88
import 'dart:typed_data' show Uint8List;
99

1010
import 'package:collection/collection.dart' show compareNatural;
1111
import 'package:path/path.dart' as path;
1212

13+
import '../logging.dart';
1314
import '../model.dart';
1415
import '../warnings.dart';
1516
import 'html_generator.dart' show HtmlGeneratorOptions;
@@ -183,14 +184,13 @@ class HtmlGeneratorInstance implements HtmlOptions {
183184

184185
void generatePackage() {
185186
TemplateData data = new PackageTemplateData(this, package, useCategories);
186-
stdout.write('\ndocumenting ${package.name}');
187+
logInfo('documenting ${package.name}');
187188

188189
_build('index.html', _templates.indexTemplate, data);
189190
}
190191

191192
void generateLibrary(Package package, Library lib) {
192-
stdout
193-
.write('\ngenerating docs for library ${lib.name} from ${lib.path}...');
193+
logInfo('Generating docs for library ${lib.name} from ${lib.path}...');
194194
if (!lib.isAnonymous && !lib.hasDocumentation) {
195195
package.warnOnElement(lib, PackageWarning.noLibraryLevelDocs);
196196
}

lib/src/logging.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
enum LogLevels { warning, info, progress }
8+
9+
final _controller = new StreamController<LogMessage>(sync: true);
10+
11+
Stream<LogMessage> get logEvents => _controller.stream;
12+
13+
class LogMessage {
14+
final LogLevels level;
15+
final Object message;
16+
17+
LogMessage(this.level, this.message);
18+
}
19+
20+
void logWarning(Object message) {
21+
_controller.add(new LogMessage(LogLevels.warning, message));
22+
}
23+
24+
void logInfo(Object message) {
25+
_controller.add(new LogMessage(LogLevels.info, message));
26+
}
27+
28+
void logProgress(Object message) {
29+
_controller.add(new LogMessage(LogLevels.progress, message));
30+
}

lib/src/model.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'package:tuple/tuple.dart';
3030
import 'config.dart';
3131
import 'element_type.dart';
3232
import 'line_number_cache.dart';
33+
import 'logging.dart';
3334
import 'markdown_processor.dart' show Documentation;
3435
import 'model_utils.dart';
3536
import 'package_meta.dart' show PackageMeta, FileContents;
@@ -3116,9 +3117,9 @@ abstract class ModelElement extends Nameable
31163117
// TODO(jcollins-g): move this to Package.warn system
31173118
var filePath =
31183119
this.element.source.fullName.substring(dirPath.length + 1);
3119-
final msg =
3120-
'\nwarning: ${filePath}: @example file not found, ${fragmentFile.path}';
3121-
stderr.write(msg);
3120+
3121+
logWarning(
3122+
'warning: ${filePath}: @example file not found, ${fragmentFile.path}');
31223123
}
31233124
return replacement;
31243125
});

lib/src/package_meta.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'dart:io';
99
import 'package:path/path.dart' as path;
1010
import 'package:yaml/yaml.dart';
1111

12+
import 'logging.dart';
13+
1214
abstract class PackageMeta {
1315
final Directory dir;
1416
final bool useCategories;
@@ -103,8 +105,9 @@ class _FilePackageMeta extends PackageMeta {
103105
ProcessResult result =
104106
Process.runSync(pubPath, ['get'], workingDirectory: dir.path);
105107

106-
if (result.stdout.isNotEmpty) {
107-
print(result.stdout.trim());
108+
var trimmedStdout = (result.stdout as String).trim();
109+
if (trimmedStdout.isNotEmpty) {
110+
logInfo(trimmedStdout);
108111
}
109112

110113
if (result.exitCode != 0) {

lib/src/warnings.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import 'dart:io';
2-
31
import 'package:analyzer/dart/element/element.dart';
42
import 'package:tuple/tuple.dart';
53

64
import 'config.dart';
5+
import 'logging.dart';
76

87
class PackageWarningHelpText {
98
final String warningName;
@@ -165,7 +164,7 @@ class PackageWarningCounter {
165164
final _warningCounts = new Map<PackageWarning, int>();
166165
final PackageWarningOptions options;
167166

168-
final _buffer = new StringBuffer();
167+
final _items = <String>[];
169168

170169
PackageWarningCounter(this.options);
171170

@@ -175,8 +174,10 @@ class PackageWarningCounter {
175174
/// warnings here might be duplicated across multiple Package constructions.
176175
void maybeFlush() {
177176
if (options.autoFlush) {
178-
stderr.write(_buffer.toString());
179-
_buffer.clear();
177+
for (var item in _items) {
178+
logWarning(item);
179+
}
180+
_items.clear();
180181
}
181182
}
182183

@@ -192,18 +193,20 @@ class PackageWarningCounter {
192193
toWrite = "warning: ${fullMessage}";
193194
}
194195
if (toWrite != null) {
195-
_buffer.write("\n ${toWrite}");
196+
var entry = " ${toWrite}";
196197
if (_warningCounts[kind] == 1 &&
197198
config.verboseWarnings &&
198199
packageWarningText[kind].longHelp.isNotEmpty) {
199200
// First time we've seen this warning. Give a little extra info.
200201
final String separator = '\n ';
201202
final String nameSub = r'@@name@@';
202203
String verboseOut =
203-
'$separator${packageWarningText[kind].longHelp.join(separator)}';
204-
verboseOut = verboseOut.replaceAll(nameSub, name);
205-
_buffer.write(verboseOut);
204+
'$separator${packageWarningText[kind].longHelp.join(separator)}'
205+
.replaceAll(nameSub, name);
206+
entry = '$entry$verboseOut';
206207
}
208+
assert(entry == entry.trimRight());
209+
_items.add(entry);
207210
}
208211
maybeFlush();
209212
}

pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dartdoc
22
# Also update the `version` field in lib/dartdoc.dart.
3-
version: 0.14.1
3+
version: 0.14.2-dev
44
author: Dart Team <[email protected]>
55
description: A documentation generator for Dart.
66
homepage: https://github.com/dart-lang/dartdoc
@@ -14,7 +14,6 @@ dependencies:
1414
# We don't use http_parser directly; this dep exists to ensure that we get at
1515
# least version 3.0.3 to work around an issue with 3.0.2.
1616
http_parser: '>=3.0.3 <4.0.0'
17-
logging: '>=0.9.0 <0.12.0'
1817
markdown: ^0.11.2
1918
mustache4dart: ^1.1.0
2019
package_config: '>=0.1.5 <2.0.0'

test/compare_output_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void main() {
145145
final sep = '.'; // We don't care what the path separator character is
146146
final firstUnfoundExample = new RegExp('warning: lib${sep}example.dart: '
147147
'@example file not found.*test_package${sep}dog${sep}food.md');
148-
if (!result.stderr.contains(firstUnfoundExample)) {
148+
if (!result.stdout.contains(firstUnfoundExample)) {
149149
fail('Should warn about unfound @example files: \n'
150150
'stdout:\n${result.stdout}\nstderr:\n${result.stderr}');
151151
}

0 commit comments

Comments
 (0)