TeaWeb/shared/js/log.ts

125 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-04-16 18:38:35 +00:00
enum LogCategory {
CHANNEL,
CLIENT,
SERVER,
2018-04-16 18:38:35 +00:00
PERMISSIONS,
GENERAL,
2018-09-26 13:30:22 +00:00
NETWORKING,
VOICE
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>([
[LogCategory.CHANNEL, "Channel "],
[LogCategory.CLIENT, "Client "],
[LogCategory.SERVER, "Server "],
2018-04-16 18:38:35 +00:00
[LogCategory.PERMISSIONS, "Permission "],
[LogCategory.GENERAL, "General "],
2018-09-26 13:30:22 +00:00
[LogCategory.NETWORKING, "Network "],
[LogCategory.VOICE, "Voice "]
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[]) {
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));
return new Group(level, category, name, optionalParams);
}
export class Group {
readonly level: LogType;
readonly category: LogCategory;
owner: Group = undefined;
private readonly name: string;
private readonly optionalParams: any[][];
private _collapsed: boolean = true;
private initialized = false;
constructor(level: LogType, category: LogCategory, name: string, optionalParams: any[][], owner: Group = undefined) {
this.level = level;
this.category = category;
this.name = name;
this.optionalParams = optionalParams;
}
group(level: LogType, name: string, ...optionalParams: any[]) : Group {
return new Group(level, this.category, name, optionalParams, this);
}
collapsed(flag: boolean = true) : this {
this._collapsed = flag;
return this;
}
log(message: string, ...optionalParams: any[]) : this {
2018-12-02 13:12:23 +00:00
if(!this.initialized) {
2018-04-16 18:38:35 +00:00
if(this._collapsed && console.groupCollapsed)
console.groupCollapsed(this.name, ...this.optionalParams);
else
console.group(this.name, ...this.optionalParams);
this.initialized = true;
}
logDirect(this.level, message, ...optionalParams);
return this;
}
end() {
if(this.initialized)
console.groupEnd();
}
}
}