@plugjs/plug
Version:
PlugJS Build System ===================
137 lines (136 loc) • 3.98 kB
JavaScript
// logging/logger.ts
import { formatWithOptions } from "node:util";
import { BuildFailure } from "../asserts.mjs";
import { currentContext } from "../async.mjs";
import { stripAnsi } from "../utils/ansi.mjs";
import { $gry } from "./colors.mjs";
import { emit } from "./emit.mjs";
import { DEBUG, ERROR, INFO, NOTICE, TRACE, WARN } from "./levels.mjs";
import { logOptions } from "./options.mjs";
import { ReportImpl } from "./report.mjs";
var _level = logOptions.level;
logOptions.on("changed", ({ level }) => {
_level = level;
});
function getLogger(task, indent) {
const context = currentContext();
const taskName = task === void 0 ? context?.taskName || "" : task;
const indentLevel = indent === void 0 ? context?.log.indent || 0 : 0;
return new LoggerImpl(taskName, emit, indentLevel);
}
var _loggedFailures = /* @__PURE__ */ new WeakSet();
var LoggerImpl = class {
constructor(_task, _emitter, indent) {
this._task = _task;
this._emitter = _emitter;
this.indent = indent;
}
_stack = [];
level = _level;
_emit(level, args, taskName = this._task) {
if (this.level > level) return;
const params = args.filter((arg) => {
if (arg instanceof BuildFailure) {
if (_loggedFailures.has(arg)) return false;
_loggedFailures.add(arg);
arg.errors?.forEach((error) => this._emit(level, [error]));
if (!arg.message) return false;
if (_level < INFO) return true;
this._emit(level, [arg.message]);
return false;
} else {
return true;
}
});
if (params.length === 0) return;
const options = { level, taskName, indent: this.indent };
if (this._stack.length) {
for (const { message, ...extras } of this._stack) {
this._emitter({ ...options, ...extras }, [message]);
}
this._stack.splice(0);
}
this._emitter(options, params);
}
trace(...args) {
this._emit(TRACE, args);
}
debug(...args) {
this._emit(DEBUG, args);
}
info(...args) {
this._emit(INFO, args);
}
notice(...args) {
this._emit(NOTICE, args);
}
warn(...args) {
this._emit(WARN, args);
}
error(...args) {
this._emit(ERROR, args);
}
fail(...args) {
this._emit(ERROR, args);
throw BuildFailure.fail();
}
enter(...args) {
if (args.length) {
const [level, message] = args;
this._stack.push({ level, message, indent: this.indent });
}
this.indent++;
}
leave(...args) {
this._stack.pop();
this.indent--;
if (this.indent < 0) this.indent = 0;
if (args.length) {
const [level, message] = args;
this._emit(level, [message]);
}
}
report(title) {
const emitter = (options, args) => {
if (this._stack.length) {
for (const { message, ...extras } of this._stack) {
this._emitter({ ...options, ...extras }, [message]);
}
this._stack.splice(0);
}
let { indent = 0, prefix = "" } = options;
prefix = this.indent ? $gry("| ") + prefix : prefix;
indent += this.indent;
this._emitter({ ...options, indent, prefix }, args);
};
return new ReportImpl(title, this._task, emitter);
}
};
var TestLogger = class extends LoggerImpl {
_lines = [];
constructor() {
super("", (options, args) => {
const { prefix = "", indent = 0 } = options;
const linePrefix = "".padStart(indent * 2) + prefix;
formatWithOptions({ colors: false, breakLength: 120 }, ...args).split("\n").forEach((line) => {
const stripped = stripAnsi(line);
this._lines.push(`${linePrefix}${stripped}`);
});
}, 0);
}
/** Return the _current_ buffer for this instance */
get buffer() {
return this._lines.join("\n");
}
/** Reset the buffer and return any previously buffered text */
reset() {
const buffer = this.buffer;
this._lines = [];
return buffer;
}
};
export {
TestLogger,
getLogger
};
//# sourceMappingURL=logger.mjs.map