Skip to content

Commit

Permalink
Fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Dec 24, 2023
1 parent 6012722 commit 9e2a9d5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
27 changes: 13 additions & 14 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ const makeTrackDict = require('./track-dict')
const audio = require('./windows/audio')
const player = require('./windows/player')
const AudioLibrary = require('./lib/audio-library')
const log = require('electron-log')
const autoUpdater = require('electron-updater').autoUpdater

// handle uncaught exceptions before calling any functions
process.on('uncaughtException', (err) => {
log.error(err)
console.error(err)
})

const windows = [player, audio]
Expand Down Expand Up @@ -65,7 +64,7 @@ app.on('ready', function appReady () {
artwork.init()

electron.powerMonitor.on('suspend', function pauseOnWake () {
log.info('Entering sleep, pausing')
broadcast('log', 'Entering sleep, pausing')
ipcMain.emit('pause')
})

Expand All @@ -92,7 +91,7 @@ app.on('ready', function appReady () {
// register autoUpdater
if (!process.env.DEV_SERVER) {
setTimeout(() => {
log.info('autoUpdater: Auto update initalized...')
broadcast('log', 'autoUpdater: Auto update initalized...')
autoUpdater.checkForUpdatesAndNotify()
}, 1000 * 3)
}
Expand All @@ -103,17 +102,17 @@ app.on('ready', function appReady () {
})

autoUpdater.on('checking-for-update', () => {
log.info('autoUpdater: Checking for update...')
broadcast('log', 'autoUpdater: Checking for update...')
broadcast('au:checking-for-update')
})

autoUpdater.on('update-available', (info) => {
log.info('autoUpdater: Update available!')
broadcast('log', 'autoUpdater: Update available!')
broadcast('au:update-available', info)
})

autoUpdater.on('update-not-available', (info) => {
log.info('autoUpdater: No update available')
broadcast('log', 'autoUpdater: No update available')
broadcast('au:update-not-available', info)
})

Expand All @@ -122,7 +121,7 @@ app.on('ready', function appReady () {
})

autoUpdater.on('update-downloaded', (info) => {
log.info('autoUpdater: Update downloaded')
broadcast('log', 'autoUpdater: Update downloaded')
broadcast('au:update-downloaded', info)
})

Expand All @@ -146,7 +145,7 @@ app.on('ready', function appReady () {

function queue (ev, newIndex) {
const newTrack = al.queue(newIndex)
log.info(newTrack)
broadcast('log', newTrack)
broadcast('new-track', newTrack)
if (player.win) {
player.win.send('new-index', al.index)
Expand All @@ -163,7 +162,7 @@ app.on('ready', function appReady () {
}

function handleGetPath (err, blobPath) {
if (err) return log.error(err)
if (err) return broadcast('log', err)
al.currentTrack.artwork = blobPath
if (player.win) {
player.win.send('new-track', al.currentTrack)
Expand Down Expand Up @@ -247,16 +246,16 @@ app.on('ready', function appReady () {
function handleNewTracks (err, newTrackDict) {
state.loading = false
broadcast('loading', false)
if (err) return log.warn(err)
if (err) return broadcast('log', err)
const newState = al.load(newTrackDict)
if (player.win) player.win.send('track-dict', newState.trackDict, newState.order, state.paths)
console.timeEnd('update-library')
log.info('Done scanning. Found ' + Object.keys(newState.trackDict).length + ' tracks.')
broadcast('log', 'Done scanning. Found ' + Object.keys(newState.trackDict).length + ' tracks.')
}

function updateLibrary (ev, paths) {
if (state.loading) state.loading.destroy()
log.info('Updating library with new path(s): ' + paths)
broadcast('log', 'Updating library with new path(s): ' + paths)
console.time('update-library')
state.paths = paths
state.loading = makeTrackDict(paths, handleNewTracks)
Expand Down Expand Up @@ -300,7 +299,7 @@ app.on('before-quit', function beforeQuit (e) {
app.isQuitting = true
e.preventDefault()
setTimeout(function () {
log.error('Saving state took too long. Quitting.')
console.log('Saving state took too long. Quitting.')
app.quit()
}, 20000) // quit after 5 secs, at most
persist.set({
Expand Down
7 changes: 3 additions & 4 deletions main/track-dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const mm = require('music-metadata')
const writer = require('flush-write-stream')
const filter = require('through2-filter')
const pump = require('pump')
const log = require('electron-log')
const validExtensions = ['m4a', 'mp3', 'ogg']

module.exports = makeTrackDict
Expand All @@ -20,7 +19,7 @@ function makeTrackDict (paths, cb) {

function handleEos (err) {
if (err) return cb(err)
log.info('')
console.log('')
cb(null, newTrackDict)
}
}
Expand All @@ -32,7 +31,7 @@ function isValidFile (data, enc, cb) {

function concatTrackDict (obj) {
function writeTrackDict (data, enc, cb) {
log.info(`Scanning ${data.filepath}`)
console.log(`Scanning ${data.filepath}`)
parseMetadata(data, handleMeta)

function handleMeta (err, meta) {
Expand Down Expand Up @@ -83,7 +82,7 @@ function parseMetadata (data, cb) {
})
}).catch(err => {
// Ignore errors
log.info(err.message += ` (file: ${filepath})`)
console.log(err.message += ` (file: ${filepath})`)
const { basename } = data
const ext = path.extname(basename)
const title = path.basename(basename, ext)
Expand Down
3 changes: 1 addition & 2 deletions main/windows/audio.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { app, BrowserWindow } = require('electron')
const path = require('path')
const log = require('electron-log')
const remoteMain = require('@electron/remote/main')
const AUDIO_WINDOW = 'file://' + path.resolve(__dirname, '..', '..', 'renderer', 'audio', 'index.html')
const audio = module.exports = {
Expand Down Expand Up @@ -53,7 +52,7 @@ function show () {

function toggleDevTools () {
if (!audio.win) return
log.info('[AUDIO] Toggling dev tools')
console.log('[AUDIO] Toggling dev tools')
if (audio.win.webContents.isDevToolsOpened()) {
audio.win.webContents.closeDevTools()
audio.win.hide()
Expand Down
3 changes: 1 addition & 2 deletions main/windows/player.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { BrowserWindow, app } = require('electron')
const windowStateKeeper = require('electron-window-state')
const path = require('path')
const log = require('electron-log')
const remoteMain = require('@electron/remote/main')
const PLAYER_WINDOW = 'file://' + path.resolve(__dirname, '..', '..', 'renderer', 'player', 'index.html')

Expand Down Expand Up @@ -63,7 +62,7 @@ function init () {

function toggleAlwaysOnTop () {
if (!player.win) return
log.info('[PLAYER] Toggling Always on Top')
console.log('[PLAYER] Toggling Always on Top')
if (player.win.isAlwaysOnTop()) {
alwaysOnTop = false
player.win.setAlwaysOnTop(alwaysOnTop)
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"electron-context-menu": "^3.6.1",
"electron-debug": "^3.0.1",
"electron-default-menu": "^1.0.1",
"electron-log": "^5.0.0-beta.16",
"electron-store": "^8.1.0",
"electron-updater": "^6.1.7",
"electron-window-state": "^5.0.2",
Expand Down
2 changes: 2 additions & 0 deletions renderer/player/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const choo = require('choo')
const { ipcRenderer } = require('electron')
const app = window.hyperamp = choo()

ipcRenderer.on('log', (...args) => console.log(args))

const entypoSprite = require('entypo').getNode()
document.body.insertAdjacentElement('afterbegin', entypoSprite)

Expand Down

0 comments on commit 9e2a9d5

Please sign in to comment.