1 | ;
|
2 | /**
|
3 | * @license
|
4 | * Copyright Google LLC All Rights Reserved.
|
5 | *
|
6 | * Use of this source code is governed by an MIT-style license that can be
|
7 | * found in the LICENSE file at https://angular.io/license
|
8 | */
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.createConsoleLogger = void 0;
|
11 | const rxjs_1 = require("rxjs");
|
12 | const src_1 = require("../src");
|
13 | /**
|
14 | * A Logger that sends information to STDOUT and STDERR.
|
15 | */
|
16 | function createConsoleLogger(verbose = false, stdout = process.stdout, stderr = process.stderr, colors) {
|
17 | const logger = new src_1.logging.IndentLogger('cling');
|
18 | logger.pipe((0, rxjs_1.filter)((entry) => entry.level !== 'debug' || verbose)).subscribe((entry) => {
|
19 | const color = colors && colors[entry.level];
|
20 | let output = stdout;
|
21 | switch (entry.level) {
|
22 | case 'warn':
|
23 | case 'fatal':
|
24 | case 'error':
|
25 | output = stderr;
|
26 | break;
|
27 | }
|
28 | // If we do console.log(message) or process.stdout.write(message + '\n'), the process might
|
29 | // stop before the whole message is written and the stream is flushed. This happens when
|
30 | // streams are asynchronous.
|
31 | //
|
32 | // NodeJS IO streams are different depending on platform and usage. In POSIX environment,
|
33 | // for example, they're asynchronous when writing to a pipe, but synchronous when writing
|
34 | // to a TTY. In windows, it's the other way around. You can verify which is which with
|
35 | // stream.isTTY and platform, but this is not good enough.
|
36 | // In the async case, one should wait for the callback before sending more data or
|
37 | // continuing the process. In our case it would be rather hard to do (but not impossible).
|
38 | //
|
39 | // Instead we take the easy way out and simply chunk the message and call the write
|
40 | // function while the buffer drain itself asynchronously. With a smaller chunk size than
|
41 | // the buffer, we are mostly certain that it works. In this case, the chunk has been picked
|
42 | // as half a page size (4096/2 = 2048), minus some bytes for the color formatting.
|
43 | // On POSIX it seems the buffer is 2 pages (8192), but just to be sure (could be different
|
44 | // by platform).
|
45 | //
|
46 | // For more details, see https://nodejs.org/api/process.html#process_a_note_on_process_i_o
|
47 | const chunkSize = 2000; // Small chunk.
|
48 | let message = entry.message;
|
49 | while (message) {
|
50 | const chunk = message.slice(0, chunkSize);
|
51 | message = message.slice(chunkSize);
|
52 | output.write(color ? color(chunk) : chunk);
|
53 | }
|
54 | output.write('\n');
|
55 | });
|
56 | return logger;
|
57 | }
|
58 | exports.createConsoleLogger = createConsoleLogger;
|