From 777ea1cbdcec06ace0186172576b145aa364bb0c Mon Sep 17 00:00:00 2001 From: Ovidiu Viorel Iepure Date: Mon, 5 Sep 2016 06:38:53 -0700 Subject: [PATCH] '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 --- react-packager/src/Activity/Types.js | 3 ++- .../src/Activity/__tests__/Activity-test.js | 27 +++++++++++++++++-- react-packager/src/Activity/index.js | 24 ++++++++++------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/react-packager/src/Activity/Types.js b/react-packager/src/Activity/Types.js index 934400b5..47332bf1 100644 --- a/react-packager/src/Activity/Types.js +++ b/react-packager/src/Activity/Types.js @@ -12,7 +12,8 @@ 'use strict'; export type EventOptions = { - telemetric: boolean, + telemetric?: boolean, + silent?: boolean, }; export type Event = { diff --git a/react-packager/src/Activity/__tests__/Activity-test.js b/react-packager/src/Activity/__tests__/Activity-test.js index 0a513acc..5edadd98 100644 --- a/react-packager/src/Activity/__tests__/Activity-test.js +++ b/react-packager/src/Activity/__tests__/Activity-test.js @@ -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(); }); diff --git a/react-packager/src/Activity/index.js b/react-packager/src/Activity/index.js index a3e512ff..2339090f 100644 --- a/react-packager/src/Activity/index.js +++ b/react-packager/src/Activity/index.js @@ -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}] ${name}${dataString}`)); + if (!silent) { + // eslint-disable-next-line no-console-disallow + console.log(chalk.dim(`[${logTimeStamp}] ${name}${dataString}`)); + } break; case 'endEvent': - // eslint-disable-next-line no-console-disallow - console.log( - chalk.dim(`[${logTimeStamp}] ${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}] ${name}${dataString} `) + + (telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`)) + ); + } forgetEvent(id); break;