Add verbose logging setting + setup buffering of logs to prevent interleaving logs with the package manager.

This commit is contained in:
Sander Declerck 2025-10-27 17:09:28 +01:00
parent 0b393eeb5f
commit c5e25f4813
No known key found for this signature in database
4 changed files with 64 additions and 7 deletions

View file

@ -2,12 +2,25 @@
import chalk from "chalk";
import ora from "ora";
import { isCi } from "./environment.js";
import { getLoggingLevel, LOGGING_SILENT } from "../config/settings.js";
import {
getLoggingLevel,
LOGGING_SILENT,
LOGGING_VERBOSE,
} from "../config/settings.js";
const state = {
bufferOutput: false,
bufferedMessages: [],
};
function isSilentMode() {
return getLoggingLevel() === LOGGING_SILENT;
}
function isVerboseMode() {
return getLoggingLevel() === LOGGING_VERBOSE;
}
function emptyLine() {
if (isSilentMode()) return;
@ -17,7 +30,7 @@ function emptyLine() {
function writeInformation(message, ...optionalParams) {
if (isSilentMode()) return;
console.log(message, ...optionalParams);
writeOrBuffer(() => console.log(message, ...optionalParams));
}
function writeWarning(message, ...optionalParams) {
@ -26,14 +39,14 @@ function writeWarning(message, ...optionalParams) {
if (!isCi()) {
message = chalk.yellow(message);
}
console.warn(message, ...optionalParams);
writeOrBuffer(() => console.warn(message, ...optionalParams));
}
function writeError(message, ...optionalParams) {
if (!isCi()) {
message = chalk.red(message);
}
console.error(message, ...optionalParams);
writeOrBuffer(() => console.error(message, ...optionalParams));
}
function writeExitWithoutInstallingMaliciousPackages() {
@ -41,12 +54,21 @@ function writeExitWithoutInstallingMaliciousPackages() {
if (!isCi()) {
message = chalk.red(message);
}
console.error(message);
writeOrBuffer(() => console.error(message));
}
function writeVerboseInformation(message, ...optionalParams) {
// TODO: Correctly implement verbose logging
writeInformation(message, ...optionalParams);
if (!isVerboseMode()) return;
writeOrBuffer(() => console.log(message, ...optionalParams));
}
function writeOrBuffer(messageFunction) {
if (state.bufferOutput) {
state.bufferedMessages.push(messageFunction);
} else {
messageFunction();
}
}
function startProcess(message) {
@ -91,6 +113,19 @@ function startProcess(message) {
}
}
function startBufferingLogs() {
state.bufferOutput = true;
state.bufferedMessages = [];
}
function writeBufferedLogsAndStopBuffering() {
state.bufferOutput = false;
for (const log of state.bufferedMessages) {
log();
}
state.bufferedMessages = [];
}
export const ui = {
writeInformation,
writeVerboseInformation,
@ -99,4 +134,6 @@ export const ui = {
writeExitWithoutInstallingMaliciousPackages,
emptyLine,
startProcess,
startBufferingLogs,
writeBufferedLogsAndStopBuffering,
};