Allow disabling localStorage logs

This commit is contained in:
Manav Rathi 2024-05-10 11:14:45 +05:30
parent 1b3e11f713
commit ff9a286910
No known key found for this signature in database
3 changed files with 25 additions and 9 deletions

View file

@ -1,4 +1,5 @@
import { CustomHead } from "@/next/components/Head"; import { CustomHead } from "@/next/components/Head";
import { disableDiskLogs } from "@/next/log";
import { logUnhandledErrorsAndRejections } from "@/next/log-web"; import { logUnhandledErrorsAndRejections } from "@/next/log-web";
import { APPS, APP_TITLES } from "@ente/shared/apps/constants"; import { APPS, APP_TITLES } from "@ente/shared/apps/constants";
import { getTheme } from "@ente/shared/themes"; import { getTheme } from "@ente/shared/themes";
@ -11,6 +12,7 @@ import "styles/global.css";
export default function App({ Component, pageProps }: AppProps) { export default function App({ Component, pageProps }: AppProps) {
useEffect(() => { useEffect(() => {
disableDiskLogs();
logUnhandledErrorsAndRejections(true); logUnhandledErrorsAndRejections(true);
return () => logUnhandledErrorsAndRejections(false); return () => logUnhandledErrorsAndRejections(false);
}, []); }, []);

View file

@ -46,7 +46,6 @@ export default function Index() {
return; return;
} }
log.info("Pairing complete");
storeCastData(data); storeCastData(data);
await router.push("/slideshow"); await router.push("/slideshow");
} catch (e) { } catch (e) {

View file

@ -3,6 +3,19 @@ import { isDevBuild } from "./env";
import { logToDisk as webLogToDisk } from "./log-web"; import { logToDisk as webLogToDisk } from "./log-web";
import { workerBridge } from "./worker/worker-bridge"; import { workerBridge } from "./worker/worker-bridge";
/**
* Whether logs go to disk or are always emitted to the console.
*/
let shouldLogToDisk = true;
/**
* By default, logs get saved into a ring buffer in the browser's local storage.
* However, in some contexts, e.g. when we're running as the cast app, there is
* no mechanism for the user to retrieve these logs. So this function exists as
* a way to disable the on disk logging and always use the console.
*/
export const disableDiskLogs = () => (shouldLogToDisk = false);
/** /**
* Write a {@link message} to the on-disk log. * Write a {@link message} to the on-disk log.
* *
@ -46,13 +59,13 @@ const messageWithError = (message: string, e?: unknown) => {
const logError = (message: string, e?: unknown) => { const logError = (message: string, e?: unknown) => {
const m = `[error] ${messageWithError(message, e)}`; const m = `[error] ${messageWithError(message, e)}`;
console.error(m); console.error(m);
logToDisk(m); if (shouldLogToDisk) logToDisk(m);
}; };
const logWarn = (message: string, e?: unknown) => { const logWarn = (message: string, e?: unknown) => {
const m = `[warn] ${messageWithError(message, e)}`; const m = `[warn] ${messageWithError(message, e)}`;
console.error(m); console.error(m);
logToDisk(m); if (shouldLogToDisk) logToDisk(m);
}; };
const logInfo = (...params: unknown[]) => { const logInfo = (...params: unknown[]) => {
@ -60,8 +73,8 @@ const logInfo = (...params: unknown[]) => {
.map((p) => (typeof p == "string" ? p : JSON.stringify(p))) .map((p) => (typeof p == "string" ? p : JSON.stringify(p)))
.join(" "); .join(" ");
const m = `[info] ${message}`; const m = `[info] ${message}`;
if (isDevBuild) console.log(m); if (isDevBuild || !shouldLogToDisk) console.log(m);
logToDisk(m); if (shouldLogToDisk) logToDisk(m);
}; };
const logDebug = (param: () => unknown) => { const logDebug = (param: () => unknown) => {
@ -71,8 +84,8 @@ const logDebug = (param: () => unknown) => {
/** /**
* Ente's logger. * Ente's logger.
* *
* This is an object that provides three functions to log at the corresponding * This is an object that provides functions to log at the corresponding levels:
* levels - error, info or debug. * error, warn, info or debug.
* *
* Whenever we need to save a log message to disk, * Whenever we need to save a log message to disk,
* *
@ -103,8 +116,10 @@ export default {
* This is meant as a replacement of {@link console.log}, and takes an * This is meant as a replacement of {@link console.log}, and takes an
* arbitrary number of arbitrary parameters that it then serializes. * arbitrary number of arbitrary parameters that it then serializes.
* *
* The log is written to disk. In development builds, the log is also * The log is written to disk. However, if logging to disk is disabled by
* printed to the browser console. * using {@link disableDiskLogs}, then the log is printed to the console.
*
* In development builds, the log is always printed to the browser console.
*/ */
info: logInfo, info: logInfo,
/** /**