logger.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Logger used througouht the application to allow configuration of
  3. * the log level required for the messages.
  4. */
  5. export class Logger {
  6. /**
  7. * No log
  8. */
  9. public static readonly NoneLogLevel = 0;
  10. /**
  11. * Only message logs
  12. */
  13. public static readonly MessageLogLevel = 1;
  14. /**
  15. * Only warning logs
  16. */
  17. public static readonly WarningLogLevel = 2;
  18. /**
  19. * Only error logs
  20. */
  21. public static readonly ErrorLogLevel = 4;
  22. /**
  23. * All logs
  24. */
  25. public static readonly AllLogLevel = 7;
  26. private static _LogCache = "";
  27. /**
  28. * Gets a value indicating the number of loading errors
  29. * @ignorenaming
  30. */
  31. public static errorsCount = 0;
  32. /**
  33. * Callback called when a new log is added
  34. */
  35. public static OnNewCacheEntry: (entry: string) => void;
  36. private static _AddLogEntry(entry: string) {
  37. Logger._LogCache = entry + Logger._LogCache;
  38. if (Logger.OnNewCacheEntry) {
  39. Logger.OnNewCacheEntry(entry);
  40. }
  41. }
  42. private static _FormatMessage(message: string): string {
  43. var padStr = (i: number) => (i < 10) ? "0" + i : "" + i;
  44. var date = new Date();
  45. return "[" + padStr(date.getHours()) + ":" + padStr(date.getMinutes()) + ":" + padStr(date.getSeconds()) + "]: " + message;
  46. }
  47. private static _LogDisabled(message: string): void {
  48. // nothing to do
  49. }
  50. private static _LogEnabled(message: string): void {
  51. var formattedMessage = Logger._FormatMessage(message);
  52. console.log("BJS - " + formattedMessage);
  53. var entry = "<div style='color:white'>" + formattedMessage + "</div><br>";
  54. Logger._AddLogEntry(entry);
  55. }
  56. private static _WarnDisabled(message: string): void {
  57. // nothing to do
  58. }
  59. private static _WarnEnabled(message: string): void {
  60. var formattedMessage = Logger._FormatMessage(message);
  61. console.warn("BJS - " + formattedMessage);
  62. var entry = "<div style='color:orange'>" + formattedMessage + "</div><br>";
  63. Logger._AddLogEntry(entry);
  64. }
  65. private static _ErrorDisabled(message: string): void {
  66. // nothing to do
  67. }
  68. private static _ErrorEnabled(message: string): void {
  69. Logger.errorsCount++;
  70. var formattedMessage = Logger._FormatMessage(message);
  71. console.error("BJS - " + formattedMessage);
  72. var entry = "<div style='color:red'>" + formattedMessage + "</div><br>";
  73. Logger._AddLogEntry(entry);
  74. }
  75. /**
  76. * Log a message to the console
  77. */
  78. public static Log: (message: string) => void = Logger._LogEnabled;
  79. /**
  80. * Write a warning message to the console
  81. */
  82. public static Warn: (message: string) => void = Logger._WarnEnabled;
  83. /**
  84. * Write an error message to the console
  85. */
  86. public static Error: (message: string) => void = Logger._ErrorEnabled;
  87. /**
  88. * Gets current log cache (list of logs)
  89. */
  90. public static get LogCache(): string {
  91. return Logger._LogCache;
  92. }
  93. /**
  94. * Clears the log cache
  95. */
  96. public static ClearLogCache(): void {
  97. Logger._LogCache = "";
  98. Logger.errorsCount = 0;
  99. }
  100. /**
  101. * Sets the current log level (MessageLogLevel / WarningLogLevel / ErrorLogLevel)
  102. */
  103. public static set LogLevels(level: number) {
  104. if ((level & Logger.MessageLogLevel) === Logger.MessageLogLevel) {
  105. Logger.Log = Logger._LogEnabled;
  106. }
  107. else {
  108. Logger.Log = Logger._LogDisabled;
  109. }
  110. if ((level & Logger.WarningLogLevel) === Logger.WarningLogLevel) {
  111. Logger.Warn = Logger._WarnEnabled;
  112. }
  113. else {
  114. Logger.Warn = Logger._WarnDisabled;
  115. }
  116. if ((level & Logger.ErrorLogLevel) === Logger.ErrorLogLevel) {
  117. Logger.Error = Logger._ErrorEnabled;
  118. }
  119. else {
  120. Logger.Error = Logger._ErrorDisabled;
  121. }
  122. }
  123. }