Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

createLogger.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _detectNode = _interopRequireDefault(require("detect-node"));
  7. var _globalthis = _interopRequireDefault(require("globalthis"));
  8. var _jsonStringifySafe = _interopRequireDefault(require("json-stringify-safe"));
  9. var _sprintfJs = require("sprintf-js");
  10. var _constants = require("../constants");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  13. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  14. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  15. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  16. const globalThis = (0, _globalthis.default)();
  17. let domain;
  18. if (_detectNode.default) {
  19. // eslint-disable-next-line global-require
  20. domain = require('domain');
  21. }
  22. const getParentDomainContext = () => {
  23. if (!domain) {
  24. return {};
  25. }
  26. const parentRoarrContexts = [];
  27. let currentDomain = process.domain; // $FlowFixMe
  28. if (!currentDomain || !currentDomain.parentDomain) {
  29. return {};
  30. }
  31. while (currentDomain && currentDomain.parentDomain) {
  32. currentDomain = currentDomain.parentDomain;
  33. if (currentDomain.roarr && currentDomain.roarr.context) {
  34. parentRoarrContexts.push(currentDomain.roarr.context);
  35. }
  36. }
  37. let domainContext = {};
  38. for (const parentRoarrContext of parentRoarrContexts) {
  39. domainContext = _objectSpread(_objectSpread({}, domainContext), parentRoarrContext);
  40. }
  41. return domainContext;
  42. };
  43. const getFirstParentDomainContext = () => {
  44. if (!domain) {
  45. return {};
  46. }
  47. let currentDomain = process.domain; // $FlowFixMe
  48. if (currentDomain && currentDomain.roarr && currentDomain.roarr.context) {
  49. return currentDomain.roarr.context;
  50. } // $FlowFixMe
  51. if (!currentDomain || !currentDomain.parentDomain) {
  52. return {};
  53. }
  54. while (currentDomain && currentDomain.parentDomain) {
  55. currentDomain = currentDomain.parentDomain;
  56. if (currentDomain.roarr && currentDomain.roarr.context) {
  57. return currentDomain.roarr.context;
  58. }
  59. }
  60. return {};
  61. };
  62. const createLogger = (onMessage, parentContext) => {
  63. // eslint-disable-next-line id-length, unicorn/prevent-abbreviations
  64. const log = (a, b, c, d, e, f, g, h, i, k) => {
  65. const time = Date.now();
  66. const sequence = globalThis.ROARR.sequence++;
  67. let context;
  68. let message;
  69. if (typeof a === 'string') {
  70. context = _objectSpread(_objectSpread({}, getFirstParentDomainContext()), parentContext || {}); // eslint-disable-next-line id-length, object-property-newline
  71. const args = _extends({}, {
  72. a,
  73. b,
  74. c,
  75. d,
  76. e,
  77. f,
  78. g,
  79. h,
  80. i,
  81. k
  82. });
  83. const values = Object.keys(args).map(key => {
  84. return args[key];
  85. }); // eslint-disable-next-line unicorn/no-reduce
  86. const hasOnlyOneParameterValued = 1 === values.reduce((accumulator, value) => {
  87. // eslint-disable-next-line no-return-assign, no-param-reassign
  88. return accumulator += typeof value === 'undefined' ? 0 : 1;
  89. }, 0);
  90. message = hasOnlyOneParameterValued ? (0, _sprintfJs.sprintf)('%s', a) : (0, _sprintfJs.sprintf)(a, b, c, d, e, f, g, h, i, k);
  91. } else {
  92. if (typeof b !== 'string') {
  93. throw new TypeError('Message must be a string.');
  94. }
  95. context = JSON.parse((0, _jsonStringifySafe.default)(_objectSpread(_objectSpread(_objectSpread({}, getFirstParentDomainContext()), parentContext || {}), a)));
  96. message = (0, _sprintfJs.sprintf)(b, c, d, e, f, g, h, i, k);
  97. }
  98. onMessage({
  99. context,
  100. message,
  101. sequence,
  102. time,
  103. version: '1.0.0'
  104. });
  105. };
  106. log.child = context => {
  107. if (typeof context === 'function') {
  108. return createLogger(message => {
  109. if (typeof context !== 'function') {
  110. throw new TypeError('Unexpected state.');
  111. }
  112. onMessage(context(message));
  113. }, parentContext);
  114. }
  115. return createLogger(onMessage, _objectSpread(_objectSpread(_objectSpread({}, getFirstParentDomainContext()), parentContext), context));
  116. };
  117. log.getContext = () => {
  118. return _objectSpread(_objectSpread({}, getFirstParentDomainContext()), parentContext || {});
  119. };
  120. log.adopt = async (routine, context) => {
  121. if (!domain) {
  122. return routine();
  123. }
  124. const adoptedDomain = domain.create();
  125. return adoptedDomain.run(() => {
  126. // $FlowFixMe
  127. adoptedDomain.roarr = {
  128. context: _objectSpread(_objectSpread({}, getParentDomainContext()), context)
  129. };
  130. return routine();
  131. });
  132. };
  133. for (const logLevel of Object.keys(_constants.logLevels)) {
  134. // eslint-disable-next-line id-length, unicorn/prevent-abbreviations
  135. log[logLevel] = (a, b, c, d, e, f, g, h, i, k) => {
  136. return log.child({
  137. logLevel: _constants.logLevels[logLevel]
  138. })(a, b, c, d, e, f, g, h, i, k);
  139. };
  140. } // @see https://github.com/facebook/flow/issues/6705
  141. // $FlowFixMe
  142. return log;
  143. };
  144. var _default = createLogger;
  145. exports.default = _default;
  146. //# sourceMappingURL=createLogger.js.map