2019-09-01 15:24:06 +00:00
|
|
|
//Used by CertAccept popup
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
enum LogCategory {
|
|
|
|
CHANNEL,
|
2019-02-17 15:08:10 +00:00
|
|
|
CHANNEL_PROPERTIES, /* separating channel and channel properties because on channel init logging is a big bottleneck */
|
2018-04-16 18:38:35 +00:00
|
|
|
CLIENT,
|
2019-08-30 21:06:39 +00:00
|
|
|
BOOKMARKS,
|
2018-06-20 19:05:35 +00:00
|
|
|
SERVER,
|
2018-04-16 18:38:35 +00:00
|
|
|
PERMISSIONS,
|
|
|
|
GENERAL,
|
2018-09-26 13:30:22 +00:00
|
|
|
NETWORKING,
|
2018-12-15 13:04:29 +00:00
|
|
|
VOICE,
|
2019-08-30 21:06:39 +00:00
|
|
|
AUDIO,
|
2019-03-17 11:15:39 +00:00
|
|
|
I18N,
|
2019-04-04 19:47:52 +00:00
|
|
|
IPC,
|
2019-08-30 21:06:39 +00:00
|
|
|
IDENTITIES,
|
|
|
|
STATISTICS
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace log {
|
|
|
|
export enum LogType {
|
|
|
|
TRACE,
|
|
|
|
DEBUG,
|
|
|
|
INFO,
|
|
|
|
WARNING,
|
|
|
|
ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
let category_mapping = new Map<number, string>([
|
2019-02-17 15:08:10 +00:00
|
|
|
[LogCategory.CHANNEL, "Channel "],
|
2019-03-17 11:15:39 +00:00
|
|
|
[LogCategory.CHANNEL_PROPERTIES, "Channel "],
|
|
|
|
[LogCategory.CLIENT, "Client "],
|
2019-02-17 15:08:10 +00:00
|
|
|
[LogCategory.SERVER, "Server "],
|
2019-08-30 21:06:39 +00:00
|
|
|
[LogCategory.BOOKMARKS, "Bookmark "],
|
2019-02-17 15:08:10 +00:00
|
|
|
[LogCategory.PERMISSIONS, "Permission "],
|
|
|
|
[LogCategory.GENERAL, "General "],
|
|
|
|
[LogCategory.NETWORKING, "Network "],
|
|
|
|
[LogCategory.VOICE, "Voice "],
|
2019-08-30 21:06:39 +00:00
|
|
|
[LogCategory.AUDIO, "Audio "],
|
2019-03-17 11:15:39 +00:00
|
|
|
[LogCategory.I18N, "I18N "],
|
2019-08-30 21:06:39 +00:00
|
|
|
[LogCategory.IDENTITIES, "Identities "],
|
|
|
|
[LogCategory.IPC, "IPC "],
|
|
|
|
[LogCategory.STATISTICS, "Statistics "]
|
2018-04-16 18:38:35 +00:00
|
|
|
]);
|
|
|
|
|
2019-02-17 15:08:10 +00:00
|
|
|
export let enabled_mapping = new Map<number, boolean>([
|
|
|
|
[LogCategory.CHANNEL, true],
|
|
|
|
[LogCategory.CHANNEL_PROPERTIES, false],
|
|
|
|
[LogCategory.CLIENT, true],
|
|
|
|
[LogCategory.SERVER, true],
|
2019-08-30 21:06:39 +00:00
|
|
|
[LogCategory.BOOKMARKS, true],
|
2019-02-17 15:08:10 +00:00
|
|
|
[LogCategory.PERMISSIONS, true],
|
|
|
|
[LogCategory.GENERAL, true],
|
|
|
|
[LogCategory.NETWORKING, true],
|
|
|
|
[LogCategory.VOICE, true],
|
2019-08-30 21:06:39 +00:00
|
|
|
[LogCategory.AUDIO, true],
|
2019-03-17 11:15:39 +00:00
|
|
|
[LogCategory.I18N, false],
|
2019-04-04 19:47:52 +00:00
|
|
|
[LogCategory.IDENTITIES, true],
|
2019-08-30 21:06:39 +00:00
|
|
|
[LogCategory.IPC, true],
|
|
|
|
[LogCategory.STATISTICS, true]
|
|
|
|
]);
|
|
|
|
|
|
|
|
//Values will be overridden by initialize()
|
|
|
|
export let level_mapping = new Map<LogType, boolean>([
|
|
|
|
[LogType.TRACE, true],
|
|
|
|
[LogType.DEBUG, true],
|
|
|
|
[LogType.INFO, true],
|
|
|
|
[LogType.WARNING, true],
|
|
|
|
[LogType.ERROR, true]
|
2019-02-17 15:08:10 +00:00
|
|
|
]);
|
|
|
|
|
2019-03-25 19:04:04 +00:00
|
|
|
enum GroupMode {
|
|
|
|
NATIVE,
|
|
|
|
PREFIX
|
|
|
|
}
|
2019-04-25 18:22:13 +00:00
|
|
|
const group_mode: GroupMode = GroupMode.PREFIX;
|
2019-03-25 19:04:04 +00:00
|
|
|
|
2019-08-30 21:06:39 +00:00
|
|
|
//Category Example: <url>?log.i18n.enabled=0
|
|
|
|
//Level Example A: <url>?log.level.trace.enabled=0
|
|
|
|
//Level Example B: <url>?log.level=0
|
2019-08-31 16:31:01 +00:00
|
|
|
export function initialize(default_level: LogType) {
|
2019-02-17 15:08:10 +00:00
|
|
|
for(const category of Object.keys(LogCategory).map(e => parseInt(e))) {
|
|
|
|
if(isNaN(category)) continue;
|
2019-08-30 21:06:39 +00:00
|
|
|
const category_name = LogCategory[category].toLowerCase();
|
|
|
|
enabled_mapping.set(category, settings.static_global<boolean>("log." + category_name.toLowerCase() + ".enabled", enabled_mapping.get(category)));
|
|
|
|
}
|
|
|
|
|
2019-08-31 16:31:01 +00:00
|
|
|
const base_level = settings.static_global<number>("log.level", default_level);
|
2019-08-30 21:06:39 +00:00
|
|
|
|
|
|
|
for(const level of Object.keys(LogType).map(e => parseInt(e))) {
|
|
|
|
if(isNaN(level)) continue;
|
|
|
|
|
|
|
|
const level_name = LogType[level].toLowerCase();
|
|
|
|
level_mapping.set(level, settings.static_global<boolean>("log." + level_name + ".enabled", level >= base_level));
|
2019-02-17 15:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
function logDirect(type: LogType, message: string, ...optionalParams: any[]) {
|
2019-08-30 21:06:39 +00:00
|
|
|
if(!level_mapping.get(type))
|
|
|
|
return;
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
switch (type) {
|
|
|
|
case LogType.TRACE:
|
|
|
|
case LogType.DEBUG:
|
|
|
|
console.debug(message, ...optionalParams);
|
|
|
|
break;
|
|
|
|
case LogType.INFO:
|
|
|
|
console.log(message, ...optionalParams);
|
|
|
|
break;
|
|
|
|
case LogType.WARNING:
|
|
|
|
console.warn(message, ...optionalParams);
|
|
|
|
break;
|
|
|
|
case LogType.ERROR:
|
|
|
|
console.error(message, ...optionalParams);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function log(type: LogType, category: LogCategory, message: string, ...optionalParams: any[]) {
|
2019-08-30 21:06:39 +00:00
|
|
|
if(!enabled_mapping.get(category)) return;
|
2019-02-17 15:08:10 +00:00
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
optionalParams.unshift(category_mapping.get(category));
|
|
|
|
message = "[%s] " + message;
|
|
|
|
logDirect(type, message, ...optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function trace(category: LogCategory, message: string, ...optionalParams: any[]) {
|
|
|
|
log(LogType.TRACE, category, message, ...optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function debug(category: LogCategory, message: string, ...optionalParams: any[]) {
|
|
|
|
log(LogType.DEBUG, category, message, ...optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function info(category: LogCategory, message: string, ...optionalParams: any[]) {
|
|
|
|
log(LogType.INFO, category, message, ...optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function warn(category: LogCategory, message: string, ...optionalParams: any[]) {
|
|
|
|
log(LogType.WARNING, category, message, ...optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function error(category: LogCategory, message: string, ...optionalParams: any[]) {
|
|
|
|
log(LogType.ERROR, category, message, ...optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function group(level: LogType, category: LogCategory, name: string, ...optionalParams: any[]) : Group {
|
|
|
|
name = "[%s] " + name;
|
|
|
|
optionalParams.unshift(category_mapping.get(category));
|
|
|
|
|
2019-03-25 19:04:04 +00:00
|
|
|
return new Group(group_mode, level, category, name, optionalParams);
|
2019-03-17 11:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 21:06:39 +00:00
|
|
|
export function table(level: LogType, category: LogCategory, title: string, arguments: any) {
|
2019-03-25 19:04:04 +00:00
|
|
|
if(group_mode == GroupMode.NATIVE) {
|
|
|
|
console.groupCollapsed(title);
|
|
|
|
console.table(arguments);
|
|
|
|
console.groupEnd();
|
|
|
|
} else {
|
2019-08-30 21:06:39 +00:00
|
|
|
if(!enabled_mapping.get(category) || !level_mapping.get(level))
|
|
|
|
return;
|
|
|
|
logDirect(level, tr("Snipped table \"%s\""), title);
|
2019-03-25 19:04:04 +00:00
|
|
|
}
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Group {
|
2019-03-17 11:15:39 +00:00
|
|
|
readonly mode: GroupMode;
|
2018-04-16 18:38:35 +00:00
|
|
|
readonly level: LogType;
|
|
|
|
readonly category: LogCategory;
|
2019-02-17 15:08:10 +00:00
|
|
|
readonly enabled: boolean;
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
owner: Group = undefined;
|
|
|
|
|
|
|
|
private readonly name: string;
|
|
|
|
private readonly optionalParams: any[][];
|
2019-03-25 19:04:04 +00:00
|
|
|
private _collapsed: boolean = false;
|
2018-04-16 18:38:35 +00:00
|
|
|
private initialized = false;
|
2019-03-17 11:15:39 +00:00
|
|
|
private _log_prefix: string;
|
2018-04-16 18:38:35 +00:00
|
|
|
|
2019-03-17 11:15:39 +00:00
|
|
|
constructor(mode: GroupMode, level: LogType, category: LogCategory, name: string, optionalParams: any[][], owner: Group = undefined) {
|
2018-04-16 18:38:35 +00:00
|
|
|
this.level = level;
|
2019-03-17 11:15:39 +00:00
|
|
|
this.mode = mode;
|
2018-04-16 18:38:35 +00:00
|
|
|
this.category = category;
|
|
|
|
this.name = name;
|
|
|
|
this.optionalParams = optionalParams;
|
2019-08-30 21:06:39 +00:00
|
|
|
this.enabled = enabled_mapping.get(category);
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
group(level: LogType, name: string, ...optionalParams: any[]) : Group {
|
2019-03-17 11:15:39 +00:00
|
|
|
return new Group(this.mode, level, this.category, name, optionalParams, this);
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
collapsed(flag: boolean = true) : this {
|
|
|
|
this._collapsed = flag;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(message: string, ...optionalParams: any[]) : this {
|
2019-02-17 15:08:10 +00:00
|
|
|
if(!this.enabled)
|
|
|
|
return this;
|
|
|
|
|
2018-12-02 13:12:23 +00:00
|
|
|
if(!this.initialized) {
|
2019-03-17 11:15:39 +00:00
|
|
|
if(this.mode == GroupMode.NATIVE) {
|
|
|
|
if(this._collapsed && console.groupCollapsed)
|
|
|
|
console.groupCollapsed(this.name, ...this.optionalParams);
|
|
|
|
else
|
|
|
|
console.group(this.name, ...this.optionalParams);
|
|
|
|
} else {
|
|
|
|
this._log_prefix = " ";
|
|
|
|
let parent = this.owner;
|
|
|
|
while(parent) {
|
|
|
|
if(parent.mode == GroupMode.PREFIX)
|
|
|
|
this._log_prefix = this._log_prefix + parent._log_prefix;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 18:38:35 +00:00
|
|
|
this.initialized = true;
|
|
|
|
}
|
2019-03-17 11:15:39 +00:00
|
|
|
if(this.mode == GroupMode.NATIVE)
|
|
|
|
logDirect(this.level, message, ...optionalParams);
|
2019-08-30 21:06:39 +00:00
|
|
|
else {
|
|
|
|
logDirect(this.level, "[%s] " + this._log_prefix + message, category_mapping.get(this.category), ...optionalParams);
|
|
|
|
}
|
2018-04-16 18:38:35 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
2019-03-17 11:15:39 +00:00
|
|
|
if(this.initialized) {
|
|
|
|
if(this.mode == GroupMode.NATIVE)
|
|
|
|
console.groupEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get prefix() : string {
|
|
|
|
return this._log_prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
set prefix(prefix: string) {
|
|
|
|
this._log_prefix = prefix;
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-01 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
import LogType = log.LogType;
|