'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:
Ovidiu Viorel Iepure 2016-09-05 06:38:53 -07:00 committed by Facebook Github Bot 3
parent 9889e7d5ca
commit 777ea1cbdc
3 changed files with 41 additions and 13 deletions

View File

@ -12,7 +12,8 @@
'use strict';
export type EventOptions = {
telemetric: boolean,
telemetric?: boolean,
silent?: boolean,
};
export type Event = {

View File

@ -28,7 +28,7 @@ describe('Activity', () => {
});
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 DATA = {someData: 42};
@ -43,10 +43,21 @@ describe('Activity', () => {
expect(consoleMsg).toContain(EVENT_NAME);
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', () => {
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 DATA = {someData: 42};
@ -63,6 +74,18 @@ describe('Activity', () => {
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', () => {
expect(() => Activity.endEvent(42)).toThrow();
});

View File

@ -26,7 +26,7 @@ const EVENT_EMITTER = new events.EventEmitter();
function startEvent(
name: string,
data: any = null,
options?: EventOptions = {telemetric: false},
options?: EventOptions = {},
): number {
if (name == null) {
throw new Error('No event name specified!');
@ -64,7 +64,7 @@ function forgetEvent(id: number): void {
}
function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
const event = EVENT_INDEX[id];
const event = getEvent(id);
EVENT_EMITTER.emit(phase, id);
if (!ENABLED) {
@ -80,20 +80,24 @@ function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
const logTimeStamp = new Date().toLocaleString();
const dataString = data ? ': ' + JSON.stringify(data) : '';
const {telemetric} = options;
const {telemetric, silent} = options;
switch (phase) {
case 'startEvent':
// eslint-disable-next-line no-console-disallow
console.log(chalk.dim(`[${logTimeStamp}] <START> ${name}${dataString}`));
if (!silent) {
// eslint-disable-next-line no-console-disallow
console.log(chalk.dim(`[${logTimeStamp}] <START> ${name}${dataString}`));
}
break;
case 'endEvent':
// eslint-disable-next-line no-console-disallow
console.log(
chalk.dim(`[${logTimeStamp}] <END> ${name}${dataString} `) +
(telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`))
);
if (!silent) {
// eslint-disable-next-line no-console-disallow
console.log(
chalk.dim(`[${logTimeStamp}] <END> ${name}${dataString} `) +
(telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`))
);
}
forgetEvent(id);
break;