taskLogger.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const os = require("os");
  4. const logger_1 = require("./logger");
  5. let logger = new logger_1.Logger('testLogger');
  6. class TaskLogger {
  7. /**
  8. * Log output such that metadata are appended.
  9. * Calling log(data) will not flush to console until you call flush()
  10. *
  11. * @constructor
  12. * @param {object} task Task that is being reported.
  13. * @param {number} pid PID of process running the task.
  14. */
  15. constructor(task, pid) {
  16. this.task = task;
  17. this.pid = pid;
  18. this.buffer = '';
  19. this.insertTag = true;
  20. this.logHeader_();
  21. }
  22. /**
  23. * Log the header for the current task including information such as
  24. * PID, browser name/version, task Id, specs being run.
  25. *
  26. * @private
  27. */
  28. logHeader_() {
  29. let output = 'PID: ' + this.pid + os.EOL;
  30. if (this.task.specs.length === 1) {
  31. output += 'Specs: ' + this.task.specs.toString() + os.EOL + os.EOL;
  32. }
  33. this.log(output);
  34. }
  35. /**
  36. * Prints the contents of the buffer without clearing it.
  37. */
  38. peek() {
  39. if (this.buffer) {
  40. // Flush buffer if nonempty
  41. logger.info(os.EOL + '------------------------------------' + os.EOL);
  42. logger.info(this.buffer);
  43. logger.info(os.EOL);
  44. }
  45. }
  46. /**
  47. * Flushes the buffer to stdout.
  48. */
  49. flush() {
  50. if (this.buffer) {
  51. this.peek();
  52. this.buffer = '';
  53. }
  54. }
  55. /**
  56. * Log the data in the argument such that metadata are appended.
  57. * The data will be saved to a buffer until flush() is called.
  58. *
  59. * @param {string} data
  60. */
  61. log(data) {
  62. let tag = '[';
  63. let capabilities = this.task.capabilities;
  64. tag += (capabilities.logName) ? capabilities.logName :
  65. (capabilities.browserName) ? capabilities.browserName : '';
  66. tag += (capabilities.version) ? (' ' + capabilities.version) : '';
  67. tag += (capabilities.platform) ? (' ' + capabilities.platform) : '';
  68. tag += (capabilities.logName && capabilities.count < 2) ? '' : ' #' + this.task.taskId;
  69. tag += '] ';
  70. data = data.toString();
  71. for (let i = 0; i < data.length; i++) {
  72. if (this.insertTag) {
  73. this.insertTag = false;
  74. // This ensures that the '\x1B[0m' appears before the tag, so that
  75. // data remains correct when color is not processed.
  76. // See https://github.com/angular/protractor/pull/1216
  77. if (data[i] === '\x1B' && data.substring(i, i + 4) === '\x1B[0m') {
  78. this.buffer += ('\x1B[0m' + tag);
  79. i += 3;
  80. continue;
  81. }
  82. this.buffer += tag;
  83. }
  84. if (data[i] === '\n') {
  85. this.insertTag = true;
  86. }
  87. this.buffer += data[i];
  88. }
  89. }
  90. }
  91. exports.TaskLogger = TaskLogger;
  92. //# sourceMappingURL=taskLogger.js.map