Skip to content

Commit

Permalink
feat(Log)!: Convert Log class to module (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue authored Nov 22, 2024
1 parent 9cf0d3f commit fc55716
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 58 deletions.
7 changes: 4 additions & 3 deletions src/parser/helpers.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -62,8 +62,9 @@ export class YTNode {
}
}

const MAYBE_TAG = 'Maybe';

export class Maybe {
#TAG = 'Maybe';
readonly #value;

constructor (value: any) {
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions src/platform/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<IDBDatabase>((resolve, reject) => {
const request = indexedDB.open('youtubei.js', 1);
Expand Down
97 changes: 48 additions & 49 deletions src/utils/Log.ts
Original file line number Diff line number Diff line change
@@ -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<string>();

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;
}
}
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<string>();

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;
}
2 changes: 1 addition & 1 deletion src/utils/StreamingInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit fc55716

Please sign in to comment.