From eab5bccb99a48b71931c315a3f3d9e3fd12fe8ec Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Mon, 20 May 2024 15:03:57 +0500 Subject: [PATCH] mobile: use sqlite based logger --- apps/mobile/app/app.tsx | 42 ++++++++++++---------- apps/mobile/app/common/database/index.js | 8 ++++- apps/mobile/app/common/database/logger.js | 29 ++++++++++----- apps/mobile/app/screens/settings/debug.tsx | 3 +- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/apps/mobile/app/app.tsx b/apps/mobile/app/app.tsx index 501b875c5a..5329b3bbc9 100644 --- a/apps/mobile/app/app.tsx +++ b/apps/mobile/app/app.tsx @@ -16,15 +16,16 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import "react-native-gesture-handler"; import { THEME_COMPATIBILITY_VERSION, useThemeEngineStore } from "@notesnook/theme"; import React, { useEffect } from "react"; import { I18nManager, View } from "react-native"; +import "react-native-gesture-handler"; import { GestureHandlerRootView } from "react-native-gesture-handler"; import { SafeAreaProvider } from "react-native-safe-area-context"; +import { initializeLogger } from "./common/database/logger"; import AppLockedOverlay from "./components/app-lock-overlay"; import { withErrorBoundry } from "./components/exception-handler"; import GlobalSafeAreaProvider from "./components/globalsafearea"; @@ -44,23 +45,28 @@ I18nManager.swapLeftAndRightInRTL(false); const App = () => { const init = useAppEvents(); useEffect(() => { - const { appLockEnabled, appLockMode } = SettingsService.get(); - if (appLockEnabled || appLockMode !== "none") { - useUserStore.getState().lockApp(true); - } - - //@ts-ignore - globalThis["IS_MAIN_APP_RUNNING"] = true; - init(); - setTimeout(async () => { - SettingsService.onFirstLaunch(); - await Notifications.get(); - if (SettingsService.get().notifNotes) { - Notifications.pinQuickNote(true); - } - TipManager.init(); - }, 100); - + initializeLogger() + .catch((e) => { + console.log(e); + }) + .finally(() => { + const { appLockEnabled, appLockMode } = SettingsService.get(); + if (appLockEnabled || appLockMode !== "none") { + useUserStore.getState().lockApp(true); + } + + //@ts-ignore + globalThis["IS_MAIN_APP_RUNNING"] = true; + init(); + setTimeout(async () => { + SettingsService.onFirstLaunch(); + await Notifications.get(); + if (SettingsService.get().notifNotes) { + Notifications.pinQuickNote(true); + } + TipManager.init(); + }, 100); + }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/apps/mobile/app/common/database/index.js b/apps/mobile/app/common/database/index.js index 434943a36a..375934bd79 100644 --- a/apps/mobile/app/common/database/index.js +++ b/apps/mobile/app/common/database/index.js @@ -85,4 +85,10 @@ export async function setupDatabase(password) { } export const db = database; -export const DatabaseLogger = dbLogger; +let DatabaseLogger = dbLogger.scope(Platform.OS); + +const setLogger = () => { + DatabaseLogger = dbLogger.scope(Platform.OS); +}; + +export { DatabaseLogger, setLogger }; diff --git a/apps/mobile/app/common/database/logger.js b/apps/mobile/app/common/database/logger.js index aed3bfa5e2..2a86a163fc 100644 --- a/apps/mobile/app/common/database/logger.js +++ b/apps/mobile/app/common/database/logger.js @@ -16,14 +16,27 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import { MMKVLoader } from "react-native-mmkv-storage"; -import { initialize } from "@notesnook/core/dist/logger"; -import { KV } from "./storage"; -const LoggerStorage = new MMKVLoader() - .withInstanceID("notesnook_logs") - .initialize(); +import { initialize } from "@notesnook/core/dist/logger"; +import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from "kysely"; +import { Platform } from "react-native"; +import { setLogger } from "."; +import { RNSqliteDriver } from "./sqlite.kysely"; -initialize(new KV(LoggerStorage)); +const initializeLogger = async () => { + await initialize({ + dialect: (name) => ({ + createDriver: () => { + return new RNSqliteDriver({ async: true, dbName: name }); + }, + createAdapter: () => new SqliteAdapter(), + createIntrospector: (db) => new SqliteIntrospector(db), + createQueryCompiler: () => new SqliteQueryCompiler() + }), + tempStore: "memory", + journalMode: Platform.OS === "ios" ? "DELETE" : "WAL" + }); + setLogger(); +}; -export { LoggerStorage }; +export { initializeLogger }; diff --git a/apps/mobile/app/screens/settings/debug.tsx b/apps/mobile/app/screens/settings/debug.tsx index e709a87958..23a438d7e1 100644 --- a/apps/mobile/app/screens/settings/debug.tsx +++ b/apps/mobile/app/screens/settings/debug.tsx @@ -122,7 +122,8 @@ export default function DebugLogs() { colors.primary.paragraph, colors.error.paragraph, colors.static.black, - colors.static.orange + colors.static.orange, + colors.primary.border ] );