From 3ad529d937a4aa5af4e68575c6780676edd23248 Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Fri, 24 Aug 2018 15:39:25 -0700 Subject: [PATCH] misc(logger): add time/timeEnd methods (#5905) --- lighthouse-logger/index.js | 17 +++++++++++++++++ lighthouse-logger/package.json | 5 +++-- lighthouse-logger/yarn.lock | 4 ++++ typings/lighthouse-logger/index.d.ts | 9 +++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lighthouse-logger/index.js b/lighthouse-logger/index.js index 39cc6eae1f9f..6ccf1190f5ac 100644 --- a/lighthouse-logger/index.js +++ b/lighthouse-logger/index.js @@ -6,6 +6,8 @@ 'use strict'; const debug = require('debug'); +const marky = require('marky'); + const EventEmitter = require('events').EventEmitter; const isWindows = process.platform === 'win32'; @@ -104,6 +106,16 @@ class Log { Log._logToStdErr(`${prefix}:${level || ''}`, [method, snippet]); } + static time({msg, id, args=[]}, level='log') { + marky.mark(id); + Log[level]('status', msg, ...args); + } + + static timeEnd({msg, id, args=[]}, level='verbose') { + Log[level]('statusEnd', msg, ...args); + marky.stop(id); + } + static log(title, ...args) { Log.events.issueStatus(title, args); return Log._logToStdErr(title, args); @@ -207,5 +219,10 @@ class Log { } Log.events = new Emitter(); +Log.takeTimeEntries = _ => { + const entries = marky.getEntries(); + marky.clear(); + return entries; +}; module.exports = Log; diff --git a/lighthouse-logger/package.json b/lighthouse-logger/package.json index 0491cac2910c..cbe4a8275884 100644 --- a/lighthouse-logger/package.json +++ b/lighthouse-logger/package.json @@ -1,8 +1,9 @@ { "name": "lighthouse-logger", - "version": "1.0.1", + "version": "1.1.0", "license": "Apache-2.0", "dependencies": { - "debug": "^2.6.8" + "debug": "^2.6.8", + "marky": "^1.2.0" } } diff --git a/lighthouse-logger/yarn.lock b/lighthouse-logger/yarn.lock index 3e130cd61404..5cef70256d6c 100644 --- a/lighthouse-logger/yarn.lock +++ b/lighthouse-logger/yarn.lock @@ -8,6 +8,10 @@ debug@^2.6.8: dependencies: ms "2.0.0" +marky@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/marky/-/marky-1.2.0.tgz#9617ed647bbbea8f45d19526da33dec70606df42" + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" diff --git a/typings/lighthouse-logger/index.d.ts b/typings/lighthouse-logger/index.d.ts index 3e862b762c42..2863cda5ba40 100644 --- a/typings/lighthouse-logger/index.d.ts +++ b/typings/lighthouse-logger/index.d.ts @@ -5,12 +5,21 @@ */ declare module 'lighthouse-logger' { + interface Status { + msg: string; + id: string; + args?: any[]; + } export function setLevel(level: string): void; export function formatProtocol(prefix: string, data: Object, level?: string): void; export function log(title: string, ...args: any[]): void; export function warn(title: string, ...args: any[]): void; export function error(title: string, ...args: any[]): void; export function verbose(title: string, ...args: any[]): void; + export function time(status: Status, level?: string): void; + export function timeEnd(status: Status, level?: string): void; export function reset(): string; + /** Retrieves and clears all stored time entries */ + export function takeTimeEntries(): PerformanceEntry[]; export var events: import('events').EventEmitter; }