mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
refactor console imports, added config update event
This commit is contained in:
127
app/src/core/utils/console.ts
Normal file
127
app/src/core/utils/console.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { datetimeStringLocal } from "core/utils";
|
||||
import colors from "picocolors";
|
||||
import { env } from "core";
|
||||
|
||||
function hasColors() {
|
||||
try {
|
||||
// biome-ignore lint/style/useSingleVarDeclarator: <explanation>
|
||||
const p = process || {},
|
||||
argv = p.argv || [],
|
||||
env = p.env || {};
|
||||
return (
|
||||
!(!!env.NO_COLOR || argv.includes("--no-color")) &&
|
||||
(!!env.FORCE_COLOR ||
|
||||
argv.includes("--color") ||
|
||||
p.platform === "win32" ||
|
||||
// biome-ignore lint/complexity/useOptionalChain: <explanation>
|
||||
((p.stdout || {}).isTTY && env.TERM !== "dumb") ||
|
||||
!!env.CI)
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const __consoles = {
|
||||
critical: {
|
||||
prefix: "CRT",
|
||||
color: colors.red,
|
||||
args_color: colors.red,
|
||||
original: console.error,
|
||||
},
|
||||
error: {
|
||||
prefix: "ERR",
|
||||
color: colors.red,
|
||||
args_color: colors.red,
|
||||
original: console.error,
|
||||
},
|
||||
warn: {
|
||||
prefix: "WRN",
|
||||
color: colors.yellow,
|
||||
args_color: colors.yellow,
|
||||
original: console.warn,
|
||||
},
|
||||
info: {
|
||||
prefix: "INF",
|
||||
color: colors.cyan,
|
||||
original: console.info,
|
||||
},
|
||||
log: {
|
||||
prefix: "LOG",
|
||||
color: colors.dim,
|
||||
args_color: colors.dim,
|
||||
original: console.log,
|
||||
},
|
||||
debug: {
|
||||
prefix: "DBG",
|
||||
color: colors.yellow,
|
||||
args_color: colors.dim,
|
||||
original: console.debug,
|
||||
},
|
||||
} as const;
|
||||
|
||||
function __tty(_type: any, args: any[]) {
|
||||
const has = hasColors();
|
||||
const cons = __consoles[_type];
|
||||
const prefix = cons.color(`[${cons.prefix}]`);
|
||||
const _args = args.map((a) =>
|
||||
"args_color" in cons && has && typeof a === "string" ? cons.args_color(a) : a,
|
||||
);
|
||||
return cons.original(prefix, colors.gray(datetimeStringLocal()), ..._args);
|
||||
}
|
||||
|
||||
export type TConsoleSeverity = keyof typeof __consoles;
|
||||
declare global {
|
||||
var __consoleConfig:
|
||||
| {
|
||||
level: TConsoleSeverity;
|
||||
id?: string;
|
||||
}
|
||||
| undefined;
|
||||
}
|
||||
|
||||
// Ensure the config exists only once globally
|
||||
const defaultLevel = env("cli_log_level", "log") as TConsoleSeverity;
|
||||
|
||||
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
|
||||
const config = (globalThis.__consoleConfig ??= {
|
||||
level: defaultLevel,
|
||||
//id: crypto.randomUUID(), // for debugging
|
||||
});
|
||||
|
||||
const keys = Object.keys(__consoles);
|
||||
export const $console = new Proxy(config as any, {
|
||||
get: (_, prop) => {
|
||||
switch (prop) {
|
||||
case "original":
|
||||
return console;
|
||||
case "setLevel":
|
||||
return (l: TConsoleSeverity) => {
|
||||
config.level = l;
|
||||
};
|
||||
case "resetLevel":
|
||||
return () => {
|
||||
config.level = defaultLevel;
|
||||
};
|
||||
}
|
||||
|
||||
const current = keys.indexOf(config.level);
|
||||
const requested = keys.indexOf(prop as string);
|
||||
|
||||
if (prop in __consoles && requested <= current) {
|
||||
return (...args: any[]) => __tty(prop, args);
|
||||
}
|
||||
return () => null;
|
||||
},
|
||||
}) as typeof console & {
|
||||
original: typeof console;
|
||||
} & {
|
||||
setLevel: (l: TConsoleSeverity) => void;
|
||||
resetLevel: () => void;
|
||||
};
|
||||
|
||||
export function colorizeConsole(con: typeof console) {
|
||||
for (const [key] of Object.entries(__consoles)) {
|
||||
con[key] = $console[key];
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { extension, guess, isMimeType } from "media/storage/mime-types-tiny";
|
||||
import { randomString } from "core/utils/strings";
|
||||
import type { Context } from "hono";
|
||||
import { invariant } from "core/utils/runtime";
|
||||
import { $console } from "../console";
|
||||
import { $console } from "./console";
|
||||
|
||||
export function getContentName(request: Request): string | undefined;
|
||||
export function getContentName(contentDisposition: string): string | undefined;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./console";
|
||||
export * from "./browser";
|
||||
export * from "./objects";
|
||||
export * from "./strings";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { $console } from "core";
|
||||
import { $console } from "./console";
|
||||
|
||||
type ConsoleSeverity = "log" | "warn" | "error";
|
||||
const _oldConsoles = {
|
||||
@@ -36,14 +36,14 @@ export function disableConsoleLog(severities: ConsoleSeverity[] = ["log", "warn"
|
||||
severities.forEach((severity) => {
|
||||
console[severity] = () => null;
|
||||
});
|
||||
$console.setLevel("critical");
|
||||
$console?.setLevel("critical");
|
||||
}
|
||||
|
||||
export function enableConsoleLog() {
|
||||
Object.entries(_oldConsoles).forEach(([severity, fn]) => {
|
||||
console[severity as ConsoleSeverity] = fn;
|
||||
});
|
||||
$console.resetLevel();
|
||||
$console?.resetLevel();
|
||||
}
|
||||
|
||||
export function formatMemoryUsage() {
|
||||
|
||||
Reference in New Issue
Block a user