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,
|
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-03-17 11:15:39 +00:00
|
|
|
I18N,
|
|
|
|
IDENTITIES
|
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 "],
|
|
|
|
[LogCategory.PERMISSIONS, "Permission "],
|
|
|
|
[LogCategory.GENERAL, "General "],
|
|
|
|
[LogCategory.NETWORKING, "Network "],
|
|
|
|
[LogCategory.VOICE, "Voice "],
|
2019-03-17 11:15:39 +00:00
|
|
|
[LogCategory.I18N, "I18N "],
|
|
|
|
[LogCategory.IDENTITIES, "IDENTITIES "]
|
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],
|
|
|
|
[LogCategory.PERMISSIONS, true],
|
|
|
|
[LogCategory.GENERAL, true],
|
|
|
|
[LogCategory.NETWORKING, true],
|
|
|
|
[LogCategory.VOICE, true],
|
2019-03-17 11:15:39 +00:00
|
|
|
[LogCategory.I18N, false],
|
|
|
|
[LogCategory.IDENTITIES, true]
|
2019-02-17 15:08:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
loader.register_task(loader.Stage.LOADED, {
|
|
|
|
name: "log enabled initialisation",
|
|
|
|
function: async () => initialize(),
|
|
|
|
priority: 10
|
|
|
|
});
|
|
|
|
|
|
|
|
//Example: <url>?log.i18n.enabled=0
|
|
|
|
export function initialize() {
|
|
|
|
for(const category of Object.keys(LogCategory).map(e => parseInt(e))) {
|
|
|
|
if(isNaN(category)) continue;
|
|
|
|
const category_name = LogCategory[category];
|
|
|
|
enabled_mapping[category] = settings.static_global<boolean>("log." + category_name.toLowerCase() + ".enabled", enabled_mapping.get(category));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
function logDirect(type: LogType, message: string, ...optionalParams: any[]) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
//console.log("This is %cMy stylish message", "color: yellow; font-style: italic; background-color: blue;padding: 2px");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function log(type: LogType, category: LogCategory, message: string, ...optionalParams: any[]) {
|
2019-02-17 15:08:10 +00:00
|
|
|
if(!enabled_mapping[category]) return;
|
|
|
|
|
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-17 11:15:39 +00:00
|
|
|
return new Group(GroupMode.PREFIX, level, category, name, optionalParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GroupMode {
|
|
|
|
NATIVE,
|
|
|
|
PREFIX
|
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[][];
|
|
|
|
private _collapsed: boolean = true;
|
|
|
|
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-02-17 15:08:10 +00:00
|
|
|
this.enabled = enabled_mapping[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);
|
|
|
|
else
|
|
|
|
logDirect(this.level, this._log_prefix + message, ...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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|