mirror of https://github.com/status-im/metro.git
'silent' option for Activity events
Summary: Activity events now have a `silent` boolean option that specifies whether or not the event phases are to be logged to the console. Defaults to `false`, preserving current behaviour for events that do not explicitly have this option set. Reviewed By: davidaurelio Differential Revision: D3810802 fbshipit-source-id: 38d14b9e6c6502fbc73eece9466f20b60d19965e
This commit is contained in:
parent
9889e7d5ca
commit
777ea1cbdc
|
@ -12,7 +12,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
export type EventOptions = {
|
export type EventOptions = {
|
||||||
telemetric: boolean,
|
telemetric?: boolean,
|
||||||
|
silent?: boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Event = {
|
export type Event = {
|
||||||
|
|
|
@ -28,7 +28,7 @@ describe('Activity', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('startEvent', () => {
|
describe('startEvent', () => {
|
||||||
it('writes a START event out to the console', () => {
|
it('writes the "START" phase of non-silent events to the console', () => {
|
||||||
const EVENT_NAME = 'EVENT_NAME';
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
const DATA = {someData: 42};
|
const DATA = {someData: 42};
|
||||||
|
|
||||||
|
@ -43,10 +43,21 @@ describe('Activity', () => {
|
||||||
expect(consoleMsg).toContain(EVENT_NAME);
|
expect(consoleMsg).toContain(EVENT_NAME);
|
||||||
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not write the "START" phase of silent events to the console', () => {
|
||||||
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
|
const DATA = {someData: 42};
|
||||||
|
|
||||||
|
Activity.startEvent(EVENT_NAME, DATA, {silent: true});
|
||||||
|
jest.runOnlyPendingTimers();
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-console-disallow
|
||||||
|
expect(console.log.mock.calls.length).toBe(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('endEvent', () => {
|
describe('endEvent', () => {
|
||||||
it('writes an END event out to the console', () => {
|
it('writes the "END" phase of non-silent events to the console', () => {
|
||||||
const EVENT_NAME = 'EVENT_NAME';
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
const DATA = {someData: 42};
|
const DATA = {someData: 42};
|
||||||
|
|
||||||
|
@ -63,6 +74,18 @@ describe('Activity', () => {
|
||||||
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not write the "END" phase of silent events to the console', () => {
|
||||||
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
|
const DATA = {someData: 42};
|
||||||
|
|
||||||
|
const eventID = Activity.startEvent(EVENT_NAME, DATA, {silent: true});
|
||||||
|
Activity.endEvent(eventID);
|
||||||
|
jest.runOnlyPendingTimers();
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-console-disallow
|
||||||
|
expect(console.log.mock.calls.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('throws when called with an invalid eventId', () => {
|
it('throws when called with an invalid eventId', () => {
|
||||||
expect(() => Activity.endEvent(42)).toThrow();
|
expect(() => Activity.endEvent(42)).toThrow();
|
||||||
});
|
});
|
||||||
|
|
|
@ -26,7 +26,7 @@ const EVENT_EMITTER = new events.EventEmitter();
|
||||||
function startEvent(
|
function startEvent(
|
||||||
name: string,
|
name: string,
|
||||||
data: any = null,
|
data: any = null,
|
||||||
options?: EventOptions = {telemetric: false},
|
options?: EventOptions = {},
|
||||||
): number {
|
): number {
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
throw new Error('No event name specified!');
|
throw new Error('No event name specified!');
|
||||||
|
@ -64,7 +64,7 @@ function forgetEvent(id: number): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
|
function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
|
||||||
const event = EVENT_INDEX[id];
|
const event = getEvent(id);
|
||||||
EVENT_EMITTER.emit(phase, id);
|
EVENT_EMITTER.emit(phase, id);
|
||||||
|
|
||||||
if (!ENABLED) {
|
if (!ENABLED) {
|
||||||
|
@ -80,20 +80,24 @@ function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
|
||||||
|
|
||||||
const logTimeStamp = new Date().toLocaleString();
|
const logTimeStamp = new Date().toLocaleString();
|
||||||
const dataString = data ? ': ' + JSON.stringify(data) : '';
|
const dataString = data ? ': ' + JSON.stringify(data) : '';
|
||||||
const {telemetric} = options;
|
const {telemetric, silent} = options;
|
||||||
|
|
||||||
switch (phase) {
|
switch (phase) {
|
||||||
case 'startEvent':
|
case 'startEvent':
|
||||||
// eslint-disable-next-line no-console-disallow
|
if (!silent) {
|
||||||
console.log(chalk.dim(`[${logTimeStamp}] <START> ${name}${dataString}`));
|
// eslint-disable-next-line no-console-disallow
|
||||||
|
console.log(chalk.dim(`[${logTimeStamp}] <START> ${name}${dataString}`));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'endEvent':
|
case 'endEvent':
|
||||||
// eslint-disable-next-line no-console-disallow
|
if (!silent) {
|
||||||
console.log(
|
// eslint-disable-next-line no-console-disallow
|
||||||
chalk.dim(`[${logTimeStamp}] <END> ${name}${dataString} `) +
|
console.log(
|
||||||
(telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`))
|
chalk.dim(`[${logTimeStamp}] <END> ${name}${dataString} `) +
|
||||||
);
|
(telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`))
|
||||||
|
);
|
||||||
|
}
|
||||||
forgetEvent(id);
|
forgetEvent(id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue