Skip to content

Commit

Permalink
build: electron log
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkontoask committed Aug 25, 2021
1 parent aa9cd2e commit 09a6fd4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@radishes/api",
"version": "2.0.1",
"version": "4.0.9",
"description": "网易云音乐 NodeJS 版 API",
"scripts": {
"dev": "nodemon app.js",
Expand Down
12 changes: 8 additions & 4 deletions src/electron/event/ipc-main/auto-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { AutoDownload } from '../action-types'
import pgk from '../../../../package.json'
import log from 'electron-log'
import { infoMain } from '@/electron/utils/log'
import { infoMain, warnMain } from '@/electron/utils/log'

export interface AutoUpdateContent {
[AutoDownload.VERSION]: UpdateInfo & { url: string }
Expand All @@ -28,10 +28,14 @@ export default (win: BrowserWindow) => {

function sendStatusToWindow<T extends keyof AutoUpdateContent>(
type: T,
content: AutoUpdateContent[T]
content?: AutoUpdateContent[T]
) {
infoMain('[Updater]:', type, content)
win.webContents.send(AutoDownload.MESSAGE, type, content)
try {
win.webContents.send(AutoDownload.MESSAGE, type, content)
} catch (e) {
warnMain(e)
}
}

autoUpdater.on('update-available', info => {
Expand All @@ -43,7 +47,7 @@ export default (win: BrowserWindow) => {
})
autoUpdater.on('update-not-available', info => {
infoMain('Uupdater]:', 'update-not-available', info.version)
sendStatusToWindow(AutoDownload.NOT_VERSION, info)
sendStatusToWindow(AutoDownload.NOT_VERSION)
})
autoUpdater.on('error', err => {
sendStatusToWindow(AutoDownload.ERROR, 'Error in auto-updater. ' + err)
Expand Down
8 changes: 6 additions & 2 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import installExtension from 'electron-devtools-installer'
import { eventInit } from '@/electron/event/index'
import { downloadIntercept } from './event/ipc-main/download'
import { runService } from './service/index'
import { errorMain, infoMain } from './utils/log'
import { errorMain, infoMain, warnMain } from './utils/log'
import store from '@/electron/store/index'
import path from 'path'

Expand Down Expand Up @@ -90,7 +90,11 @@ async function createWindow() {
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
try {
createProtocol('app')
} catch (e) {
warnMain(e)
}
// Load the index.html when not in development
win
.loadURL('app://./index.html')
Expand Down
14 changes: 8 additions & 6 deletions src/electron/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ const execPath =
(process.env.PORTABLE_EXECUTABLE_DIR as string)

export enum LogInfoType {
MAIN = '[Main]',
MAIN_ERROR = '[Main Error]',
MAIN_WARN = '[Main Warn]'
MAIN = '[Main]',
MAIN_ERROR = '[Main Error]',
MAIN_WARN = '[Main Warn]'
}

log.transports.file.resolvePath = () => path.join(execPath, 'logs/main.log')

const format = (arg: unknown) => JSON.stringify(arg)

export const infoMain = (...args: unknown[]) => {
log.info(LogInfoType.MAIN, ...args)
log.info(...args.map(format))
}

export const errorMain = (...args: unknown[]) => {
log.error(LogInfoType.MAIN_ERROR, ...args)
log.error(...args.map(format))
}

export const warnMain = (...args: unknown[]) => {
log.warn(LogInfoType.MAIN_WARN, ...args)
log.warn(...args.map(format))
}

Object.assign(console, log.functions)
Expand Down

0 comments on commit 09a6fd4

Please sign in to comment.