-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
200 lines (156 loc) · 4.91 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* globals log, __command, NSThread */
var util = require("util")
var prepareValue = require("./prepare-value")
var indentLevel = 0
function indentString(string) {
var indent = ""
for (var i = 0; i < indentLevel; i += 1) {
indent += " "
}
if (indentLevel > 0) {
indent += "| "
}
return string.replace(/\n/g, indent + "\n")
}
function logEverywhere(level, payload, skipFormat) {
var stringValue = skipFormat ? payload : util.format.apply(this, payload)
log({
stringValue: indentString(stringValue),
payload: [prepareValue(payload)],
level: level,
command: __command
})
}
if (!console._sketch) {
var oldAssert = console.assert
console.assert = function assert(condition, text) {
// log to the JS context
if (oldAssert) oldAssert.apply(this, arguments)
if (!condition) {
return logEverywhere("assert", [text])
}
return undefined
}
var oldClear = console.clear
console.clear = function clear() {
if (oldClear) oldClear.apply(this, arguments)
var threadDictionary = NSThread.mainThread().threadDictionary()
var panel = threadDictionary["skpm.debugger"]
if (!panel) {
return
}
var webview = panel.contentView().subviews()[0]
if (!webview || !webview.evaluateJavaScript_completionHandler) {
return
}
webview.evaluateJavaScript_completionHandler(
'sketchBridge({"name":"logs/CLEAR_LOGS"});',
null
)
}
var counts = {}
var oldCount = console.count
console.count = function count(label) {
// log to the JS context
if (oldCount) oldCount.apply(this, arguments)
label = typeof label !== "undefined" ? label : "default"
counts[label] = (counts[label] || 0) + 1
return logEverywhere("log", label + ": " + counts[label], true)
}
var oldCountReset = console.countReset
console.countReset = function countReset(label) {
// log to the JS context
if (oldCountReset) oldCountReset.apply(this, arguments)
label = typeof label !== "undefined" ? label : "default"
counts[label] = 0
}
console.debug = console.log
var oldDir = console.dir
console.dir = function dir(obj, options) {
// log to the JS context
if (oldDir) oldDir.apply(this, arguments)
options = options || {}
options.customInspect = false
return logEverywhere("log", util.inspect(obj, options), true)
}
var oldDirxml = console.dirxml
console.dirxml = function dirxml() {
// log to the JS context
if (oldDirxml) oldDirxml.apply(this, arguments)
return logEverywhere("log", Array.from(arguments))
}
var oldError = console.error
console.error = function error() {
// log to the JS context
if (oldError) oldError.apply(this, arguments)
return logEverywhere("error", Array.from(arguments))
}
var oldGroup = console.group
console.group = function group() {
// log to the JS context
if (oldGroup) oldGroup.apply(this, arguments)
if (arguments.length) {
Array.from(arguments).forEach(function logItems(label) {
logEverywhere("log", [label])
})
}
indentLevel += 1
}
console.groupCollapsed = console.group
var oldGroupEnd = console.groupEnd
console.groupEnd = function groupEnd() {
// log to the JS context
if (oldGroupEnd) oldGroupEnd.apply(this, arguments)
indentLevel -= 1
if (indentLevel < 0) {
indentLevel = 0
}
}
var oldInfo = console.info
console.info = function info() {
// log to the JS context
if (oldInfo) oldInfo.apply(this, arguments)
return logEverywhere("info", Array.from(arguments))
}
var oldLog = console.log
console.log = function log() {
// log to the JS context
if (oldLog) oldLog.apply(this, arguments)
return logEverywhere("log", Array.from(arguments))
}
var timers = {}
var oldTime = console.time
console.time = function time(label) {
// log to the JS context
if (oldTime) oldTime.apply(this, arguments)
label = typeof label !== "undefined" ? label : "default"
if (timers[label]) {
return logEverywhere("warn", 'Timer "' + label + '" already exists', true)
}
timers[label] = Date.now()
return undefined
}
var oldTimeEnd = console.timeEnd
console.timeEnd = function timeEnd(label) {
// log to the JS context
if (oldTimeEnd) oldTimeEnd.apply(this, arguments)
label = typeof label !== "undefined" ? label : "default"
if (!timers[label]) {
return logEverywhere("warn", 'Timer "' + label + '" does not exist', true)
}
var duration = Date.now() - timers[label]
delete timers[label]
return logEverywhere("log", [label + ": " + duration + "ms"])
}
// console.trace = function() {}
var oldWarn = console.warn
console.warn = function warm() {
// log to the JS context
if (oldWarn) oldWarn.apply(this, arguments)
return logEverywhere("warn", Array.from(arguments))
}
console._sketch = true
}
module.exports = function createConsole() {
return console
}