diff --git a/doc/TOOL_API.md b/doc/TOOL_API.md index 78657e1..259f8e9 100644 --- a/doc/TOOL_API.md +++ b/doc/TOOL_API.md @@ -26,7 +26,7 @@ let console = eruda.get('console'); console.config.set('catchGlobalErr', true); ``` -### log, error, info, warn, dir, time/timeEnd, clear, count, assert, table, group/groupCollapsed/groupEnd +### log, error, info, warn, dir, time/timeLog/timeEnd, clear, count/countReset, assert, table, group/groupCollapsed/groupEnd All these methods can be used in the same way as window.console object. diff --git a/src/Console/Console.js b/src/Console/Console.js index f8ed690..a26570c 100644 --- a/src/Console/Console.js +++ b/src/Console/Console.js @@ -313,6 +313,7 @@ const CONSOLE_METHOD = [ 'warn', 'dir', 'time', + 'timeLog', 'timeEnd', 'clear', 'table', diff --git a/src/Console/Logger.js b/src/Console/Logger.js index c49b15c..84a0700 100644 --- a/src/Console/Logger.js +++ b/src/Console/Logger.js @@ -112,18 +112,29 @@ export default class Logger extends Emitter { return this.insert('table', args) } time(name = 'default') { + if (this._timer[name]) { + return this.insert('warn', [`Timer '${name}' already exists`]) + } this._timer[name] = perfNow() return this } - timeEnd(name = 'default') { + timeLog(name = 'default') { const startTime = this._timer[name] - if (!startTime) return - delete this._timer[name] + if (!startTime) { + return this.insert('warn', [`Timer '${name}' does not exist`]) + } return this.info(`${name}: ${perfNow() - startTime}ms`) } + timeEnd(name = 'default') { + this.timeLog(name) + + delete this._timer[name] + + return this + } clear() { this.silentClear() @@ -135,6 +146,8 @@ export default class Logger extends Emitter { silentClear() { this._logs = [] this._lastLog = {} + this._count = {} + this._timer = {} this._groupStack = new Stack() return this.render() diff --git a/test/console.js b/test/console.js index 806fd82..fefcf25 100644 --- a/test/console.js +++ b/test/console.js @@ -59,9 +59,9 @@ describe('console', function() { }) it('count', function() { - tool.count('test').clear() tool.count('test') - expect($tool.find('.eruda-info')).toContainText('test: 2') + tool.count('test') + expect($tool.find('.eruda-info').eq(1)).toContainText('test: 2') }) describe('substitution', function() {