Skip to content

Commit

Permalink
misc(logger): add time/timeEnd methods (#5905)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Aug 24, 2018
1 parent d891fbe commit 3ad529d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lighthouse-logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
'use strict';

const debug = require('debug');
const marky = require('marky');

const EventEmitter = require('events').EventEmitter;
const isWindows = process.platform === 'win32';

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -207,5 +219,10 @@ class Log {
}

Log.events = new Emitter();
Log.takeTimeEntries = _ => {
const entries = marky.getEntries();
marky.clear();
return entries;
};

module.exports = Log;
5 changes: 3 additions & 2 deletions lighthouse-logger/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 4 additions & 0 deletions lighthouse-logger/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions typings/lighthouse-logger/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 3ad529d

Please sign in to comment.