22//
33// bitmask levels
44// const TNONE = 0
5+ /** @internal */
56const TERROR = 1
7+ /** @internal */
68const TWARN = 2
9+ /** @internal */
710const TMESG = 4
11+ /** @internal */
812const TSUCCESS = 8
13+ /** @internal */
914const TINFO = 16
15+ /** @internal */
1016const TDEBUG = 32
17+ /** @internal */
1118const TALL = 63
1219
20+ /** @internal */
1321export enum LogLevel {
1422 Error = TERROR ,
1523 Warning = TWARN ,
@@ -20,82 +28,152 @@ export enum LogLevel {
2028 All = TALL
2129}
2230
23- class Log {
24- public level : number = TERROR + TWARN + TMESG
25- public ascending : boolean = false
26- public dom ?: HTMLDocument = document // must be able to override for tests
27- public window ?: Window = window // must be able to override for tests
28-
29- public msg ( str : string , type : number = TMESG , typestr : string = 'mesg' ) {
30- if ( ! ( this . level & type ) ) return // bitmask
31-
32- if ( typeof this . dom !== 'undefined' ) {
33- const logArea = this . dom . getElementById ( 'status' )
34- if ( ! logArea ) return
35-
36- const addendum = this . dom . createElement ( 'span' )
37- addendum . setAttribute ( 'class' , typestr )
38- const now = new Date ( )
39- addendum . innerHTML = `${ now . getHours ( ) } :${ now . getMinutes ( ) } :${ now . getSeconds ( ) } [${ typestr } ] ${ escapeForXML ( str ) } <br/>`
40- if ( this . ascending ) {
41- logArea . insertBefore ( addendum , logArea . firstChild )
42- } else {
43- logArea . appendChild ( addendum )
44- }
45- } else if ( typeof console !== 'undefined' ) {
46- console . log ( str )
31+ /** @internal */
32+ let _level : number = TERROR + TWARN + TMESG
33+ /** @internal */
34+ let _ascending : boolean = false
35+ /** @internal */
36+ let _dom : HTMLDocument = document // must be able to override for tests
37+ /** @internal */
38+ let _window : Window = window // must be able to override for tests
39+
40+ /** @internal */
41+ function log ( str : string , type : number = TMESG , typestr : string = 'mesg' ) {
42+ if ( ! ( _level & type ) ) return // bitmask
43+
44+ if ( typeof _dom !== 'undefined' ) {
45+ const logArea = _dom . getElementById ( 'status' )
46+ if ( ! logArea ) return
47+
48+ const addendum = _dom . createElement ( 'span' )
49+ addendum . setAttribute ( 'class' , typestr )
50+ const now = new Date ( )
51+ addendum . innerHTML = `${ now . getHours ( ) } :${ now . getMinutes ( ) } :${ now . getSeconds ( ) } [${ typestr } ] ${ escapeForXML ( str ) } <br/>`
52+ if ( _ascending ) {
53+ logArea . insertBefore ( addendum , logArea . firstChild )
54+ } else {
55+ logArea . appendChild ( addendum )
4756 }
57+ } else if ( typeof console !== 'undefined' ) {
58+ console . log ( str )
4859 }
60+ }
4961
50- warn ( msg : string ) : void {
51- this . msg ( msg , TWARN , 'warn' )
52- }
62+ /**
63+ * Adds a message to the element with id "status". The messages are prepended with
64+ * time and type of message, in this case [mesg].
65+ */
66+ export function msg ( message : string ) {
67+ log ( message )
68+ }
5369
54- debug ( msg : string ) : void {
55- this . msg ( msg , TDEBUG , 'dbug' )
56- }
70+ /**
71+ * Adds a warning message to the element with id "status". The messages are
72+ * prepended with time and type of message, in this case [warn].
73+ */
74+ export function warn ( message : string ) : void {
75+ log ( message , TWARN , 'warn' )
76+ }
5777
58- info ( msg : string ) : void {
59- this . msg ( msg , TINFO , 'info' )
60- }
78+ /**
79+ * Adds a debugging message to the element with id "status". The messages are
80+ * prepended with time and type of message, in this case [dbug].
81+ */
82+ export function debug ( message : string ) : void {
83+ log ( message , TDEBUG , 'dbug' )
84+ }
6185
62- error ( msg : string ) : void {
63- this . msg ( msg , TERROR , 'eror' )
64- }
86+ /**
87+ * Adds a info message to the element with id "status". The messages are
88+ * prepended with time and type of message, in this case [info].
89+ */
90+ export function info ( message : string ) : void {
91+ log ( message , TINFO , 'info' )
92+ }
6593
66- success ( msg : string ) : void {
67- this . msg ( msg , TSUCCESS , 'good' )
68- }
94+ /**
95+ * Adds a error to the element with id "status". The messages are
96+ * prepended with time and type of message, in this case [eror].
97+ */
98+ export function error ( message : string ) : void {
99+ log ( message , TERROR , 'eror' )
100+ }
69101
70- alert ( msg : string ) : void {
71- if ( this . window && typeof this . window . alert !== 'undefined' ) {
72- this . window . alert ( msg )
73- } else {
74- this . warn ( msg )
75- }
76- }
102+ /**
103+ * Adds a success message to the element with id "status". The messages are
104+ * prepended with time and type of message, in this case [good].
105+ */
106+ export function success ( message : string ) : void {
107+ log ( message , TSUCCESS , 'good' )
108+ }
77109
78- clear ( ) : void {
79- const logArea = this . dom ?. getElementById ( 'status' )
80- if ( ! logArea ) return
81- logArea . innerHTML = ''
110+ /**
111+ * Uses the global alert to send an alert. If global alert is not available, it
112+ * will output the message using the method [[warning]]s.
113+ */
114+ export function alert ( message : string ) : void {
115+ if ( _window && typeof _window . alert !== 'undefined' ) {
116+ _window . alert ( message )
117+ } else {
118+ warn ( message )
82119 }
120+ }
83121
84- setLevel ( level : number ) : void {
85- this . level = TALL
86- this . debug ( 'Log level is now ' + level )
87- this . level = level
88- }
122+ /**
123+ * Will clear the content of the element with id "status".
124+ */
125+ export function clear ( ) : void {
126+ const logArea = _dom ?. getElementById ( 'status' )
127+ if ( ! logArea ) return
128+ logArea . innerHTML = ''
129+ }
89130
90- dumpHTML ( ) : void {
91- if ( ! this . dom ) return
92- const level = this . level
93- this . level = TALL
94- this . debug ( this . dom ?. body ?. innerHTML || '' )
95- this . level = level
96- }
131+ /**
132+ * Lets you configure which types of messages will be shown. The number is a
133+ * sum of the levels you want to show:
134+ *
135+ * - Error: 1
136+ * - Warning: 2
137+ * - Message: 4
138+ * - Success: 8
139+ * - Info: 16
140+ * - Debug: 32
141+ *
142+ * You can also choose to set all by passing the number *64*.
143+ */
144+ export function setLevel ( level : number ) : void {
145+ _level = TALL
146+ debug ( 'Log level is now ' + level )
147+ _level = level
97148}
98149
150+ /**
151+ * Will dump the current HTML using the [[debug]] method.
152+ */
153+ export function dumpHTML ( ) : void {
154+ if ( ! _dom ) return
155+ const level = _level
156+ _level = TALL
157+ debug ( _dom ?. body ?. innerHTML || '' )
158+ _level = level
159+ }
160+
161+ /**
162+ * Will start prepending messages the list of log messages.
163+ */
164+ export function logAscending ( ) {
165+ _ascending = true
166+ }
167+
168+ /**
169+ * Will start appending messages the list of log messages. (This is default
170+ * behavior.)
171+ */
172+ export function logDescending ( ) {
173+ _ascending = false
174+ }
175+
176+ /** @internal */
99177export function escapeForXML ( str : string ) : string {
100178 // can be replaced with function utils module when migrating
101179 return str
@@ -104,4 +182,8 @@ export function escapeForXML (str: string): string {
104182 . replace ( / > / g, '>' )
105183}
106184
107- export const log = new Log ( )
185+ /** @internal */
186+ export function setInternals ( window , document ) {
187+ _window = window
188+ _dom = document
189+ }
0 commit comments