-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConsole.coffee
More file actions
162 lines (122 loc) · 3.95 KB
/
Console.coffee
File metadata and controls
162 lines (122 loc) · 3.95 KB
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
define [
'cord!errors'
'underscore'
], (errors, _) ->
###
System console wrapper with nice configurable debugging and logging features
###
# What kind of messages we should log
consoleConfig = global?.config?.console or {}
output =
log: consoleConfig.log or true
warn: consoleConfig.warn or true
error: consoleConfig.error or true
notice: consoleConfig.notice or false
internal: consoleConfig.internal or false
errorTrace: consoleConfig.errorTrace or false
appendConsoleCallTrace: consoleConfig.appendConsoleCallTrace or false
# Enable assertions (for development only)
assertionsEnabled = global?.config?.debug.assertions or false
splitSeparator = /\n/
stringify = (args) ->
args.map (x) ->
if x instanceof Object
try
# TypeError: Converting circular structure to JSON
JSON.stringify(x)
catch
x
else
x
.join(', ')
prepareArgs = (args) ->
if not CORD_IS_BROWSER
host = global?.config?.api.backend.host
args.unshift host if host
args.unshift((new Date).toString())
args
getConsoleCallTraceLine = ->
###
Returns first line without Console.js from the current call stack-trace.
@return {String|undefined}
###
try
throw new Error()
catch e
error = e
lines = error.stack.split(splitSeparator).slice(1)
_.find lines, (x) ->
x.indexOf('/cord/core/Console.js') == -1
_minErrorType = (type1, type2) ->
typeWeight =
error: 5
warn: 4
log: 3
notice: 2
internal: 1
weight1 = typeWeight[type1] or typeWeight['log']
weight2 = typeWeight[type2] or typeWeight['log']
if weight1 > weight2 then type2 else type1
_log: (type, args) ->
if output[type]
@publish 'logger.log.publish',
tags: [type]
params:
message: stringify(args)
console: true
# Add console call trace line
args.push("\n_console.#{type} called here:\n" + getConsoleCallTraceLine()) if output.appendConsoleCallTrace
method = if console[type] then type else 'log'
args.unshift "[#{type}]"
console[method] prepareArgs(args).join(" ")
_taggedError: (tags, errorType, args) ->
###
Smart console.error:
* appends stack-trace of Error-typed argument if configured
* sends error information to logger
* displays error in console if configured
@param {Array} tags - tags for logging, e.g. ['error'] | ['warning']
@param {Any} args - usual console.error arguments
###
# Get error type from args
for item in args when item and item.stack
errorType = _minErrorType(errorType, errors.getType(item))
args = args.map (item) ->
if item and item.stack
if output.errorTrace then "\n#{item.stack}\n" else item.message
else
item
# Report the error so that we could show it to the user
if errorType == 'error'
@publish 'error.notify.publish',
message: 'Произошла ошибка'
console: true
link: ''
details: stringify(args)
# And log the error
@_log errorType, args if output[errorType]
## export ##
log: (args...) ->
@_log 'log', args
warn: (args...) ->
@_taggedError [], 'warn', args
error: (args...) ->
@_taggedError [], 'error', args
taggedError: (tags, args...) ->
@_taggedError tags, 'error', args
clear: ->
console.clear?()
return
assertLazy: (errorMessage, checkFunction) ->
###
Checks that checkFunction() value is true. Otherwise throws an error with errorMessage text.
###
if assertionsEnabled and not checkFunction()
throw new Error("Assertion failed. #{errorMessage}")
logAndPublish: (method, args, publish) ->
oldPublish = @publish
@publish = publish
@[method].apply(this, args)
@publish = oldPublish
publish: (message, params) ->
# No publish capabilites by default. Overloaded by logger while using