forked from bayleeadamoss/zazu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.js
112 lines (99 loc) · 2.92 KB
/
debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const React = require('react')
const globalEmitter = require('../lib/globalEmitter')
class Debug extends React.Component {
constructor (props) {
super(props)
this.state = {
selectedPlugin: 'Any',
selectedLevel: 'info',
logTypes: ['verbose', 'info', 'warn', 'error'],
plugins: [],
items: [],
}
}
log = (options) => {
const items = Object.assign([], this.state.items)
const plugins = Object.assign([], this.state.plugins)
items.unshift(Object.assign({}, options, {
time: new Date(),
}))
if (plugins.indexOf(options.plugin) === -1) {
plugins.push(options.plugin)
}
this.setState({
plugins,
items,
})
}
componentDidMount () {
globalEmitter.on('pluginLog', (options) => {
this.log(options)
})
}
componentWillUnmount = () => {
globalEmitter.removeAllListeners('pluginLog')
}
handleTypeChange = (e) => {
this.setState({
selectedLevel: e.target.value,
})
}
handlePluginChange = (e) => {
this.setState({
selectedPlugin: e.target.value,
})
}
allowedPlugins = () => {
if (this.state.selectedPlugin === 'Any') return this.state.plugins
else return [this.state.selectedPlugin]
}
allowedLevels = () => {
if (this.state.selectedLevel === 'error') {
return ['error']
} else if (this.state.selectedLevel === 'warn') {
return ['error', 'warn']
} else if (this.state.selectedLevel === 'info') {
return ['error', 'warn', 'info']
} else if (this.state.selectedLevel === 'verbose') {
return ['error', 'warn', 'info', 'verbose']
}
}
render () {
const allowedPlugins = this.allowedPlugins()
const allowedLevels = this.allowedLevels()
return (
<ul>
<select defaultValue='Any' onChange={this.handlePluginChange}>
{['Any'].concat(this.state.plugins).map((plugin) => {
return (
<option key={plugin}>{plugin}</option>
)
})}
</select>
<select defaultValue={this.state.selectedLevel} onChange={this.handleTypeChange}>
{this.state.logTypes.map((logType) => {
return (
<option key={logType}>{logType}</option>
)
})}
</select>
<ul>
{this.state.items.filter((item) => {
return allowedPlugins.indexOf(item.plugin) !== -1 && allowedLevels.indexOf(item.level) !== -1
}).slice(0, 100).map((item, key) => {
return (
<li key={key}>
<pre className={item.level}>
{item.level.toUpperCase()}: [{item.time.toTimeString().split(' ')[0]}] [{item.plugin} {item.block ? ':' + item.block : ''}]
</pre>
<pre>{item.message}</pre>
<pre>{JSON.stringify(item, null, 2)}</pre>
</li>
)
})}
</ul>
</ul>
)
}
}
module.exports = Debug