index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { inWorker, isDevBuild } from "@/next/env";
  2. import { logError } from "@ente/shared/sentry";
  3. import isElectron from "is-electron";
  4. import ElectronAPIs from "@/next/electron";
  5. import { workerBridge } from "../worker/worker-bridge";
  6. import { formatLog, logWeb } from "./web";
  7. export const MAX_LOG_SIZE = 5 * 1024 * 1024; // 5MB
  8. export const MAX_LOG_LINES = 1000;
  9. export const logToDisk = (message: string) => {
  10. if (isElectron()) {
  11. ElectronAPIs.logToDisk(message);
  12. } else {
  13. logWeb(message);
  14. }
  15. };
  16. export function addLogLine(
  17. log: string | number | boolean,
  18. ...optionalParams: (string | number | boolean)[]
  19. ) {
  20. try {
  21. const completeLog = [log, ...optionalParams].join(" ");
  22. if (isDevBuild) {
  23. console.log(completeLog);
  24. }
  25. if (inWorker()) {
  26. workerBridge
  27. .logToDisk(completeLog)
  28. .catch((e) =>
  29. console.error(
  30. "Failed to log a message from worker",
  31. e,
  32. "\nThe message was",
  33. completeLog,
  34. ),
  35. );
  36. } else {
  37. logToDisk(completeLog);
  38. }
  39. } catch (e) {
  40. logError(e, "failed to addLogLine", undefined, true);
  41. // ignore
  42. }
  43. }
  44. export const addLocalLog = (getLog: () => string) => {
  45. if (isDevBuild) {
  46. console.log(
  47. formatLog({
  48. logLine: getLog(),
  49. timestamp: Date.now(),
  50. }),
  51. );
  52. }
  53. };