logging.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2. const fs = require('fs');
  3. const { Console } = require('console');
  4. var loggers=[];
  5. var logFunctions=[];
  6. var logColorFunctions=[];
  7. console.log = function(msg, ...args) {
  8. logFunctions.forEach((logFunction) => {
  9. logFunction(msg, ...args);
  10. });
  11. }
  12. console.logColor = function(color, msg, ...args) {
  13. logColorFunctions.forEach((logColorFunction) => {
  14. logColorFunction(color, msg, ...args);
  15. });
  16. }
  17. const AllAttributesOff = '\x1b[0m';
  18. const BoldOn = '\x1b[1m';
  19. const Black = '\x1b[30m';
  20. const Red = '\x1b[31m';
  21. const Green = '\x1b[32m';
  22. const Yellow = '\x1b[33m';
  23. const Blue = '\x1b[34m';
  24. const Magenta = '\x1b[35m';
  25. const Cyan = '\x1b[36m';
  26. const White = '\x1b[37m';
  27. /**
  28. * Pad the start of the given number with zeros so it takes up the number of digits.
  29. * e.g. zeroPad(5, 3) = '005' and zeroPad(23, 2) = '23'.
  30. */
  31. function zeroPad(number, digits) {
  32. let string = number.toString();
  33. while (string.length < digits) {
  34. string = '0' + string;
  35. }
  36. return string;
  37. }
  38. /**
  39. * Create a string of the form 'YEAR.MONTH.DATE.HOURS.MINUTES.SECONDS'.
  40. */
  41. function dateTimeToString() {
  42. let date = new Date();
  43. return `${date.getFullYear()}.${zeroPad(date.getMonth(), 2)}.${zeroPad(date.getDate(), 2)}.${zeroPad(date.getHours(), 2)}.${zeroPad(date.getMinutes(), 2)}.${zeroPad(date.getSeconds(), 2)}`;
  44. }
  45. /**
  46. * Create a string of the form 'HOURS.MINUTES.SECONDS.MILLISECONDS'.
  47. */
  48. function timeToString() {
  49. let date = new Date();
  50. return `${zeroPad(date.getHours(), 2)}:${zeroPad(date.getMinutes(), 2)}:${zeroPad(date.getSeconds(), 2)}.${zeroPad(date.getMilliseconds(), 3)}`;
  51. }
  52. function RegisterFileLogger(path) {
  53. if(path == null)
  54. path = './';
  55. if (!fs.existsSync(path))
  56. fs.mkdirSync(path);
  57. var output = fs.createWriteStream(`./logs/${dateTimeToString()}.log`);
  58. var fileLogger = new Console(output);
  59. logFunctions.push(function(msg, ...args) {
  60. fileLogger.log(`${timeToString()} ${msg}`, ...args);
  61. });
  62. logColorFunctions.push(function(color, msg, ...args) {
  63. fileLogger.log(`${timeToString()} ${msg}`, ...args);
  64. });
  65. loggers.push(fileLogger);
  66. }
  67. function RegisterConsoleLogger() {
  68. var consoleLogger = new Console(process.stdout, process.stderr)
  69. logFunctions.push(function(msg, ...args) {
  70. consoleLogger.log(`${timeToString()} ${msg}`, ...args);
  71. });
  72. logColorFunctions.push(function(color, msg, ...args) {
  73. consoleLogger.log(`${BoldOn}${color}${timeToString()} ${msg}${AllAttributesOff}`, ...args);
  74. });
  75. loggers.push(consoleLogger);
  76. }
  77. module.exports = {
  78. //Functions
  79. RegisterFileLogger,
  80. RegisterConsoleLogger,
  81. //Variables
  82. AllAttributesOff,
  83. BoldOn,
  84. Black,
  85. Red,
  86. Green,
  87. Yellow,
  88. Blue,
  89. Magenta,
  90. Cyan,
  91. White
  92. }