debugger.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Debugger = void 0;
  4. const system_external_js_1 = require("./system_external.js");
  5. class Debugger {
  6. static getInstance() {
  7. Debugger.instance = Debugger.instance || new Debugger();
  8. return Debugger.instance;
  9. }
  10. init(opt_file) {
  11. if (opt_file) {
  12. this.startDebugFile_(opt_file);
  13. }
  14. this.isActive_ = true;
  15. return this.fileHandle;
  16. }
  17. output(...args) {
  18. if (this.isActive_) {
  19. this.output_(args);
  20. }
  21. }
  22. generateOutput(func) {
  23. if (this.isActive_) {
  24. this.output_(func.apply(func, []));
  25. }
  26. }
  27. exit(callback = () => { }) {
  28. this.fileHandle.then(() => {
  29. if (this.isActive_ && this.stream_) {
  30. this.stream_.end('', '', callback);
  31. }
  32. });
  33. }
  34. constructor() {
  35. this.isActive_ = false;
  36. this.outputFunction_ = console.info;
  37. this.fileHandle = Promise.resolve();
  38. this.stream_ = null;
  39. }
  40. startDebugFile_(filename) {
  41. this.fileHandle = system_external_js_1.SystemExternal.fs.promises.open(filename, 'w');
  42. this.fileHandle = this.fileHandle.then((handle) => {
  43. this.stream_ = handle.createWriteStream(filename);
  44. this.outputFunction_ = function (...args) {
  45. this.stream_.write(args.join(' '));
  46. this.stream_.write('\n');
  47. }.bind(this);
  48. this.stream_.on('error', function (_error) {
  49. console.info('Invalid log file. Debug information sent to console.');
  50. this.outputFunction_ = console.info;
  51. }.bind(this));
  52. this.stream_.on('finish', function () {
  53. console.info('Finalizing debug file.');
  54. });
  55. });
  56. }
  57. output_(outputList) {
  58. if (console.info === this.outputFunction_) {
  59. this.outputFunction_.apply(console, ['Speech Rule Engine Debugger:'].concat(outputList));
  60. return;
  61. }
  62. this.fileHandle.then(() => this.outputFunction_.apply(this.outputFunction_, ['Speech Rule Engine Debugger:'].concat(outputList)));
  63. }
  64. }
  65. exports.Debugger = Debugger;