forked from liamnewmarch/console-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console-shim.coffee
45 lines (31 loc) · 1.08 KB
/
console-shim.coffee
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
### https://github.com/liamnewmarch/console-shim 2014 CC-BY @liamnewmarch ###
# If the console exists, don't shim it
unless 'console' of window then do ->
Console = ->
# __buffer holds log messages
@__buffer = []
# log() adds messages to __buffer
log = -> @__buffer.push arguments
# each `console.XYZ` that we want to map
methods = 'assert count debug dir dirxml error exception info log trace warn'
# add public methods to console for log, error, warn and info
@[key] = log for key in methods.split()
# Check every second to check if the real console exists
check = setInterval ->
# console.__buffer won't exist on the real console
if window.console?.log? and not console.__buffer
# Stop checking
clearInterval check
# Get the real console.log
func = if Function::bind
Function::bind.call console.log, console
else
console.log
# Output everything in __buffer to the console
for data in __console.__buffer
func.apply console, data
return
, 1000
return
# Expose
__console = window.console = new Console()