Skip to content

Commit

Permalink
feat(core): support console annotations (#124)
Browse files Browse the repository at this point in the history
Summary:
When dev set the following config, we want stack traces to be printed even if `lowLevel` output is muted (stack traces is output at `lowLevel` for various reasons such as coloring, but semantically it is part of the error output).
```
memlabConfig.muteConfig = {
      muteError: false,
      muteWarning: false,
      muteInfo: true,
      muteSuccess: true,
      muteLog: true,
      muteTable: true,
      muteTrace: true,
      muteTopLevel: true,
      muteHighLevel: true,
      muteMidLevel: true,
      muteLowLevel: true,
};
memlabConfig.verbose = true;
```

This diff patches the MemLab console output module so that stack traces is still printed when `muteLowLevel` is `true` while `muteError` is `false`.

Differential Revision: D63012111

fbshipit-source-id: 535bde1a3e7287a2d5b33c75f48b04f708a44be8
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Sep 19, 2024
1 parent 91f182b commit b52e7dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
20 changes: 17 additions & 3 deletions packages/core/src/lib/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import path from 'path';
import readline from 'readline';
import stringWidth from 'string-width';
import {OutputFormat, type MemLabConfig} from './Config';
import {AnyValue, Nullable, Optional} from './Types';
import {
AnyValue,
ConsoleOutputAnnotation,
ConsoleOutputOptions,
Nullable,
Optional,
} from './Types';

type Message = {
lines: number[];
Expand Down Expand Up @@ -117,6 +123,9 @@ class MemLabConsole {
};

private static singleton: MemLabConsole;
public annotations: {[key: string]: ConsoleOutputAnnotation} = {
STACK_TRACE: 'stack-trace',
};

protected constructor() {
this.sections = {
Expand Down Expand Up @@ -442,13 +451,18 @@ class MemLabConsole {
this.printStr(this.style(msg, 'mid'));
}

public lowLevel(msg: string): void {
public lowLevel(msg: string, options: ConsoleOutputOptions = {}): void {
if (this.shouldBeConcise('lowLevel')) {
return this.overwrite(msg);
}
this.logMsg(msg);
if (this.config.muteConfig?.muteLowLevel) {
return;
if (
options.annotation !== this.annotations.STACK_TRACE ||
this.config.muteConfig?.muteError
) {
return;
}
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'low'));
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/lib/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2446,3 +2446,11 @@ export type JSONifyOptions = {
forceJSONifyDepth?: number;
serializationHelper?: ISerializationHelper;
};

/** @internal */
export type ConsoleOutputAnnotation = 'stack-trace';

/** @internal */
export type ConsoleOutputOptions = {
annotation?: ConsoleOutputAnnotation;
};
12 changes: 9 additions & 3 deletions packages/core/src/lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,9 @@ function callAsync(f: AnyAyncFunction): void {
promise.catch((e: unknown) => {
const parsedError = getError(e);
info.error(parsedError.message);
info.lowLevel(parsedError.stack ?? '');
info.lowLevel(parsedError.stack ?? '', {
annotation: info.annotations.STACK_TRACE,
});
});
}
}
Expand Down Expand Up @@ -1892,7 +1894,9 @@ function haltOrThrow(
}
// only print stack trace in verbose mode
if (config.verbose) {
info.lowLevel(err.stack ?? '');
info.lowLevel(err.stack ?? '', {
annotation: info.annotations.STACK_TRACE,
});
} else {
info.topLevel(
'Use `memlab help` or `memlab <COMMAND> -h` to get helper text',
Expand Down Expand Up @@ -2036,7 +2040,9 @@ export function runShell(
if (config.verbose || config.isContinuousTest) {
if (ex instanceof Error) {
info.lowLevel(ex.message);
info.lowLevel(ex.stack ?? '');
info.lowLevel(ex.stack ?? '', {
annotation: info.annotations.STACK_TRACE,
});
}
}
if (options.throwError) {
Expand Down

0 comments on commit b52e7dc

Please sign in to comment.