// @ts-nocheck /* ============================================================================ - TAON READONLY SOURCE - DO NOT MODIFY THIS FILE. - Changes may be overwritten. - Use command: taon build:watch:lib inside ng2-logger project repository for watch development of third party modules like this. * ========================================================================== */ import * as stringify from 'json-stringify-safe'; import { Helpers, json5, UtilsOs } from 'tnp-core/websql'; export enum Level { DATA, INFO, WARN, ERROR, SUCCESS, TASK_STARTED, TASK_DONE } export const LevelKey = { [Level.DATA]: 'data', [Level.INFO]: 'info', [Level.WARN]: 'warn', [Level.ERROR]: 'error', [Level.SUCCESS]: 'success', [Level.TASK_STARTED]: 'taskstarted', [Level.TASK_DONE]: 'taskdone', }; export const LevelOrder = [ LevelKey[Level.DATA], LevelKey[Level.TASK_STARTED], LevelKey[Level.TASK_DONE], LevelKey[Level.INFO], LevelKey[Level.SUCCESS], LevelKey[Level.WARN], LevelKey[Level.ERROR], ]; const levelIcon = { [Level.INFO]: 'ℹ️', [Level.ERROR]: '❌', [Level.WARN]: '⚠️', [Level.SUCCESS]: '✅', [Level.TASK_STARTED]: '🚀', [Level.TASK_DONE]: '🏁', [Level.DATA]: '', }; export namespace Log { const instances = new Map(); let originalConsole: Partial | null = null; let permanentlyDisabled = false; const consoleMethods = ['log', 'info', 'warn', 'error'] as const; let globalLevel: Level | null = null; export const settings = { mutedModules: [] as string[], showTimestamp: false, }; const isMutedModule = (moduleName: string): boolean => { if (settings.mutedModules.length === 0) return false; return !Helpers.contain(settings.mutedModules, moduleName); }; export const create = (moduleName: string, ...level: Level[]): Logger => { const log = new Logger(moduleName, getRandomColor(), level, isMutedModule(moduleName), undefined); instances.set(moduleName, log); return log; }; export const setGlobalLevel = (level: Level) => { globalLevel = level; }; export const onlyLevel = (...levels: Level[]): void => { instances.forEach(instance => { instance.allowed = levels; }); }; export const onlyModules = (...modules: (string | RegExp)[]): void => { instances.forEach(instance => { const moduleName = instance.moduleName; const isAllowed = modules.some(m => { if (typeof m === 'string') { return m === moduleName; } if (m instanceof RegExp) { return m.test(moduleName); } return false; }); instance.isMuted = !isAllowed; }); }; export const disableAllLogsPermanetly = () => { if (permanentlyDisabled) return; if (!originalConsole) { originalConsole = {}; consoleMethods.forEach(m => { originalConsole![m] = console[m]; }); } consoleMethods.forEach(m => { console[m] = () => { }; }); permanentlyDisabled = true; }; export const disableAllLogs = () => { if (permanentlyDisabled) return; if (!originalConsole) { originalConsole = {}; consoleMethods.forEach(m => { originalConsole![m] = console[m]; }); } consoleMethods.forEach(m => { console[m] = () => { }; }); }; export const enableAllLogs = () => { if (permanentlyDisabled) return; if (!originalConsole) return; consoleMethods.forEach(m => { if (originalConsole && originalConsole[m]) { console[m] = originalConsole[m] as any; } }); }; class Logger { constructor(public readonly moduleName: string, public color: string, public allowed: Level[], public isMuted: boolean, public moduleWidth: number | undefined) { this.createLevelMethod(Level.DATA); this.createLevelMethod(Level.DATA, 'd'); this.createLevelMethod(Level.INFO); this.createLevelMethod(Level.INFO, 'i'); this.createLevelMethod(Level.WARN); this.createLevelMethod(Level.WARN, 'w'); this.createLevelMethod(Level.ERROR); this.createLevelMethod(Level.ERROR, 'er'); this.createLevelMethod(Level.SUCCESS); this.createLevelMethod(Level.TASK_STARTED); this.createLevelMethod(Level.TASK_DONE); } declare data: (message: string, ...data: any[]) => void; declare d: (message: string, ...data: any[]) => void; declare info: (message: string, ...data: any[]) => void; declare i: (message: string, ...data: any[]) => void; declare warn: (message: string, ...data: any[]) => void; declare w: (message: string, ...data: any[]) => void; declare error: (message: string, ...data: any[]) => void; declare er: (message: string, ...data: any[]) => void; public setLevel(l: Level) { this.allowed = [l]; return this; } public mute() { this.isMuted = true; return this; } public logOnlyWhen(expression: (() => boolean) | boolean): void { this.isMuted = typeof expression === 'function' ? !expression() : !expression; } private createLevelMethod(level: Level, overrideMethodName?: string) { const methodName = overrideMethodName ? overrideMethodName : LevelKey[level]; (this as any)[methodName] = (message: string, ...data: any[]) => { if (this.isMuted) return this; if (globalLevel !== null && level < globalLevel) { return this; } if (this.allowed.length === 0 || Helpers.contain(this.allowed, level)) { displayMsg(message, data.length === 0 ? undefined : data, this.moduleName, this.color, level, this.moduleWidth, permanentlyDisabled); } return this; }; } } } const getRandomColor = (): string => { const letters = '0123456789ABCDEF'; return ('#' + Array.from({ length: 6 }) .map(() => letters[Math.floor(Math.random() * 16)]) .join('')); }; const consoleLog = (data: string, level: Level): void => { }; const displayMsg = (message: string, params: any[] | undefined, moduleName: string, moduleColor: string, level: Level, moduleWidth: number | undefined, permanentlyDisabled: boolean): void => { if (permanentlyDisabled) return; if (message) { message = `${levelIcon[level]} ${message?.toString()}`; } else { message = levelIcon[level]; } const timestamp = Log.settings.showTimestamp ? `[${new Date().toISOString()}] ` : ''; if (moduleWidth) { const diff = moduleWidth - moduleName.length; if (diff > 0) { for (let i = 0; i < diff; i++) { moduleName += ' '; } } } if (UtilsOs.isBrowser) { let levelColor = 'gray'; switch (level) { case Level.INFO: levelColor = 'deepskyblue'; break; case Level.WARN: levelColor = 'orange'; break; case Level.ERROR: levelColor = 'red'; break; case Level.SUCCESS: levelColor = 'green'; break; case Level.TASK_STARTED: levelColor = 'purple'; break; case Level.TASK_DONE: levelColor = 'mediumseagreen'; break; case Level.DATA: default: levelColor = 'gray'; } const moduleStyle = `background:${moduleColor};color:white;padding:2px 4px;border-radius:2px;`; const messageStyle = `color:${levelColor};font-weight:${level === Level.ERROR ? 'bold' : 'normal'};`; console.log(`%c ${moduleName} %c ${timestamp}${message}`, moduleStyle, messageStyle, ...(params ?? [])); } };