-
Notifications
You must be signed in to change notification settings - Fork 97
/
logging.ts
65 lines (54 loc) · 1.76 KB
/
logging.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
import { DotvvmPostbackError } from "../shared-classes";
type LogLevel = "normal" | "verbose";
export const level = getLogLevel();
export type DotvvmLoggingArea = (
| "debug"
| "configuration"
| "postback"
| "spa"
| "static-command"
| "binding-handler"
| "resource-loader"
| "coercer"
| "state-manager"
| "validation"
| "events"
| "rest-api"
)
export function logInfoVerbose(area: DotvvmLoggingArea, ...args: any[]) {
if (compileConstants.debug && level === "verbose") {
console.log(area, ...args);
}
}
export function logInfo(area: DotvvmLoggingArea, ...args: any[]) {
console.log(area, ...args);
}
export function logWarning(area: DotvvmLoggingArea, ...args: any[]) {
console.warn(area, ...args);
}
export function logError(area: DotvvmLoggingArea, ...args: any[]) {
console.error(area, ...args);
}
export function logPostBackScriptError(err: any) {
if (err instanceof DotvvmPostbackError) {
return; // this was logged or handled in the postback pipeline
}
logError("postback", "Uncaught error returned from promise!", err);
}
function getLogLevel() : LogLevel {
if (compileConstants.debug) {
var logLevel = window.localStorage.getItem("dotvvm-loglevel");
if (!logLevel) return "normal";
if (logLevel === "normal" || logLevel === "verbose") return logLevel;
logWarning("configuration", "Invalid value of 'dotvvm-loglevel' config value! Supported values: 'normal', 'verbose'");
}
return "normal";
}
/** puts the string in quotes, escaping weird characters if it is more complex than just letters */
export function debugQuoteString(s: string) {
if (/[\w-_]/.test(s)) {
return s;
} else {
return JSON.stringify(s);
}
}