Skip to content

Commit 64698df

Browse files
committed
Added documentation for log module
Reverted use of class, has "flattened" set of methods
1 parent 8332e96 commit 64698df

9 files changed

Lines changed: 179 additions & 101 deletions

File tree

src/authn/authn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import solidAuthClient from 'solid-auth-client'
2626
import ns from '../ns.js'
2727
import kb from '../store.js'
2828
import utils from '../utils.js'
29-
import { log } from '../log'
29+
import { alert } from '../log'
3030
import { AppDetails, AuthenticationContext } from './types'
3131
import { PaneDefinition } from 'pane-registry'
3232

@@ -1069,7 +1069,7 @@ export function loginStatusBox (
10691069
const message = `Your WebID was ${me}. It has been forgotten.`
10701070
me = null
10711071
try {
1072-
log.alert(message)
1072+
alert(message)
10731073
} catch (e) {
10741074
window.alert(message)
10751075
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { authn } from './authn/index'
4646
import create from './create'
4747
// @ts-ignore
4848
import icons from './iconBase'
49-
import { log } from './log'
49+
import * as log from './log'
5050
// @ts-ignore
5151
import matrix from './matrix'
5252
// @ts-ignore

src/log.ts

Lines changed: 145 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
//
33
// bitmask levels
44
// const TNONE = 0
5+
/** @internal */
56
const TERROR = 1
7+
/** @internal */
68
const TWARN = 2
9+
/** @internal */
710
const TMESG = 4
11+
/** @internal */
812
const TSUCCESS = 8
13+
/** @internal */
914
const TINFO = 16
15+
/** @internal */
1016
const TDEBUG = 32
17+
/** @internal */
1118
const TALL = 63
1219

20+
/** @internal */
1321
export 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 */
99177
export 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, '&gt;')
105183
}
106184

107-
export const log = new Log()
185+
/** @internal */
186+
export function setInternals (window, document) {
187+
_window = window
188+
_dom = document
189+
}

src/table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
var UI = {
1717
icons: require('./iconBase'),
18-
log: require('./log').log,
18+
log: require('./log'),
1919
ns: require('./ns'),
2020
store: require('./store'),
2121
widgets: require('./widgets')

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
}
3737

3838
var UI = {
39-
log: require('./log').log,
39+
log: require('./log'),
4040
ns: require('./ns'),
4141
rdf: require('rdflib'),
4242
store: require('./store')

src/widgets/buttons.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { iconBase, originalIconBase } from '../iconBase'
33
import store from '../store'
44
import ns from '../ns'
55
import style from '../style'
6-
import { log } from '../log'
6+
import { info } from '../log'
77

88
/**
99
* UI Widgets such as buttons
@@ -869,7 +869,7 @@ export function propertyTriage (kb: IndexedFormula): any {
869869
}
870870
possibleProperties.op = op
871871
possibleProperties.dp = dp
872-
log.info(`propertyTriage: ${no} non-lit, ${nd} literal. ${nu} unknown.`)
872+
info(`propertyTriage: ${no} non-lit, ${nd} literal. ${nu} unknown.`)
873873
return possibleProperties
874874
}
875875

src/widgets/forms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ forms.field = {} // Form field functions by URI of field type.
1515

1616
var UI = {
1717
icons: require('../iconBase'),
18-
log: require('../log').log,
18+
log: require('../log'),
1919
ns: require('../ns'),
2020
store: require('../store'),
2121
style: require('../style'),

test/unit/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('Index', () => {
1010
'authn',
1111
'create',
1212
'icons',
13-
'log',
1413
'matrix',
1514
'media',
1615
'messageArea',
@@ -25,6 +24,7 @@ describe('Index', () => {
2524
'versionInfo',
2625
'dom',
2726
'rdf',
27+
'log',
2828
'tabs'
2929
])
3030
})

0 commit comments

Comments
 (0)