Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: add logging

Ammar Ahmed282705a6830c
diff --git a/apps/mobile/app/common/filesystem/download.js b/apps/mobile/app/common/filesystem/download.js
index 1ff7271e6..84c44d1a6 100644
--- a/apps/mobile/app/common/filesystem/download.js
+++ b/apps/mobile/app/common/filesystem/download.js
@@ -27,16 +27,19 @@ import { cacheDir, fileCheck } from "./utils";
import { createCacheDir, exists } from "./io";
export async function downloadFile(filename, data, cancelToken) {
- if (!data) return false;
+ if (!data) {
+ DatabaseLogger.log(`Error downloading file: ${filename}, reason: No data`);
+ return false;
+ }
- console.log("Downloading", filename);
+ DatabaseLogger.log(`Downloading ${filename}`);
await createCacheDir();
let { url, headers } = data;
let path = `${cacheDir}/${filename}`;
try {
if (await exists(filename)) {
- console.log("Exists already", filename);
+ DatabaseLogger.log(`File Exists already: ${filename}`);
return true;
}
@@ -44,13 +47,24 @@ export async function downloadFile(filename, data, cancelToken) {
method: "GET",
headers
});
- if (!res.ok)
+
+ if (!res.ok) {
+ DatabaseLogger.log(
+ `Error downloading file: ${filename}, ${res.status}, ${res.statusText}, reason: Unable to resolve download url`
+ );
throw new Error(`${res.status}: Unable to resolve download url`);
+ }
+
const downloadUrl = await res.text();
- if (!downloadUrl) throw new Error("Unable to resolve download url");
+ if (!downloadUrl) {
+ DatabaseLogger.log(
+ `Error downloading file: ${filename}, reason: Unable to resolve download url`
+ );
+ throw new Error("Unable to resolve download url");
+ }
let totalSize = 0;
- console.log("Download starting");
+ DatabaseLogger.log(`Download starting: ${filename}`);
let request = RNFetchBlob.config({
path: path,
IOSBackgroundTask: true
@@ -61,13 +75,15 @@ export async function downloadFile(filename, data, cancelToken) {
.getState()
.setProgress(0, total, filename, recieved, "download");
totalSize = total;
- console.log("downloading: ", recieved, total);
+ DatabaseLogger.log(`Downloading: ${filename}, ${recieved}/${total}`);
});
cancelToken.cancel = () => {
useAttachmentStore.getState().remove(filename);
request.cancel();
+ DatabaseLogger.log(`Download cancelled: ${filename}`);
};
+
let response = await request;
await fileCheck(response, totalSize);
let status = response.info().status;
@@ -91,7 +107,10 @@ export async function downloadFile(filename, data, cancelToken) {
useAttachmentStore.getState().remove(filename);
RNFetchBlob.fs.unlink(path).catch(console.log);
- console.log("Download file error:", e, url, headers);
+ DatabaseLogger.error(e, {
+ url,
+ headers
+ });
return false;
}
}
diff --git a/apps/mobile/app/common/filesystem/io.js b/apps/mobile/app/common/filesystem/io.js
index ed405fbfb..613a636fc 100644
--- a/apps/mobile/app/common/filesystem/io.js
+++ b/apps/mobile/app/common/filesystem/io.js
@@ -21,12 +21,12 @@ import Sodium from "@ammarahmed/react-native-sodium";
import { Platform } from "react-native";
import RNFetchBlob from "react-native-blob-util";
import { IOS_APPGROUPID } from "../../utils/constants";
-import { db } from "../database";
+import { DatabaseLogger, db } from "../database";
import { cacheDir, cacheDirOld, getRandomId } from "./utils";
export async function readEncrypted(filename, key, cipherData) {
await migrateFilesFromCache();
- console.log("Read encrypted file...");
+ DatabaseLogger.log("Read encrypted file...");
let path = `${cacheDir}/${filename}`;
try {
@@ -34,8 +34,6 @@ export async function readEncrypted(filename, key, cipherData) {
return false;
}
- const attachment = await db.attachments.attachment(filename);
- console.log("decrypting....");
let output = await Sodium.decryptFile(
key,
{
@@ -45,12 +43,13 @@ export async function readEncrypted(filename, key, cipherData) {
},
cipherData.outputType === "base64" ? "base64" : "text"
);
- console.log("file decrypted...", attachment?.mimeType);
+
+ DatabaseLogger.log("File decrypted...");
return output;
} catch (e) {
RNFetchBlob.fs.unlink(path).catch(console.log);
- console.log("readEncrypted", e);
+ DatabaseLogger.error(e);
return false;
}
}
@@ -128,7 +127,7 @@ export async function clearFileStorage() {
export async function createCacheDir() {
if (!(await RNFetchBlob.fs.exists(cacheDir))) {
await RNFetchBlob.fs.mkdir(cacheDir);
- console.log("Cache directory created");
+ DatabaseLogger.log("Cache directory created");
}
}
@@ -188,6 +187,9 @@ export async function exists(filename) {
);
if (stat.size !== expectedFileSize) {
+ DatabaseLogger.log(
+ `File size mismatch: ${filename}, expected: ${expectedFileSize}, actual: ${stat.size}`
+ );
RNFetchBlob.fs
.unlink(existsInAppGroup ? appGroupPath : path)
.catch(console.log);
diff --git a/apps/mobile/app/components/sheets/migrate/index.tsx b/apps/mobile/app/components/sheets/migrate/index.tsx
index 40a7392bb..f7af246ea 100644
--- a/apps/mobile/app/components/sheets/migrate/index.tsx
+++ b/apps/mobile/app/components/sheets/migrate/index.tsx
@@ -96,10 +96,12 @@ export default function Migrate() {
});
setLoading(true);
await sleep(1);
- const { error } = await BackupService.run(false, "local");
+ const { error, report } = await BackupService.run(false, "local");
if (error) {
ToastManager.error(error, "Backup failed");
- reportError(error);
+ if (report) {
+ reportError(error);
+ }
setLoading(false);
return;
}
diff --git a/apps/mobile/app/services/backup.js b/apps/mobile/app/services/backup.js
index e16712d07..ae22ae6e6 100644
--- a/apps/mobile/app/services/backup.js
+++ b/apps/mobile/app/services/backup.js
@@ -31,7 +31,6 @@ import { eCloseSheet } from "../utils/events";
import { sleep } from "../utils/time";
import { ToastManager, eSendEvent, presentSheet } from "./event-manager";
import SettingsService from "./settings";
-import { useUserStore } from "../stores/use-user-store";
const MS_DAY = 86400000;
const MS_WEEK = MS_DAY * 7;
@@ -152,13 +151,14 @@ async function updateNextBackupTime() {
/**
* @param {boolean=} progress
* @param {string=} context
- * @returns {Promise<{path?: string, error?: Error}}>
+ * @returns {Promise<{path?: string, error?: Error, report?: boolean}}>
*/
async function run(progress = false, context) {
let androidBackupDirectory = await checkBackupDirExists(false, context);
if (!androidBackupDirectory)
return {
- error: new Error("Backup directory not selected")
+ error: new Error("Backup directory not selected"),
+ report: false
};
if (progress) {
@@ -259,11 +259,12 @@ async function run(progress = false, context) {
return run(progress, context);
}
- DatabaseLogger.error(e, "Backup failed");
+ DatabaseLogger.error(e);
await sleep(300);
progress && eSendEvent(eCloseSheet);
return {
- error: e
+ error: e,
+ report: true
};
}
}