summaryrefslogtreecommitdiff
path: root/node_modules/roarr/dist/factories/createLogger.js.flow
diff options
context:
space:
mode:
authorLinuxWizard42 <computerwizard@linuxmail.org>2022-10-12 22:54:37 +0300
committerLinuxWizard42 <computerwizard@linuxmail.org>2022-10-12 22:54:37 +0300
commit703e03aba33f234712206769f57717ba7d92d23d (patch)
tree0041f04ccb75bd5379c764e9fe42249fffe75fc3 /node_modules/roarr/dist/factories/createLogger.js.flow
parentab6e257e6e9d9a483d7e86f220d8b209a2cd7753 (diff)
downloadFlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.gz
FlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.zst
Added export_allowed file to make repository visible in cgit
Diffstat (limited to 'node_modules/roarr/dist/factories/createLogger.js.flow')
-rw-r--r--node_modules/roarr/dist/factories/createLogger.js.flow197
1 files changed, 197 insertions, 0 deletions
diff --git a/node_modules/roarr/dist/factories/createLogger.js.flow b/node_modules/roarr/dist/factories/createLogger.js.flow
new file mode 100644
index 0000000..4cd6c27
--- /dev/null
+++ b/node_modules/roarr/dist/factories/createLogger.js.flow
@@ -0,0 +1,197 @@
+// @flow
+
+import environmentIsNode from 'detect-node';
+import createGlobalThis from 'globalthis';
+import stringify from 'json-stringify-safe';
+import {
+ sprintf,
+} from 'sprintf-js';
+import {
+ logLevels,
+} from '../constants';
+import type {
+ LoggerType,
+ MessageContextType,
+ MessageEventHandlerType,
+ TranslateMessageFunctionType,
+} from '../types';
+
+const globalThis = createGlobalThis();
+
+let domain;
+
+if (environmentIsNode) {
+ // eslint-disable-next-line global-require
+ domain = require('domain');
+}
+
+const getParentDomainContext = () => {
+ if (!domain) {
+ return {};
+ }
+
+ const parentRoarrContexts = [];
+
+ let currentDomain = process.domain;
+
+ // $FlowFixMe
+ if (!currentDomain || !currentDomain.parentDomain) {
+ return {};
+ }
+
+ while (currentDomain && currentDomain.parentDomain) {
+ currentDomain = currentDomain.parentDomain;
+
+ if (currentDomain.roarr && currentDomain.roarr.context) {
+ parentRoarrContexts.push(currentDomain.roarr.context);
+ }
+ }
+
+ let domainContext = {};
+
+ for (const parentRoarrContext of parentRoarrContexts) {
+ domainContext = {
+ ...domainContext,
+ ...parentRoarrContext,
+ };
+ }
+
+ return domainContext;
+};
+
+const getFirstParentDomainContext = () => {
+ if (!domain) {
+ return {};
+ }
+
+ let currentDomain = process.domain;
+
+ // $FlowFixMe
+ if (currentDomain && currentDomain.roarr && currentDomain.roarr.context) {
+ return currentDomain.roarr.context;
+ }
+
+ // $FlowFixMe
+ if (!currentDomain || !currentDomain.parentDomain) {
+ return {};
+ }
+
+ while (currentDomain && currentDomain.parentDomain) {
+ currentDomain = currentDomain.parentDomain;
+
+ if (currentDomain.roarr && currentDomain.roarr.context) {
+ return currentDomain.roarr.context;
+ }
+ }
+
+ return {};
+};
+
+const createLogger = (onMessage: MessageEventHandlerType, parentContext?: MessageContextType): LoggerType => {
+ // eslint-disable-next-line id-length, unicorn/prevent-abbreviations
+ const log = (a, b, c, d, e, f, g, h, i, k) => {
+ const time = Date.now();
+ const sequence = globalThis.ROARR.sequence++;
+
+ let context;
+ let message;
+
+ if (typeof a === 'string') {
+ context = {
+ ...getFirstParentDomainContext(),
+ ...parentContext || {},
+ };
+ // eslint-disable-next-line id-length, object-property-newline
+ const {...args} = {a, b, c, d, e, f, g, h, i, k};
+ const values = Object.keys(args).map((key) => {
+ return args[key];
+ });
+ // eslint-disable-next-line unicorn/no-reduce
+ const hasOnlyOneParameterValued = 1 === values.reduce((accumulator, value) => {
+ // eslint-disable-next-line no-return-assign, no-param-reassign
+ return accumulator += typeof value === 'undefined' ? 0 : 1;
+ }, 0);
+ message = hasOnlyOneParameterValued ? sprintf('%s', a) : sprintf(a, b, c, d, e, f, g, h, i, k);
+ } else {
+ if (typeof b !== 'string') {
+ throw new TypeError('Message must be a string.');
+ }
+
+ context = JSON.parse(stringify({
+ ...getFirstParentDomainContext(),
+ ...parentContext || {},
+ ...a,
+ }));
+
+ message = sprintf(b, c, d, e, f, g, h, i, k);
+ }
+
+ onMessage({
+ context,
+ message,
+ sequence,
+ time,
+ version: '1.0.0',
+ });
+ };
+
+ log.child = (context: TranslateMessageFunctionType | MessageContextType): LoggerType => {
+ if (typeof context === 'function') {
+ return createLogger((message) => {
+ if (typeof context !== 'function') {
+ throw new TypeError('Unexpected state.');
+ }
+ onMessage(context(message));
+ }, parentContext);
+ }
+
+ return createLogger(onMessage, {
+ ...getFirstParentDomainContext(),
+ ...parentContext,
+ ...context,
+ });
+ };
+
+ log.getContext = (): MessageContextType => {
+ return {
+ ...getFirstParentDomainContext(),
+ ...parentContext || {},
+ };
+ };
+
+ log.adopt = async (routine, context) => {
+ if (!domain) {
+ return routine();
+ }
+
+ const adoptedDomain = domain.create();
+
+ return adoptedDomain
+ .run(() => {
+ // $FlowFixMe
+ adoptedDomain.roarr = {
+ context: {
+ ...getParentDomainContext(),
+ ...context,
+ },
+ };
+
+ return routine();
+ });
+ };
+
+ for (const logLevel of Object.keys(logLevels)) {
+ // eslint-disable-next-line id-length, unicorn/prevent-abbreviations
+ log[logLevel] = (a, b, c, d, e, f, g, h, i, k) => {
+ return log.child({
+ logLevel: logLevels[logLevel],
+ })(a, b, c, d, e, f, g, h, i, k);
+ };
+ }
+
+ // @see https://github.com/facebook/flow/issues/6705
+ // $FlowFixMe
+ return log;
+};
+
+export default createLogger;