cli-logger.js 3.0 KB

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