Skip to content

Commit

Permalink
feat(core): support for console mute configuration (#124)
Browse files Browse the repository at this point in the history
Summary:
OSS feature request: #124

By setting `config.muteConfig`, JS code using MemLab API can set the mute config for different types of console messages (e.g., warning, error etc.)

```
type MuteConfig = {
  muteError?: boolean;
  muteWarning?: boolean;
  muteInfo?: boolean;
  muteSuccess?: boolean;
  muteLog?: boolean;
  muteTable?: boolean;
  muteTrace?: boolean;
  muteTopLevel?: boolean;
  muteHighLevel?: boolean;
  muteMidLevel?: boolean;
  muteLowLevel?: boolean;
};
```

Reviewed By: twobassdrum

Differential Revision: D61287025

fbshipit-source-id: cb0d8b3a06d3f80d61ad107d0d41a633d8e69307
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Aug 15, 2024
1 parent f2af467 commit 7f6fcee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
16 changes: 16 additions & 0 deletions packages/core/src/lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ export enum ErrorHandling {
Throw = 2,
}

/** @internal */
export type MuteConfig = {
muteError?: boolean;
muteWarning?: boolean;
muteInfo?: boolean;
muteSuccess?: boolean;
muteLog?: boolean;
muteTable?: boolean;
muteTrace?: boolean;
muteTopLevel?: boolean;
muteHighLevel?: boolean;
muteMidLevel?: boolean;
muteLowLevel?: boolean;
};

/** @internal */
export class MemLabConfig {
_reportLeaksInTimers: boolean;
Expand Down Expand Up @@ -220,6 +235,7 @@ export class MemLabConfig {
externalLeakFilter?: Optional<ILeakFilter>;
monoRepoDir: string;
muteConsole: boolean;
muteConfig?: MuteConfig;
includeObjectInfoInTraceReturnChain: boolean;
logUnclassifiedClusters: boolean;
errorHandling: ErrorHandling;
Expand Down
44 changes: 41 additions & 3 deletions packages/core/src/lib/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ class MemLabConsole {
const lines = msg.split('\n').map(line => stringWidth(line));
const section = this.getLastSection();
section?.msgs.push({lines, options});
this.logMsg(msg);
}

private clearPrevMsgInLastSection(): void {
Expand Down Expand Up @@ -356,7 +355,8 @@ class MemLabConsole {
if (
this.shouldBeConcise('table') ||
this.config.isTest ||
this.config.muteConsole
this.config.muteConsole ||
this.config.muteConfig?.muteTable
) {
return;
}
Expand All @@ -370,7 +370,11 @@ class MemLabConsole {
}

public trace(): void {
if (this.config.isTest || this.config.muteConsole) {
if (
this.config.isTest ||
this.config.muteConsole ||
this.config.muteConfig?.muteTrace
) {
return;
}
console.trace();
Expand All @@ -380,6 +384,10 @@ class MemLabConsole {
if (this.shouldBeConcise('topLevel')) {
return this.overwrite(msg);
}
this.logMsg(msg);
if (this.config.muteConfig?.muteTopLevel) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'top'));
}
Expand All @@ -388,6 +396,10 @@ class MemLabConsole {
if (this.shouldBeConcise('highLevel')) {
return this.overwrite(msg);
}
this.logMsg(msg);
if (this.config.muteConfig?.muteHighLevel) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'high'));
}
Expand All @@ -396,6 +408,10 @@ class MemLabConsole {
if (this.shouldBeConcise('midLevel')) {
return this.overwrite(msg);
}
this.logMsg(msg);
if (this.config.muteConfig?.muteMidLevel) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'mid'));
}
Expand All @@ -404,6 +420,10 @@ class MemLabConsole {
if (this.shouldBeConcise('lowLevel')) {
return this.overwrite(msg);
}
this.logMsg(msg);
if (this.config.muteConfig?.muteLowLevel) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'low'));
}
Expand All @@ -412,6 +432,10 @@ class MemLabConsole {
if (this.shouldBeConcise('success')) {
return this.overwrite(msg);
}
this.logMsg(msg);
if (this.config.muteConfig?.muteSuccess) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'success'));
}
Expand All @@ -424,11 +448,19 @@ class MemLabConsole {
}

public error(msg: string): void {
this.logMsg(msg);
if (this.config.muteConfig?.muteError) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'error'));
}

public warning(msg: string): void {
this.logMsg(msg);
if (this.config.muteConfig?.muteWarning) {
return;
}
this.clearPrevOverwriteMsg();
this.printStr(this.style(msg, 'warning'));
}
Expand All @@ -437,6 +469,7 @@ class MemLabConsole {
if (this.shouldBeConcise('nextLine')) {
return this.overwrite('');
}
this.logMsg('');
this.clearPrevOverwriteMsg();
this.printStr('');
}
Expand All @@ -445,6 +478,10 @@ class MemLabConsole {
msg: string,
options: {level?: keyof MemlabConsoleStyles} = {},
): void {
this.logMsg(msg);
if (this.config.muteConfig?.muteLowLevel) {
return;
}
const str = this.style(msg, options.level || 'low');
if (this.config.isContinuousTest) {
this.printStr(msg, {isOverwrite: false});
Expand All @@ -464,6 +501,7 @@ class MemLabConsole {
output: process.stdout,
});
this.pushMsg(query);
this.logMsg(query);
return new Promise(resolve =>
rl.question(query, ans => {
rl.close();
Expand Down

0 comments on commit 7f6fcee

Please sign in to comment.