Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

LogService.ts 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright (c) 2019 The xterm.js authors. All rights reserved.
  3. * @license MIT
  4. */
  5. import { ILogService, IOptionsService } from 'common/services/Services';
  6. type LogType = (message?: any, ...optionalParams: any[]) => void;
  7. interface IConsole {
  8. log: LogType;
  9. error: LogType;
  10. info: LogType;
  11. trace: LogType;
  12. warn: LogType;
  13. }
  14. // console is available on both node.js and browser contexts but the common
  15. // module doesn't depend on them so we need to explicitly declare it.
  16. declare const console: IConsole;
  17. export enum LogLevel {
  18. DEBUG = 0,
  19. INFO = 1,
  20. WARN = 2,
  21. ERROR = 3,
  22. OFF = 4
  23. }
  24. const optionsKeyToLogLevel: { [key: string]: LogLevel } = {
  25. debug: LogLevel.DEBUG,
  26. info: LogLevel.INFO,
  27. warn: LogLevel.WARN,
  28. error: LogLevel.ERROR,
  29. off: LogLevel.OFF
  30. };
  31. const LOG_PREFIX = 'xterm.js: ';
  32. export class LogService implements ILogService {
  33. serviceBrand: any;
  34. private _logLevel!: LogLevel;
  35. constructor(
  36. @IOptionsService private readonly _optionsService: IOptionsService
  37. ) {
  38. this._updateLogLevel();
  39. this._optionsService.onOptionChange(key => {
  40. if (key === 'logLevel') {
  41. this._updateLogLevel();
  42. }
  43. });
  44. }
  45. private _updateLogLevel(): void {
  46. this._logLevel = optionsKeyToLogLevel[this._optionsService.options.logLevel];
  47. }
  48. private _evalLazyOptionalParams(optionalParams: any[]): void {
  49. for (let i = 0; i < optionalParams.length; i++) {
  50. if (typeof optionalParams[i] === 'function') {
  51. optionalParams[i] = optionalParams[i]();
  52. }
  53. }
  54. }
  55. private _log(type: LogType, message: string, optionalParams: any[]): void {
  56. this._evalLazyOptionalParams(optionalParams);
  57. type.call(console, LOG_PREFIX + message, ...optionalParams);
  58. }
  59. debug(message: string, ...optionalParams: any[]): void {
  60. if (this._logLevel <= LogLevel.DEBUG) {
  61. this._log(console.log, message, optionalParams);
  62. }
  63. }
  64. info(message: string, ...optionalParams: any[]): void {
  65. if (this._logLevel <= LogLevel.INFO) {
  66. this._log(console.info, message, optionalParams);
  67. }
  68. }
  69. warn(message: string, ...optionalParams: any[]): void {
  70. if (this._logLevel <= LogLevel.WARN) {
  71. this._log(console.warn, message, optionalParams);
  72. }
  73. }
  74. error(message: string, ...optionalParams: any[]): void {
  75. if (this._logLevel <= LogLevel.ERROR) {
  76. this._log(console.error, message, optionalParams);
  77. }
  78. }
  79. }