UNPKG

@rushstack/heft

Version:

Build all your JavaScript projects the same way: A way that works.

71 lines 2.77 kB
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import { FileError } from '@rushstack/node-core-library'; import { ScopedLogger } from './ScopedLogger'; export class LoggingManager { get errorsHaveBeenEmitted() { return this._hasAnyErrors; } get warningsHaveBeenEmitted() { return this._hasAnyWarnings; } constructor(options) { this._scopedLoggers = new Map(); this._shouldPrintStacks = false; this._hasAnyWarnings = false; this._hasAnyErrors = false; this._options = options; } enablePrintStacks() { this._shouldPrintStacks = true; } resetScopedLoggerErrorsAndWarnings() { this._hasAnyErrors = false; this._hasAnyWarnings = false; for (const scopedLogger of this._scopedLoggers.values()) { scopedLogger.resetErrorsAndWarnings(); } } requestScopedLogger(loggerName) { const existingScopedLogger = this._scopedLoggers.get(loggerName); if (existingScopedLogger) { throw new Error(`A named logger with name ${JSON.stringify(loggerName)} has already been requested.`); } else { const scopedLogger = new ScopedLogger({ loggerName, terminalProvider: this._options.terminalProvider, getShouldPrintStacks: () => this._shouldPrintStacks, errorHasBeenEmittedCallback: () => (this._hasAnyErrors = true), warningHasBeenEmittedCallback: () => (this._hasAnyWarnings = true) }); this._scopedLoggers.set(loggerName, scopedLogger); return scopedLogger; } } getErrorStrings(fileLocationStyle) { const result = []; for (const scopedLogger of this._scopedLoggers.values()) { result.push(...scopedLogger.errors.map((error) => `[${scopedLogger.loggerName}] ` + LoggingManager.getErrorMessage(error, { format: fileLocationStyle }))); } return result; } getWarningStrings(fileErrorFormat) { const result = []; for (const scopedLogger of this._scopedLoggers.values()) { result.push(...scopedLogger.warnings.map((warning) => `[${scopedLogger.loggerName}] ` + LoggingManager.getErrorMessage(warning, { format: fileErrorFormat }))); } return result; } static getErrorMessage(error, options) { if (error instanceof FileError) { return error.getFormattedErrorMessage(options); } else { return error.message; } } } //# sourceMappingURL=LoggingManager.js.map