Repository navigation
Expand file tree
/
Copy pathLoggingManager.ts
More file actions
108 lines (92 loc) · 3.38 KB
/
Copy pathLoggingManager.ts
File metadata and controls
108 lines (92 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
FileError,
type FileLocationStyle,
type IFileErrorFormattingOptions
} from '@rushstack/node-core-library';
import type { ITerminalProvider } from '@rushstack/terminal';
import type { HeftChildReporter } from './HeftChildReporter';
import { ScopedLogger } from './ScopedLogger';
export interface ILoggingManagerOptions {
terminalProvider: ITerminalProvider;
childReporter?: HeftChildReporter;
}
export class LoggingManager {
#options: ILoggingManagerOptions;
#scopedLoggers: Map<string, ScopedLogger> = new Map<string, ScopedLogger>();
#shouldPrintStacks: boolean = false;
#hasAnyWarnings: boolean = false;
#hasAnyErrors: boolean = false;
public get errorsHaveBeenEmitted(): boolean {
return this.#hasAnyErrors;
}
public get warningsHaveBeenEmitted(): boolean {
return this.#hasAnyWarnings;
}
public constructor(options: ILoggingManagerOptions) {
this.#options = options;
}
public enablePrintStacks(): void {
this.#shouldPrintStacks = true;
}
public resetScopedLoggerErrorsAndWarnings(): void {
this.#hasAnyErrors = false;
this.#hasAnyWarnings = false;
for (const scopedLogger of this.#scopedLoggers.values()) {
scopedLogger.resetErrorsAndWarnings();
}
}
public requestScopedLogger(loggerName: string): ScopedLogger {
const existingScopedLogger: ScopedLogger | undefined = this.#scopedLoggers.get(loggerName);
if (existingScopedLogger) {
throw new Error(`A named logger with name ${JSON.stringify(loggerName)} has already been requested.`);
} else {
const scopedLogger: ScopedLogger = new ScopedLogger({
loggerName,
terminalProvider: this.#options.terminalProvider,
getShouldPrintStacks: () => this.#shouldPrintStacks,
errorHasBeenEmittedCallback: () => (this.#hasAnyErrors = true),
warningHasBeenEmittedCallback: () => (this.#hasAnyWarnings = true),
structuredDiagnosticCallback: this.#options.childReporter
? (error, severity) => this.#options.childReporter!.emitDiagnostic(loggerName, error, severity)
: undefined
});
this.#scopedLoggers.set(loggerName, scopedLogger);
return scopedLogger;
}
}
public getErrorStrings(fileLocationStyle?: FileLocationStyle): string[] {
const result: string[] = [];
for (const scopedLogger of this.#scopedLoggers.values()) {
result.push(
...scopedLogger.errors.map(
(error) =>
`[${scopedLogger.loggerName}] ` +
LoggingManager.getErrorMessage(error, { format: fileLocationStyle })
)
);
}
return result;
}
public getWarningStrings(fileErrorFormat?: FileLocationStyle): string[] {
const result: string[] = [];
for (const scopedLogger of this.#scopedLoggers.values()) {
result.push(
...scopedLogger.warnings.map(
(warning) =>
`[${scopedLogger.loggerName}] ` +
LoggingManager.getErrorMessage(warning, { format: fileErrorFormat })
)
);
}
return result;
}
public static getErrorMessage(error: Error, options?: IFileErrorFormattingOptions): string {
if (error instanceof FileError) {
return error.getFormattedErrorMessage(options);
} else {
return error.message;
}
}
}