mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Add verbose logging setting + setup buffering of logs to prevent interleaving logs with the package manager.
This commit is contained in:
parent
0b393eeb5f
commit
c5e25f4813
4 changed files with 64 additions and 7 deletions
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue