-
Notifications
You must be signed in to change notification settings - Fork 39
/
log.ts
192 lines (173 loc) · 4.72 KB
/
log.ts
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
// /////////////////////// Logging
//
// bitmask levels
// const TNONE = 0
/** @internal */
const TERROR = 1
/** @internal */
const TWARN = 2
/** @internal */
const TMESG = 4
/** @internal */
const TSUCCESS = 8
/** @internal */
const TINFO = 16
/** @internal */
const TDEBUG = 32
/** @internal */
const TALL = 63
/** @internal */
export enum LogLevel {
Error = TERROR,
Warning = TWARN,
Message = TMESG,
Success = TSUCCESS,
Info = TINFO,
Debug = TDEBUG,
All = TALL
}
/** @internal */
let _level: number = TERROR + TWARN + TMESG
/** @internal */
let _ascending: boolean = false
/** @internal */
let _dom: HTMLDocument = document // must be able to override for tests
/** @internal */
let _window: Window = window // must be able to override for tests
/** @internal */
function log (str: string, type: number = TMESG, typestr: string = 'mesg') {
if (!(_level & type)) return // bitmask
if (typeof _dom !== 'undefined') {
const logArea = _dom.getElementById('status')
if (!logArea) return
const addendum = _dom.createElement('span')
addendum.setAttribute('class', typestr)
const now = new Date()
addendum.innerHTML = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()} [${typestr}] ${escapeForXML(str)}<br/>`
if (_ascending) {
logArea.insertBefore(addendum, logArea.firstChild)
} else {
logArea.appendChild(addendum)
}
} else if (typeof console !== 'undefined') {
// eslint-disable-next-line no-console
console.log(str)
}
}
/**
* Adds a message to the element with id "status". The messages are prepended with
* time and type of message, in this case [mesg].
*/
export function msg (message: string) {
log(message)
}
/**
* Adds a warning message to the element with id "status". The messages are
* prepended with time and type of message, in this case [warn].
*/
export function warn (message: string): void {
log(message, TWARN, 'warn')
}
/**
* Adds a debugging message to the element with id "status". The messages are
* prepended with time and type of message, in this case [dbug].
*/
export function debug (message: string): void {
log(message, TDEBUG, 'dbug')
}
/**
* Adds a info message to the element with id "status". The messages are
* prepended with time and type of message, in this case [info].
*/
export function info (message: string): void {
log(message, TINFO, 'info')
}
/**
* Adds a error to the element with id "status". The messages are
* prepended with time and type of message, in this case [eror].
*/
export function error (message: string): void {
log(message, TERROR, 'eror')
}
/**
* Adds a success message to the element with id "status". The messages are
* prepended with time and type of message, in this case [good].
*/
export function success (message: string): void {
log(message, TSUCCESS, 'good')
}
/**
* Uses the global alert to send an alert. If global alert is not available, it
* will output the message using the method [[warning]]s.
*/
export function alert (message: string): void {
if (_window && typeof _window.alert !== 'undefined') {
_window.alert(message)
} else {
warn(message)
}
}
/**
* Will clear the content of the element with id "status".
*/
export function clear (): void {
const logArea = _dom?.getElementById('status')
if (!logArea) return
logArea.innerHTML = ''
}
/**
* Lets you configure which types of messages will be shown. The module uses
* [bitmask](https://en.wikipedia.org/wiki/Mask_(computing)) to filter which
* types of messages should be shown. E.g. if you only want warning messages
* to be shown, pass 2 to the function, if you want warning and success to be
* shown, pass 10 (2+8). By passing the sum of all, 63, you'll show all
* types of messages.
*
* - Error: 1
* - Warning: 2
* - Message: 4
* - Success: 8
* - Info: 16
* - Debug: 32
*/
export function setLevel (level: number): void {
_level = TALL
debug('Log level is now ' + level)
_level = level
}
/**
* Will dump the current HTML using the [[debug]] method.
*/
export function dumpHTML (): void {
if (!_dom) return
const level = _level
_level = TALL
debug(_dom?.body?.innerHTML || '')
_level = level
}
/**
* Will start prepending messages the list of log messages.
*/
export function logAscending () {
_ascending = true
}
/**
* Will start appending messages the list of log messages. (This is default
* behavior.)
*/
export function logDescending () {
_ascending = false
}
/** @internal */
export function escapeForXML (str: string): string {
// can be replaced with function utils module when migrating
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
}
/** @internal */
export function setInternals (window, document) {
_window = window
_dom = document
}