UNPKG

@plugjs/plug

Version:
107 lines (106 loc) 4.3 kB
// logging/emit.ts import { formatWithOptions } from "node:util"; import { fail } from "../asserts.mjs"; import { $blu, $grn, $gry, $red, $t, $ylw } from "./colors.mjs"; import { DEBUG, INFO, NOTICE, TRACE, WARN } from "./levels.mjs"; import { logOptions } from "./options.mjs"; import { zapSpinner } from "./spinner.mjs"; var _output = logOptions.output; var _indentSize = logOptions.indentSize; var _taskLength = logOptions.taskLength; var _lineLength = logOptions.lineLength; var _inspectOptions = { ...logOptions.inspectOptions }; logOptions.on("changed", (options) => { _output = options.output; _indentSize = options.indentSize; _taskLength = options.taskLength; _lineLength = options.lineLength; _inspectOptions = { ...options.inspectOptions }; _defaultEmitter = options.format === "fancy" ? emitFancy : options.format === "plain" ? emitPlain : fail(`Invalid log format "${logOptions.format}"`); }); var emitFancy = (options, args) => { const { taskName, level, prefix = "", indent = 0 } = options; const logPrefix = "".padStart(indent * _indentSize) + prefix; const prefixes = []; prefixes.push("".padStart(_taskLength - taskName.length, " ")); prefixes.push(`${$t(taskName, false)}`); if (level <= TRACE) { prefixes.push(` ${$gry("\u25A1")} `); } else if (level <= DEBUG) { prefixes.push(` ${$gry("\u25A0")} `); } else if (level <= INFO) { prefixes.push(` ${$grn("\u25A0")} `); } else if (level <= NOTICE) { prefixes.push(` ${$blu("\u25A0")} `); } else if (level <= WARN) { prefixes.push(` ${$ylw("\u25A0")} `); } else { prefixes.push(` ${$red("\u25A0")} `); } prefixes.push(logPrefix); const linePrefix = prefixes.join(""); const breakLength = _lineLength - _taskLength - logPrefix.length - 3; const message = formatWithOptions({ ..._inspectOptions, breakLength }, ...args); for (const line of message.split("\n")) { _output.write(`${zapSpinner}${linePrefix}${line} `); } }; var emitPlain = (options, args) => { const { taskName, level, prefix = "", indent = 0 } = options; const logPrefix = "".padStart(indent * _indentSize) + prefix; const prefixes = []; const pad = "".padStart(_taskLength - taskName.length, " "); prefixes.push(`${pad}${$t(taskName, false)}`); if (level <= TRACE) { prefixes.push(` ${$gry("\u2502")} ${$gry(" trace")} ${$gry("\u2502")} `); } else if (level <= DEBUG) { prefixes.push(` ${$gry("\u2502")} ${$gry(" debug")} ${$gry("\u2502")} `); } else if (level <= INFO) { prefixes.push(` ${$gry("\u2502")} ${$grn(" info")} ${$gry("\u2502")} `); } else if (level <= NOTICE) { prefixes.push(` ${$gry("\u2502")} ${$blu("notice")} ${$gry("\u2502")} `); } else if (level <= WARN) { prefixes.push(` ${$gry("\u2502")} ${$ylw(" warn")} ${$gry("\u2502")} `); } else { prefixes.push(` ${$gry("\u2502")} ${$red(" error")} ${$gry("\u2502")} `); } prefixes.push(logPrefix); const linePrefix = prefixes.join(""); const breakLength = _lineLength - _taskLength - logPrefix.length - 12; const message = formatWithOptions({ ..._inspectOptions, breakLength }, ...args); for (const line of message.split("\n")) { _output.write(`${linePrefix}${line} `); } }; var emitForked = (options, args) => { if (process.connected && process.send) { const { taskName, level, prefix = "", indent = 0 } = options; const linePrefix = "".padStart(indent * _indentSize) + prefix; const breakLength = _lineLength - _taskLength - linePrefix.length - 20; const message = formatWithOptions({ ..._inspectOptions, breakLength }, ...args); const lines = message.split("\n").map((line) => `${linePrefix}${line}`); process.send({ logLevel: level, taskName, lines }); } else { _defaultEmitter(options, args); } }; var _defaultEmitter = logOptions.format === "fancy" ? emitFancy : logOptions.format === "plain" ? emitPlain : fail(`Invalid log format "${logOptions.format}"`); var _emitter = _defaultEmitter; var wrapper = function emit(options, args) { _defaultEmitter(options, args); }; var emit2 = Object.defineProperty(wrapper, "emitter", { get: () => _emitter, set: (emitter) => { _emitter = emitter || _defaultEmitter; } }); export { emit2 as emit, emitFancy, emitForked, emitPlain }; //# sourceMappingURL=emit.mjs.map