|
4 | 4 |
|
5 | 5 | library dartdoc.bin; |
6 | 6 |
|
| 7 | +import 'dart:async'; |
| 8 | +import 'dart:convert'; |
7 | 9 | import 'dart:io'; |
8 | 10 | import 'dart:isolate' show Isolate; |
9 | 11 |
|
@@ -122,48 +124,69 @@ main(List<String> arguments) async { |
122 | 124 | _printUsageAndExit(parser, exitCode: 1); |
123 | 125 | } |
124 | 126 |
|
| 127 | + final logJson = args['json'] as bool; |
| 128 | + |
125 | 129 | // By default, get all log output at `progressLevel` or greater. |
126 | 130 | // This allows us to capture progress events and print `...`. |
127 | 131 | logging.Logger.root.level = progressLevel; |
128 | | - final stopwatch = new Stopwatch()..start(); |
129 | | - |
130 | | - // Used to track if we're printing `...` to show progress. |
131 | | - // Allows unified new-line tracking |
132 | | - var writingProgress = false; |
133 | | - |
134 | | - logging.Logger.root.onRecord.listen((record) { |
135 | | - if (record.level == progressLevel) { |
136 | | - if (showProgress && stopwatch.elapsed.inMilliseconds > 250) { |
137 | | - writingProgress = true; |
138 | | - stdout.write('.'); |
139 | | - stopwatch.reset(); |
| 132 | + |
| 133 | + if (logJson) { |
| 134 | + logging.Logger.root.onRecord.listen((record) { |
| 135 | + if (record.level == progressLevel) { |
| 136 | + return; |
140 | 137 | } |
141 | | - return; |
142 | | - } |
143 | 138 |
|
144 | | - stopwatch.reset(); |
145 | | - if (writingProgress) { |
146 | | - // print a new line after progress dots... |
147 | | - print(''); |
148 | | - writingProgress = false; |
149 | | - } |
150 | | - var message = record.message; |
151 | | - assert(message == message.trimRight()); |
152 | | - assert(message.isNotEmpty); |
153 | | - |
154 | | - if (record.level < logging.Level.WARNING) { |
155 | | - if (message.endsWith('...')) { |
156 | | - // Assume there may be more progress to print, so omit the trailing |
157 | | - // newline |
158 | | - writingProgress = true; |
159 | | - stdout.write(message); |
| 139 | + var output = <String, dynamic>{'level': record.level.name}; |
| 140 | + |
| 141 | + if (record.object is Jsonable) { |
| 142 | + output['data'] = record.object; |
160 | 143 | } else { |
161 | | - print(message); |
| 144 | + output['message'] = record.message; |
162 | 145 | } |
163 | | - } else { |
164 | | - stderr.writeln(message); |
165 | | - } |
166 | | - }); |
| 146 | + |
| 147 | + print(JSON.encode(output)); |
| 148 | + }); |
| 149 | + } else { |
| 150 | + final stopwatch = new Stopwatch()..start(); |
| 151 | + |
| 152 | + // Used to track if we're printing `...` to show progress. |
| 153 | + // Allows unified new-line tracking |
| 154 | + var writingProgress = false; |
| 155 | + |
| 156 | + logging.Logger.root.onRecord.listen((record) { |
| 157 | + if (record.level == progressLevel) { |
| 158 | + if (showProgress && stopwatch.elapsed.inMilliseconds > 250) { |
| 159 | + writingProgress = true; |
| 160 | + stdout.write('.'); |
| 161 | + stopwatch.reset(); |
| 162 | + } |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + stopwatch.reset(); |
| 167 | + if (writingProgress) { |
| 168 | + // print a new line after progress dots... |
| 169 | + print(''); |
| 170 | + writingProgress = false; |
| 171 | + } |
| 172 | + var message = record.message; |
| 173 | + assert(message == message.trimRight()); |
| 174 | + assert(message.isNotEmpty); |
| 175 | + |
| 176 | + if (record.level < logging.Level.WARNING) { |
| 177 | + if (message.endsWith('...')) { |
| 178 | + // Assume there may be more progress to print, so omit the trailing |
| 179 | + // newline |
| 180 | + writingProgress = true; |
| 181 | + stdout.write(message); |
| 182 | + } else { |
| 183 | + print(message); |
| 184 | + } |
| 185 | + } else { |
| 186 | + stderr.writeln(message); |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
167 | 190 |
|
168 | 191 | PackageMeta packageMeta = sdkDocs |
169 | 192 | ? new PackageMeta.fromSdk(sdkDir, |
@@ -246,8 +269,13 @@ main(List<String> arguments) async { |
246 | 269 |
|
247 | 270 | dartdoc.onCheckProgress.listen(logProgress); |
248 | 271 | await Chain.capture(() async { |
249 | | - DartDocResults results = await dartdoc.generateDocs(); |
250 | | - logInfo('Success! Docs generated into ${results.outDir.absolute.path}'); |
| 272 | + await runZoned(() async { |
| 273 | + DartDocResults results = await dartdoc.generateDocs(); |
| 274 | + logInfo('Success! Docs generated into ${results.outDir.absolute.path}'); |
| 275 | + }, |
| 276 | + zoneSpecification: new ZoneSpecification( |
| 277 | + print: (Zone self, ZoneDelegate parent, Zone zone, String line) => |
| 278 | + logPrint(line) )); |
251 | 279 | }, onError: (e, Chain chain) { |
252 | 280 | if (e is DartDocFailure) { |
253 | 281 | stderr.writeln('\nGeneration failed: ${e}.'); |
@@ -354,6 +382,10 @@ ArgParser _createArgsParser() { |
354 | 382 | defaultsTo: false, |
355 | 383 | hide: true, |
356 | 384 | ); |
| 385 | + parser.addFlag('json', |
| 386 | + help: 'Prints out progress JSON maps. One entry per line.', |
| 387 | + defaultsTo: false, |
| 388 | + negatable: true); |
357 | 389 |
|
358 | 390 | return parser; |
359 | 391 | } |
|
0 commit comments