Skip to content

Commit 0225c47

Browse files
CHANGE: @W-18088706@: Improve readability of error and warning events in terminal (#1876)
1 parent e1ae619 commit 0225c47

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/lib/listeners/LogEventListener.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {CodeAnalyzer, EngineLogEvent, EventType, LogEvent, LogLevel} from '@sale
22
import {Display} from '../Display';
33
import {LogWriter} from '../writers/LogWriter';
44
import {BundleName, getMessage} from "../messages";
5+
import {indent, makeGrey} from '../utils/StylingUtil';
56

67
export interface LogEventListener {
78
listen(codeAnalyzer: CodeAnalyzer): void;
@@ -17,8 +18,8 @@ export class LogEventDisplayer implements LogEventListener {
1718

1819
public listen(codeAnalyzer: CodeAnalyzer): void {
1920
// Set up listeners
20-
codeAnalyzer.onEvent(EventType.LogEvent, (e: LogEvent) => this.handleEvent('Core', e));
21-
codeAnalyzer.onEvent(EventType.EngineLogEvent, (e: EngineLogEvent) => this.handleEvent(e.engineName, e));
21+
codeAnalyzer.onEvent(EventType.LogEvent, (e: LogEvent) => this.handleEvent('Code Analyzer', e));
22+
codeAnalyzer.onEvent(EventType.EngineLogEvent, (e: EngineLogEvent) => this.handleEvent(`Engine '${e.engineName}'`, e));
2223
}
2324

2425
public stopListening(): void {
@@ -31,16 +32,25 @@ export class LogEventDisplayer implements LogEventListener {
3132
if (event.logLevel > LogLevel.Info) {
3233
return;
3334
}
34-
const formattedMessage = `${source} [${formatTimestamp(event.timestamp)}]: ${event.message}`;
35+
const decoratedTimestamp = makeGrey(`[${formatTimestamp(event.timestamp)}]`);
36+
const formattedMessage = `${source} ${decoratedTimestamp}:\n${indent(event.message)}`;
3537
switch (event.logLevel) {
3638
case LogLevel.Error:
3739
this.display.displayError(formattedMessage);
40+
// Adds a newline outside of the error formatting to make errors easy to read. That is, we do not want
41+
// to add a \n inside of the above displayError or use displayError('') here because it always adds in a
42+
// red "">" character.
43+
this.display.displayInfo('');
3844
return;
3945
case LogLevel.Warn:
4046
this.display.displayWarning(formattedMessage);
47+
// Likewise, we want spacing here, but don't want to add in displayWarning('') because it adds back in
48+
// the word "Warning". So it's best to just use displayInfo('') to add in a blank line.
49+
this.display.displayInfo('');
4150
return;
4251
case LogLevel.Info:
4352
this.display.displayInfo(formattedMessage);
53+
this.display.displayInfo('');
4454
return;
4555
}
4656
}
@@ -72,5 +82,5 @@ export class LogEventLogger implements LogEventListener {
7282
}
7383

7484
function formatTimestamp(timestamp: Date): string {
75-
return `${timestamp.getHours()}:${timestamp.getMinutes()}:${timestamp.getSeconds()}`;
85+
return `${timestamp.getHours()}:${timestamp.getMinutes()}:${timestamp.getSeconds()}.${timestamp.getMilliseconds()}`;
7686
}

test/lib/actions/ConfigAction.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,9 @@ describe('ConfigAction tests', () => {
625625

626626
// ==== OUTPUT PROCESSING ====
627627
const displayEvents = spyDisplay.getDisplayEvents();
628-
if (displayEvents[4].type === DisplayEventType.LOG) {
629-
return ansis.strip(displayEvents[4].data);
630-
} else if (displayEvents[5].type === DisplayEventType.LOG) {
631-
return ansis.strip(displayEvents[5].data);
628+
const displayedConfigEventArray = displayEvents.filter(e => e.data.includes("CODE ANALYZER CONFIGURATION"));
629+
if (displayedConfigEventArray.length === 1) {
630+
return ansis.strip(displayedConfigEventArray[0].data);
632631
} else {
633632
return 'Could Not Get Specific Output';
634633
}

test/lib/listeners/LogEventListener.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ describe('LogEventListener implementations', () => {
5151

5252
// ==== ASSERTIONS ====
5353
const displayEvents = spyDisplay.getDisplayEvents();
54-
expect(displayEvents).toHaveLength(3);
54+
expect(displayEvents).toHaveLength(6);
5555
for (let i = 0; i < 3; i++) {
56-
expect(displayEvents[i].type).toEqual(displayEvent);
57-
expect(displayEvents[i].data).toContain(expectedMessages[i]);
56+
expect(displayEvents[i*2].type).toEqual(displayEvent);
57+
expect(displayEvents[i*2].data).toContain(expectedMessages[i]);
58+
expect(displayEvents[i*2+1]).toEqual({type: DisplayEventType.INFO, data: ""}); // We display an empty line after each message
5859
}
5960
});
6061

0 commit comments

Comments
 (0)