TeaWeb/shared/js/log.ts

277 lines
9.4 KiB
TypeScript
Raw Permalink Normal View History

import {Settings, settings} from "./settings";
2020-03-30 11:44:18 +00:00
import * as loader from "tc-loader";
import { tr } from "./i18n/localize";
2019-09-01 15:24:06 +00:00
2020-03-30 11:44:18 +00:00
export enum LogCategory {
2018-04-16 18:38:35 +00:00
CHANNEL,
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
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,
SERVER,
2018-04-16 18:38:35 +00:00
PERMISSIONS,
GENERAL,
2018-09-26 13:30:22 +00:00
NETWORKING,
VOICE,
2019-09-12 21:59:35 +00:00
CHAT,
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,
2020-02-16 17:09:59 +00:00
STATISTICS,
DNS,
2020-07-23 22:41:04 +00:00
FILE_TRANSFER,
EVENT_REGISTRY,
WEBRTC,
VIDEO
2018-04-16 18:38:35 +00:00
}
2020-03-30 11:44:18 +00:00
export enum LogType {
TRACE,
DEBUG,
INFO,
WARNING,
ERROR
}
2018-04-16 18:38:35 +00:00
let categoryMapping = new Map<number, string>([
2020-07-23 22:41:04 +00:00
[LogCategory.CHANNEL, "Channel "],
[LogCategory.CHANNEL_PROPERTIES, "Channel "],
[LogCategory.CLIENT, "Client "],
[LogCategory.SERVER, "Server "],
[LogCategory.BOOKMARKS, "Bookmark "],
[LogCategory.PERMISSIONS, "Permission "],
[LogCategory.GENERAL, "General "],
[LogCategory.NETWORKING, "Network "],
[LogCategory.VOICE, "Voice "],
[LogCategory.AUDIO, "Audio "],
[LogCategory.CHAT, "Chat "],
[LogCategory.I18N, "I18N "],
[LogCategory.IDENTITIES, "Identities "],
[LogCategory.IPC, "IPC "],
[LogCategory.STATISTICS, "Statistics "],
[LogCategory.DNS, "DNS "],
[LogCategory.FILE_TRANSFER, "File transfer "],
[LogCategory.EVENT_REGISTRY, "Event registry"],
[LogCategory.WEBRTC, "WebRTC "],
2021-01-10 17:32:17 +00:00
[LogCategory.VIDEO, "Video "],
2020-03-30 11:44:18 +00:00
]);
export let enabled_mapping = new Map<number, boolean>([
[LogCategory.CHANNEL, true],
[LogCategory.CHANNEL_PROPERTIES, false],
[LogCategory.CLIENT, true],
[LogCategory.SERVER, true],
[LogCategory.BOOKMARKS, true],
[LogCategory.PERMISSIONS, true],
[LogCategory.GENERAL, true],
[LogCategory.NETWORKING, true],
[LogCategory.VOICE, true],
[LogCategory.AUDIO, true],
[LogCategory.CHAT, true],
2021-01-17 22:11:21 +00:00
[LogCategory.I18N, false],
2020-03-30 11:44:18 +00:00
[LogCategory.IDENTITIES, true],
[LogCategory.IPC, true],
[LogCategory.STATISTICS, true],
[LogCategory.DNS, true],
2020-07-23 22:41:04 +00:00
[LogCategory.FILE_TRANSFER, true],
[LogCategory.EVENT_REGISTRY, true],
[LogCategory.WEBRTC, true],
[LogCategory.VIDEO, true],
2020-03-30 11:44:18 +00:00
]);
//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]
]);
enum GroupMode {
NATIVE,
PREFIX
}
const defaultGroupMode: GroupMode = GroupMode.PREFIX;
2020-03-30 11:44:18 +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
2020-07-23 22:41:04 +00:00
export function initialize(defaultLogLevel: LogType) {
for(const category of Object.keys(LogCategory).map(e => parseInt(e)).filter(e => !isNaN(e))) {
2020-07-19 16:49:00 +00:00
const categoryName = LogCategory[category].toLowerCase();
2021-01-10 15:13:15 +00:00
enabled_mapping.set(category, settings.getValue(Settings.FN_LOG_ENABLED(categoryName), enabled_mapping.get(category)));
2019-03-25 19:04:04 +00:00
}
2019-08-30 21:06:39 +00:00
2021-01-10 15:13:15 +00:00
const baseLogLevel = settings.getValue(Settings.KEY_LOG_LEVEL, defaultLogLevel);
for(const level of Object.keys(LogType).map(e => parseInt(e)).filter(e => !isNaN(e))) {
2020-07-19 16:49:00 +00:00
const levelName = LogType[level].toLowerCase();
2021-01-10 15:13:15 +00:00
level_mapping.set(level, settings.getValue(Settings.FN_LOG_LEVEL_ENABLED(levelName), level >= baseLogLevel));
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
}
2020-03-30 11:44:18 +00:00
}
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
2020-03-30 11:44:18 +00:00
function logDirect(type: LogType, message: string, ...optionalParams: any[]) {
if(!level_mapping.get(type))
return;
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;
2018-04-16 18:38:35 +00:00
}
2020-03-30 11:44:18 +00:00
}
2018-04-16 18:38:35 +00:00
function doLog(type: LogType, category: LogCategory, message: string, ...optionalParams: any[]) {
2020-03-30 11:44:18 +00:00
if(!enabled_mapping.get(category)) return;
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
optionalParams.unshift(categoryMapping.get(category));
2020-03-30 11:44:18 +00:00
message = "[%s] " + message;
logDirect(type, message, ...optionalParams);
}
2018-04-16 18:38:35 +00:00
/* methods for direct import */
export function logTrace(category: LogCategory, message: string, ...optionalParams: any[]) {
doLog(LogType.TRACE, category, message, ...optionalParams);
}
export function logDebug(category: LogCategory, message: string, ...optionalParams: any[]) {
doLog(LogType.DEBUG, category, message, ...optionalParams);
}
export function logInfo(category: LogCategory, message: string, ...optionalParams: any[]) {
doLog(LogType.INFO, category, message, ...optionalParams);
}
export function logWarn(category: LogCategory, message: string, ...optionalParams: any[]) {
doLog(LogType.WARNING, category, message, ...optionalParams);
}
export function logError(category: LogCategory, message: string, ...optionalParams: any[]) {
doLog(LogType.ERROR, category, message, ...optionalParams);
}
2020-03-30 11:44:18 +00:00
export function group(level: LogType, category: LogCategory, name: string, ...optionalParams: any[]) : Group {
name = "[%s] " + name;
optionalParams.unshift(categoryMapping.get(category));
2018-04-16 18:38:35 +00:00
return new Group(defaultGroupMode, level, category, name, optionalParams);
}
export function logGroupNative(level: LogType, category: LogCategory, name: string, ...optionalParams: any[]) : Group {
name = "[%s] " + name;
optionalParams.unshift(categoryMapping.get(category));
return new Group(GroupMode.NATIVE, level, category, name, optionalParams);
2020-03-30 11:44:18 +00:00
}
export function table(level: LogType, category: LogCategory, title: string, args: any) {
if(defaultGroupMode == GroupMode.NATIVE) {
2020-03-30 11:44:18 +00:00
console.groupCollapsed(title);
console.table(args);
console.groupEnd();
} else {
if(!enabled_mapping.get(category) || !level_mapping.get(level))
return;
logDirect(level, tr("Snipped table \"%s\""), title);
2019-03-17 11:15:39 +00:00
}
2020-03-30 11:44:18 +00:00
}
2019-03-17 11:15:39 +00:00
2020-03-30 11:44:18 +00:00
export class Group {
readonly mode: GroupMode;
readonly level: LogType;
readonly category: LogCategory;
readonly enabled: boolean;
owner: Group = undefined;
private readonly name: string;
private readonly optionalParams: any[][];
private isCollapsed: boolean = false;
2020-03-30 11:44:18 +00:00
private initialized = false;
private logPrefix: string;
2020-03-30 11:44:18 +00:00
constructor(mode: GroupMode, level: LogType, category: LogCategory, name: string, optionalParams: any[][], owner: Group = undefined) {
this.level = level;
this.mode = mode;
this.category = category;
this.name = name;
this.optionalParams = optionalParams;
this.enabled = enabled_mapping.get(category);
2018-04-16 18:38:35 +00:00
}
2020-03-30 11:44:18 +00:00
group(level: LogType, name: string, ...optionalParams: any[]) : Group {
return new Group(this.mode, level, this.category, name, optionalParams, this);
}
2018-04-16 18:38:35 +00:00
2020-03-30 11:44:18 +00:00
collapsed(flag: boolean = true) : this {
this.isCollapsed = flag;
2020-03-30 11:44:18 +00:00
return this;
}
2018-04-16 18:38:35 +00:00
2020-03-30 11:44:18 +00:00
log(message: string, ...optionalParams: any[]) : this {
2020-12-07 14:07:47 +00:00
if(!this.enabled) {
2018-04-16 18:38:35 +00:00
return this;
2020-12-07 14:07:47 +00:00
}
2018-04-16 18:38:35 +00:00
2020-03-30 11:44:18 +00:00
if(!this.initialized) {
if(this.mode == GroupMode.NATIVE) {
if(this.isCollapsed && console.groupCollapsed) {
2020-03-30 11:44:18 +00:00
console.groupCollapsed(this.name, ...this.optionalParams);
2020-12-07 14:07:47 +00:00
} else {
2020-03-30 11:44:18 +00:00
console.group(this.name, ...this.optionalParams);
2020-12-07 14:07:47 +00:00
}
2020-03-30 11:44:18 +00:00
} else {
this.logPrefix = " ";
2020-03-30 11:44:18 +00:00
let parent = this.owner;
while(parent) {
2020-12-07 14:07:47 +00:00
if(parent.mode == GroupMode.PREFIX) {
this.logPrefix = this.logPrefix + parent.logPrefix;
2020-12-07 14:07:47 +00:00
} else {
2020-03-30 11:44:18 +00:00
break;
2020-12-07 14:07:47 +00:00
}
2019-03-17 11:15:39 +00:00
}
2019-08-30 21:06:39 +00:00
}
2020-03-30 11:44:18 +00:00
this.initialized = true;
2018-04-16 18:38:35 +00:00
}
2020-12-07 14:07:47 +00:00
if(this.mode == GroupMode.NATIVE) {
2020-03-30 11:44:18 +00:00
logDirect(this.level, message, ...optionalParams);
2020-12-07 14:07:47 +00:00
} else {
logDirect(this.level, "[%s] " + this.logPrefix + message, categoryMapping.get(this.category), ...optionalParams);
2019-03-17 11:15:39 +00:00
}
2020-03-30 11:44:18 +00:00
return this;
}
2019-03-17 11:15:39 +00:00
2020-03-30 11:44:18 +00:00
end() {
if(this.initialized) {
if(this.mode == GroupMode.NATIVE)
console.groupEnd();
2019-03-17 11:15:39 +00:00
}
2020-03-30 11:44:18 +00:00
}
2019-03-17 11:15:39 +00:00
2020-03-30 11:44:18 +00:00
get prefix() : string {
return this.logPrefix;
2020-03-30 11:44:18 +00:00
}
set prefix(prefix: string) {
this.logPrefix = prefix;
2018-04-16 18:38:35 +00:00
}
2019-09-01 15:24:06 +00:00
}
2020-03-30 11:44:18 +00:00
loader.register_task(loader.Stage.JAVASCRIPT_INITIALIZING, {
name: "log enabled initialisation",
function: async () => initialize(__build.mode === "debug" ? LogType.TRACE : LogType.INFO),
2020-03-30 11:44:18 +00:00
priority: 150
});
/* initialize global logging system, use by the loader for example */
window.log = module.exports;