Skip to content

Commit

Permalink
fix(log): extra info from
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Apr 17, 2020
1 parent 729a38f commit cba5652
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Console/Log.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{#if displayHeader}}
{{#if headers}}
<div {{{class 'header'}}}>
{{#repeat group.indentLevel}}
<div {{{class 'nesting-level'}}}></div>
{{/repeat}}
<div {{{class 'time-container'}}}>
<span>{{time}}</span> <span>{{from}}</span>
<span>{{headers.time}}</span> <span>{{headers.from}}</span>
</div>
</div>
{{/if}}
Expand Down
32 changes: 4 additions & 28 deletions src/Console/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
isPrimitive,
wrap,
defaults,
dateFormat,
getObjType,
isEl,
toStr,
Expand Down Expand Up @@ -48,7 +47,7 @@ export default class Log extends Emitter {
id,
group = {},
targetGroup = {},
displayHeader = false,
headers,
ignoreFilter = false
}) {
super()
Expand All @@ -59,7 +58,7 @@ export default class Log extends Emitter {
this.args = args
this.count = 1
this.id = id
this.displayHeader = displayHeader
this.headers = headers
this.ignoreFilter = ignoreFilter
this.collapsed = false
this.el = document.createElement('li')
Expand All @@ -68,11 +67,6 @@ export default class Log extends Emitter {
this.width = 0
this._$el = $(this.el)

if (displayHeader) {
this.time = getCurTime()
this.from = getFrom()
}

this._formatMsg()

if (this.group) {
Expand Down Expand Up @@ -242,7 +236,7 @@ export default class Log extends Emitter {
}
_formatMsg() {
let { args } = this
const { type, id, displayHeader, time, from, group } = this
const { type, id, headers, group } = this

// Don't change original args for lazy evaluation.
args = clone(args)
Expand Down Expand Up @@ -320,7 +314,7 @@ export default class Log extends Emitter {
return `<a href="${url}" target="_blank">${url}</a>`
})
}
msg = render({ msg, type, icon, id, displayHeader, time, from, group })
msg = render({ msg, type, icon, id, headers, group })

this._$el.addClass('eruda-log-container').html(msg)
this._$content = this._$el.find('.eruda-log-content')
Expand Down Expand Up @@ -562,24 +556,6 @@ function formatEl(val) {
)}</pre>`
}

function getFrom() {
const e = new Error()
let ret = ''
const lines = e.stack ? e.stack.split('\n') : ''

for (let i = 0, len = lines.length; i < len; i++) {
ret = lines[i]
if (ret.indexOf('winConsole') > -1 && i < len - 1) {
ret = lines[i + 1]
break
}
}

return ret
}

const getCurTime = () => dateFormat('HH:MM:ss')

const tpl = require('./Log.hbs')
const render = data => tpl(data)

Expand Down
45 changes: 36 additions & 9 deletions src/Console/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
raf,
xpath,
isHidden,
lowerCase
lowerCase,
dateFormat
} from '../lib/util'
import evalCss from '../lib/evalCss'

Expand Down Expand Up @@ -300,16 +301,24 @@ export default class Logger extends Emitter {
return this
}
insert(type, args) {
let headers
if (this._displayHeader) {
headers = {
time: getCurTime(),
from: getFrom()
}
}

this._asyncRender
? this.insertAsync(type, args)
: this.insertSync(type, args)
? this.insertAsync(type, args, headers)
: this.insertSync(type, args, headers)
}
insertAsync(type, args) {
this._asyncList.push([type, args])
insertAsync(type, args, headers) {
this._asyncList.push([type, args, headers])

this._handleAsyncList()
}
insertSync(type, args) {
insertSync(type, args, headers) {
const logs = this._logs
const groupStack = this._groupStack

Expand All @@ -327,7 +336,7 @@ export default class Logger extends Emitter {
}
extend(options, {
id: ++id,
displayHeader: this._displayHeader
headers
})

if (options.type === 'group' || options.type === 'groupCollapsed') {
Expand Down Expand Up @@ -494,8 +503,8 @@ export default class Logger extends Emitter {
done = true
}
for (let i = 0; i < num; i++) {
const [type, args] = asyncList.shift()
this.insertSync(type, args)
const [type, args, headers] = asyncList.shift()
this.insertSync(type, args, headers)
}
if (!done) raf(() => this._handleAsyncList(timeout))
}, timeout)
Expand Down Expand Up @@ -700,3 +709,21 @@ export default class Logger extends Emitter {
this._ignoreScroll = true
}
}

const getCurTime = () => dateFormat('HH:MM:ss')

function getFrom() {
const e = new Error()
let ret = ''
const lines = e.stack ? e.stack.split('\n') : ''

for (let i = 0, len = lines.length; i < len; i++) {
ret = lines[i]
if (ret.indexOf('winConsole') > -1 && i < len - 1) {
ret = lines[i + 1]
break
}
}

return ret
}

0 comments on commit cba5652

Please sign in to comment.