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

View file

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

View file

@ -3,6 +3,19 @@ import { isDevBuild } from "./env";
import { logToDisk as webLogToDisk } from "./log-web";
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.
*
@ -46,13 +59,13 @@ const messageWithError = (message: string, e?: unknown) => {
const logError = (message: string, e?: unknown) => {
const m = `[error] ${messageWithError(message, e)}`;
console.error(m);
logToDisk(m);
if (shouldLogToDisk) logToDisk(m);
};
const logWarn = (message: string, e?: unknown) => {
const m = `[warn] ${messageWithError(message, e)}`;
console.error(m);
logToDisk(m);
if (shouldLogToDisk) logToDisk(m);
};
const logInfo = (...params: unknown[]) => {
@ -60,8 +73,8 @@ const logInfo = (...params: unknown[]) => {
.map((p) => (typeof p == "string" ? p : JSON.stringify(p)))
.join(" ");
const m = `[info] ${message}`;
if (isDevBuild) console.log(m);
logToDisk(m);
if (isDevBuild || !shouldLogToDisk) console.log(m);
if (shouldLogToDisk) logToDisk(m);
};
const logDebug = (param: () => unknown) => {
@ -71,8 +84,8 @@ const logDebug = (param: () => unknown) => {
/**
* Ente's logger.
*
* This is an object that provides three functions to log at the corresponding
* levels - error, info or debug.
* This is an object that provides functions to log at the corresponding levels:
* error, warn, info or debug.
*
* 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
* arbitrary number of arbitrary parameters that it then serializes.
*
* The log is written to disk. In development builds, the log is also
* printed to the browser console.
* The log is written to disk. However, if logging to disk is disabled by
* 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,
/**