-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghoulog.js
161 lines (137 loc) · 4.4 KB
/
ghoulog.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Mirror console logs into a small div at top-left corner,
// styled in post-apocalyptic way ;)
//
// Usage is simple, just:
// <script src='ghoulog.js'></script>
//
// Note:
// You must set body { position: relative; } to let log div to be
// absolutely positioned.
//
// Based on:
// https://github.com/bahmutov/console-log-div
//
(function initGhoulog () {
'use strict'
if (console.log.toDiv) {
return
}
function toString (x) {
return typeof x === 'string' ? x : JSON.stringify(x)
}
var log = console.log.bind(console)
var error = console.error.bind(console)
var warn = console.warn.bind(console)
var table = console.table ? console.table.bind(console) : null
var id = 'logdiv'
var rownum = 0
// Turn false at the first call. Used to detect the first printToDiv
// call and reveal the log. Empty log looks and acts weird.
var firstPrintToDivFlag = true
var outerElement = (function createOuterElement () {
var outer = document.getElementById(id)
if (!outer) {
outer = document.createElement('fieldset')
outer.id = id
document.body.appendChild(outer)
}
outer.classList.add(id)
var style = outer.style
style.width = '100px'
style.height = '100px'
style.fontFamily = 'monospace'
style.whiteSpace = 'pre'
style.padding = '10px 10px'
style.position = 'absolute'
style.left = '0px'
style.bottom = '0px'
style.color = '#1AFF80'
style.border = '1px dotted #1AFF80'
style.overflow = 'hidden'
style.fontSize = '8px'
// Hide until the first log entry
style.display = 'none'
return outer
}())
var logTo = (function createLogDiv () {
var div = document.createElement('div')
div.id = 'console-log-text'
outerElement.appendChild(div)
return div
}())
function printToDiv () {
if (firstPrintToDivFlag) {
firstPrintToDivFlag = false
outerElement.style.display = 'block'
}
var msg = Array.prototype.slice.call(arguments, 0)
.map(toString)
.join(' ')
rownum += 1
var text = logTo.textContent
logTo.textContent = rownum + ': ' + msg + '\n' + text
}
function logWithCopy () {
log.apply(null, arguments)
printToDiv.apply(null, arguments)
}
console.log = logWithCopy
console.log.toDiv = true
console.error = function errorWithCopy () {
error.apply(null, arguments)
var args = Array.prototype.slice.call(arguments, 0)
args.unshift('ERROR:')
printToDiv.apply(null, args)
}
console.warn = function logWarning () {
warn.apply(null, arguments)
var args = Array.prototype.slice.call(arguments, 0)
args.unshift('WARNING:')
printToDiv.apply(null, args)
}
function printTable (objArr, keys) {
var numCols = keys.length
var len = objArr.length
var $table = document.createElement('table')
$table.style.width = '100%'
$table.setAttribute('border', '1')
var $head = document.createElement('thead')
var $tdata = document.createElement('td')
$tdata.innerHTML = 'Index'
$head.appendChild($tdata)
for (var k = 0; k < numCols; k++) {
$tdata = document.createElement('td')
$tdata.innerHTML = keys[k]
$head.appendChild($tdata)
}
$table.appendChild($head)
for (var i = 0; i < len; i++) {
var $line = document.createElement('tr')
$tdata = document.createElement('td')
$tdata.innerHTML = i
$line.appendChild($tdata)
for (var j = 0; j < numCols; j++) {
$tdata = document.createElement('td')
$tdata.innerHTML = objArr[i][keys[j]]
$line.appendChild($tdata)
}
$table.appendChild($line)
}
var div = document.getElementById('console-log-text')
div.appendChild($table)
}
console.table = function logTable () {
if (typeof table === 'function') {
table.apply(null, arguments)
}
var objArr = arguments[0]
var keys
if (typeof objArr[0] !== 'undefined') {
keys = Object.keys(objArr[0])
}
printTable(objArr, keys)
}
window.addEventListener('error', function (err) {
printToDiv('EXCEPTION:', err.message + '\n ' + err.filename, err.lineno + ':' + err.colno)
})
}())