From 3f15de7aa3a8510e358e1b95b7b606ee8fede574 Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Thu, 21 Nov 2024 23:25:10 +0100 Subject: [PATCH] feat(Log)!: Convert Log class to module --- src/parser/helpers.ts | 7 +-- src/platform/web.ts | 7 +-- src/utils/Log.ts | 97 +++++++++++++++++++------------------- src/utils/StreamingInfo.ts | 2 +- src/utils/Utils.ts | 2 +- src/utils/index.ts | 2 +- 6 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/parser/helpers.ts b/src/parser/helpers.ts index 0e81575b0..dc5dc8d63 100644 --- a/src/parser/helpers.ts +++ b/src/parser/helpers.ts @@ -1,4 +1,4 @@ -import Log from '../utils/Log.js'; +import * as Log from '../utils/Log.js'; import { deepCompare, ParsingError } from '../utils/Utils.js'; const isObserved = Symbol('ObservedArray.isObserved'); @@ -62,8 +62,9 @@ export class YTNode { } } +const MAYBE_TAG = 'Maybe'; + export class Maybe { - #TAG = 'Maybe'; readonly #value; constructor (value: any) { @@ -278,7 +279,7 @@ export class Maybe { * This call is not meant to be used outside of debugging. Please use the specific type getter instead. */ any(): any { - Log.warn(this.#TAG, 'This call is not meant to be used outside of debugging. Please use the specific type getter instead.'); + Log.warn(MAYBE_TAG, 'This call is not meant to be used outside of debugging. Please use the specific type getter instead.'); return this.#value; } diff --git a/src/platform/web.ts b/src/platform/web.ts index 9e3874905..4dadbb232 100644 --- a/src/platform/web.ts +++ b/src/platform/web.ts @@ -4,10 +4,11 @@ import { Platform } from '../utils/Utils.js'; import sha1Hash from './polyfills/web-crypto.js'; import package_json from '../../package.json' assert { type: 'json' }; import evaluate from './jsruntime/jinter.js'; -import Log from '../utils/Log.js'; +import * as Log from '../utils/Log.js'; + +const CACHE_TAG = 'Cache'; class Cache implements ICache { - #TAG = 'Cache'; #persistent_directory: string; #persistent: boolean; @@ -23,7 +24,7 @@ class Cache implements ICache { #getBrowserDB() { const indexedDB: IDBFactory = Reflect.get(globalThis, 'indexedDB') || Reflect.get(globalThis, 'webkitIndexedDB') || Reflect.get(globalThis, 'mozIndexedDB') || Reflect.get(globalThis, 'msIndexedDB'); - if (!indexedDB) return Log.warn(this.#TAG, 'IndexedDB is not supported. No cache will be used.'); + if (!indexedDB) return Log.warn(CACHE_TAG, 'IndexedDB is not supported. No cache will be used.'); return new Promise((resolve, reject) => { const request = indexedDB.open('youtubei.js', 1); diff --git a/src/utils/Log.ts b/src/utils/Log.ts index 83b237b62..105203fa0 100644 --- a/src/utils/Log.ts +++ b/src/utils/Log.ts @@ -1,49 +1,48 @@ -export default class Log { - static #YTJS_TAG = 'YOUTUBEJS'; - - public static Level = { - NONE: 0, - ERROR: 1, - WARNING: 2, - INFO: 3, - DEBUG: 4 - }; - - static #log_map_ = { - [Log.Level.ERROR]: (...args: any[]) => console.error(...args), - [Log.Level.WARNING]: (...args: any[]) => console.warn(...args), - [Log.Level.INFO]: (...args: any[]) => console.info(...args), - [Log.Level.DEBUG]: (...args: any[]) => console.debug(...args) - }; - - static #log_level_ = [ Log.Level.WARNING ]; - static #one_time_warnings_issued_ = new Set(); - - static warnOnce = (id: string, ...args: any[]) => { - if (this.#one_time_warnings_issued_.has(id)) - return; - this.#doLog(Log.Level.WARNING, id, args); - this.#one_time_warnings_issued_.add(id); - }; - - static warn = (tag?: string, ...args: any[]) => this.#doLog(Log.Level.WARNING, tag, args); - static error = (tag?: string, ...args: any[]) => this.#doLog(Log.Level.ERROR, tag, args); - static info = (tag?: string, ...args: any[]) => this.#doLog(Log.Level.INFO, tag, args); - static debug = (tag?: string, ...args: any[]) => this.#doLog(Log.Level.DEBUG, tag, args); - - static #doLog(level: number, tag?: string, args?: any[]) { - if (!this.#log_map_ [level] || !this.#log_level_.includes(level)) - return; - - const tags = [ `[${this.#YTJS_TAG}]` ]; - - if (tag) - tags.push(`[${tag}]`); - - this.#log_map_ [level](`${tags.join('')}:`, ...(args || [])); - } - - static setLevel(...args: number[]) { - this.#log_level_ = args; - } -} \ No newline at end of file +const YTJS_TAG = 'YOUTUBEJS'; + +export const Level = { + NONE: 0, + ERROR: 1, + WARNING: 2, + INFO: 3, + DEBUG: 4 +}; + +const log_map = { + [Level.ERROR]: (...args: any[]) => console.error(...args), + [Level.WARNING]: (...args: any[]) => console.warn(...args), + [Level.INFO]: (...args: any[]) => console.info(...args), + [Level.DEBUG]: (...args: any[]) => console.debug(...args) +}; + +let log_level = [ Level.WARNING ]; +const one_time_warnings_issued = new Set(); + +function doLog(level: number, tag?: string, args?: any[]) { + if (!log_map[level] || !log_level.includes(level)) + return; + + const tags = [ `[${YTJS_TAG}]` ]; + + if (tag) + tags.push(`[${tag}]`); + + log_map[level](`${tags.join('')}:`, ...(args || [])); +} + +export const warnOnce = (id: string, ...args: any[]) => { + if (one_time_warnings_issued.has(id)) + return; + + doLog(Level.WARNING, id, args); + one_time_warnings_issued.add(id); +}; + +export const warn = (tag?: string, ...args: any[]) => doLog(Level.WARNING, tag, args); +export const error = (tag?: string, ...args: any[]) => doLog(Level.ERROR, tag, args); +export const info = (tag?: string, ...args: any[]) => doLog(Level.INFO, tag, args); +export const debug = (tag?: string, ...args: any[]) => doLog(Level.DEBUG, tag, args); + +export function setLevel(...args: number[]) { + log_level = args; +} diff --git a/src/utils/StreamingInfo.ts b/src/utils/StreamingInfo.ts index 464ec5a06..85eb566a7 100644 --- a/src/utils/StreamingInfo.ts +++ b/src/utils/StreamingInfo.ts @@ -2,7 +2,7 @@ import type { StoryboardData } from '../parser/classes/PlayerStoryboardSpec.js'; import PlayerStoryboardSpec from '../parser/classes/PlayerStoryboardSpec.js'; import { getStringBetweenStrings, InnertubeError, Platform } from './Utils.js'; import * as Constants from './Constants.js'; -import Log from './Log.js'; +import * as Log from './Log.js'; import type Actions from '../core/Actions.js'; import type Player from '../core/Player.js'; diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index ebafd53ba..0c960c68f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,6 +1,6 @@ import { Memo } from '../parser/helpers.js'; import { Text } from '../parser/misc.js'; -import Log from './Log.js'; +import * as Log from './Log.js'; import userAgents from './user-agents.js'; import { Jinter } from 'jintr'; diff --git a/src/utils/index.ts b/src/utils/index.ts index 1787ee168..d9ccc4395 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -12,7 +12,7 @@ export * from './HTTPClient.js'; export { Platform } from './Utils.js'; export * as Utils from './Utils.js'; -export { default as Log } from './Log.js'; +export * as Log from './Log.js'; export * as LZW from './LZW.js'; export * as ProtoUtils from './ProtoUtils.js'; \ No newline at end of file