Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: write memory logs to a file on Electron #5893

Merged
merged 17 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"adm-zip": "^0.5.10",
"electron-context-menu": "^3.5.0",
"electron-is-dev": "^2.0.0",
"electron-log": "^4.4.8",
"electron-log": "5.2.0",
"electron-store": "^8.2.0",
"electron-updater": "6.1.8",
"keytar": "^7.9.0",
Expand Down
9 changes: 7 additions & 2 deletions apps/desktop/src-electron/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { EventEmitter } from 'events';
import os from 'os';
import * as path from 'path';
import { format as formatUrl } from 'url';

Expand All @@ -18,7 +17,7 @@
} from 'electron';
import contextMenu from 'electron-context-menu';
import isDev from 'electron-is-dev';
import logger from 'electron-log';
import logger from 'electron-log/main';

import {
ONEKEY_APP_DEEP_LINK_NAME,
Expand All @@ -29,7 +28,7 @@
IDesktopAppState,
IDesktopSubModuleInitParams,
IMediaType,
IPrefType,

Check warning on line 31 in apps/desktop/src-electron/app.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

'IPrefType' is defined but never used
} from '@onekeyhq/shared/types/desktop';

import appDevOnlyApi from './appDevOnlyApi';
Expand All @@ -42,6 +41,8 @@
import { parseContentPList } from './libs/utils';
import initProcess, { restartBridge } from './process';

logger.initialize();

// https://github.com/sindresorhus/electron-context-menu
const disposeContextMenu = contextMenu({
showSaveImageAs: true,
Expand Down Expand Up @@ -610,6 +611,10 @@
systemIdleHandler(setIdleTime, event);
});

ipcMain.on(ipcMessageKeys.APP_OPEN_LOGGER_FILE, () => {
void shell.openPath(path.dirname(logger.transports.file.getFile().path));
});

huhuanming marked this conversation as resolved.
Show resolved Hide resolved
ipcMain.on(ipcMessageKeys.CLEAR_WEBVIEW_CACHE, () => {
void session.defaultSession.clearStorageData({
storages: ['cookies', 'cachestorage'],
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src-electron/appNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Notification,
app,
ipcMain,
nativeImage,

Check warning on line 5 in apps/desktop/src-electron/appNotification.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

'nativeImage' is defined but never used
systemPreferences,
} from 'electron';
import logger from 'electron-log';
import logger from 'electron-log/main';
import TaskBarBadgeWindows from 'electron-taskbar-badge';
import { isNil } from 'lodash';

Expand Down Expand Up @@ -128,7 +128,7 @@
// electron show notification
const { notificationId, title, description, icon } = params;
console.log('NOTIFICATION_SHOW ', params);
const uuid = notificationId || generateUUID();

Check warning on line 131 in apps/desktop/src-electron/appNotification.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

'uuid' is assigned a value but never used
const notification = new Notification({
title,
body: description,
Expand All @@ -141,7 +141,7 @@
notification.on('click', () => {
logger.info('notification clicked');
// 可以在这里处理通知被点击的事件
const safelyMainWindow = getSafelyMainWindow();

Check warning on line 144 in apps/desktop/src-electron/appNotification.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

'safelyMainWindow' is assigned a value but never used
// safelyMainWindow?.webContents.send(ipcMessageKeys.NOTIFICATION_CLICKED, uuid);
// showMainWindow(); // 例如,显示主窗口
});
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-electron/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const ipcMessageKeys = {
APP_LOCK_NOW: 'app/lockNow',
APP_GET_ENV_PATH: 'app/getEnvPath',
APP_GET_BUNDLE_INFO: 'app/getBundleInfo',
APP_OPEN_LOGGER_FILE: 'app/openLoggerFile',
huhuanming marked this conversation as resolved.
Show resolved Hide resolved

// Theme
THEME_UPDATE: 'theme/update',
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src-electron/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from 'electron';
import logger from 'electron-log';
import logger from 'electron-log/main';
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
import { isFunction } from 'lodash';

import type {
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src-electron/libs/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { safeStorage } from 'electron';
import logger from 'electron-log';
import logger from 'electron-log/main';
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
import Store from 'electron-store';

import type { ILocaleSymbol } from '@onekeyhq/shared/src/locale';
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src-electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export type IDesktopAPI = {
setBadge: (params: INotificationSetBadgeParams) => void;
getNotificationPermission: () => INotificationPermissionDetail;
callDevOnlyApi: (params: IDesktopMainProcessDevOnlyApiParams) => any;
openLoggerFile: () => void;
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
};
declare global {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -225,6 +226,7 @@ const desktopApi = {
},
getBundleInfo: () =>
ipcRenderer.sendSync(ipcMessageKeys.APP_GET_BUNDLE_INFO) as IMacBundleInfo,
openLoggerFile: () => ipcRenderer.send(ipcMessageKeys.APP_OPEN_LOGGER_FILE),
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
promptTouchID: async (
msg: string,
): Promise<{ success: boolean; error?: string }> =>
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src-electron/process/AutoUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
import checkDiskSpace from 'check-disk-space';
import { BrowserWindow, app, dialog, ipcMain } from 'electron';
import isDev from 'electron-is-dev';
import logger from 'electron-log';
import logger from 'electron-log/main';
import { rootPath } from 'electron-root-path';
import { CancellationToken, autoUpdater } from 'electron-updater';
import { readCleartextMessage, readKey } from 'openpgp';
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src-electron/process/BaseProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { spawn } from 'child_process';
import path from 'path';

import isDev from 'electron-is-dev';
import logger from 'electron-log';
import logger from 'electron-log/main';
huhuanming marked this conversation as resolved.
Show resolved Hide resolved

import type { ChildProcess } from 'child_process';

Expand Down
3 changes: 1 addition & 2 deletions apps/desktop/src-electron/process/Bridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import logger from 'electron-log';
import logger from 'electron-log/main';
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
import fetch from 'node-fetch';

import BaseProcess from './BaseProcess';
Expand All @@ -11,7 +11,6 @@ class BridgeProcess extends BaseProcess {
super('bridge', 'onekeyd', {
startupThrottleTime: 3,
});
logger.info('logger file name =====> :', logger.transports.file.file);
}

async getStatus(): Promise<IStatus> {
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src-electron/process/TouchRes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import AdmZip from 'adm-zip';
import Axios from 'axios';
import { dialog, ipcMain, shell } from 'electron';
import isDev from 'electron-is-dev';
import logger from 'electron-log';
import logger from 'electron-log/main';

import { ipcMessageKeys } from '../config';

Expand Down
3 changes: 1 addition & 2 deletions apps/desktop/src-electron/process/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from 'electron';
import logger from 'electron-log';
import logger from 'electron-log/main';

import autoUpdateInit from './AutoUpdate';
import BridgeProcess, { BridgeHeart } from './Bridge';
Expand Down Expand Up @@ -41,7 +41,6 @@ export const restartBridge = async () => {
};

const init = async ({ mainWindow, store }: IDependencies) => {
logger.info('Electron main process log path: ', logger.transports.file.file);
await launchBridge();
if (!process.mas) {
autoUpdateInit({ mainWindow, store });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const exportLogs = async () => {
window.desktopApi.openLoggerFile();
};
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 28 additions & 0 deletions packages/shared/src/logger/utils/index.desktop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import logger from 'electron-log/renderer';

import platformEnv from '../../platformEnv';

import type { IUtilsType } from './types';

const consoleFunc = (msg: string) => {
if (platformEnv.isDev) {
// eslint-disable-next-line no-console
console.log(msg);
}
logger.info(msg);
};
huhuanming marked this conversation as resolved.
Show resolved Hide resolved

const getLogFilePath = () => Promise.resolve('');
huhuanming marked this conversation as resolved.
Show resolved Hide resolved

const getDeviceInfo = () =>
[
`System: ${window.desktopApi.platform} ${window.desktopApi.systemVersion}`,
`appPlatform: ${platformEnv.appPlatform ?? ''}`,
`appChannel: ${platformEnv.appChannel ?? ''}`,
`buildNumber: ${platformEnv.buildNumber ?? ''}`,
`Version Hash: ${platformEnv.githubSHA ?? ''}`,
`version: ${platformEnv.version ?? ''}`,
].join(',');
huhuanming marked this conversation as resolved.
Show resolved Hide resolved

const utils: IUtilsType = { getDeviceInfo, getLogFilePath, consoleFunc };
export default utils;
16 changes: 8 additions & 8 deletions packages/shared/src/request/axiosInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ axios.interceptors.response.use(

try {
const isOneKeyDomain = await checkRequestIsOneKeyDomain({ config });
defaultLogger.app.network.end({
requestType: 'axios',
method: config.method as string,
path: config.url as string,
statusCode: response.status,
requestId: config.headers[HEADER_REQUEST_ID_KEY],
responseCode: response.data.code,
});
if (!isOneKeyDomain) {
defaultLogger.app.network.end({
requestType: 'axios',
method: config.method as string,
path: config.url as string,
statusCode: response.status,
requestId: config.headers[HEADER_REQUEST_ID_KEY],
responseCode: response.data.code,
});
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
return response;
}
} catch (e) {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6685,7 +6685,7 @@ __metadata:
electron-builder: "npm:24.13.3"
electron-context-menu: "npm:^3.5.0"
electron-is-dev: "npm:^2.0.0"
electron-log: "npm:^4.4.8"
electron-log: "npm:5.2.0"
electron-store: "npm:^8.2.0"
electron-updater: "npm:6.1.8"
esbuild: "npm:0.15.18"
Expand Down Expand Up @@ -19604,10 +19604,10 @@ __metadata:
languageName: node
linkType: hard

"electron-log@npm:^4.4.8":
version: 4.4.8
resolution: "electron-log@npm:4.4.8"
checksum: 10/9416e1d04395e93b6bfdd9db3815d81b39286e53aa58e21817021ae7dd5a7f03ff5878ad9edcaa2be3e1d0a2d966f785880e10f197abc2a68b8cb7f5719529e5
"electron-log@npm:5.2.0":
version: 5.2.0
resolution: "electron-log@npm:5.2.0"
checksum: 10/92e454f69f9d625ad7c5daef1e431d6489ab07b048720a39acba2a8cb9798850a41f65b10169a7672c99e2c90e07ed3fc62ab58ce229b8f00f8bf94195b6514c
languageName: node
linkType: hard

Expand Down
Loading