Skip to content

Commit

Permalink
Better logging, and log files written to disk (#361)
Browse files Browse the repository at this point in the history
remove unused bugsnag errors
  • Loading branch information
okdistribute committed Jun 23, 2020
1 parent 5d0028e commit f55f43d
Show file tree
Hide file tree
Showing 27 changed files with 673 additions and 144 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"browser": true
},
"rules": {
"no-proto": 0,
"react/prop-types": 0,
"react/display-name": 0,
"react-hooks/rules-of-hooks": "error", // Checks rules of Hooks
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ examples/.dat
static/build.js
test/lib/test-data
bin/test-data
/translations
translations
storybook-static
updates
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ Then, in another terminal, run the app in development mode:
npm run dev
```

## Testing
Running `npm run dev` will run the background process in an electron window
that can be stepped through similarly to the front-end code.

To see log messages in real-time while in debug mode, run `tail` in another
terminal window:

```sh
tail -f USERDATA/Mapeo/logs/$DATE.debug.log
```

### Debbugging

Data is stored in `USERDATA/Mapeo` on your machine, which will be different
depending on your operating system.
Expand All @@ -54,6 +64,12 @@ To simulate a reinstall, remove this `Mapeo` directory.
To only delete data and not presets or tiles, delete the `Mapeo/kappa.db`
directory.

In production mode, `info` and `error` messages are written to
`USERDATA/Mapeo/logs/$DATE.log` and kept for 1 year.

In debug mode and development mode (via `npm run dev` or in the Help menu),
Mapeo will verbosely write all messages for 1 day.

### Run a mock device


Expand Down
52 changes: 31 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ const app = electron.app
const BrowserWindow = electron.BrowserWindow

const logger = require('./src/logger')
const miscellaneousIpc = require('./src/main/ipc')
const electronIpc = require('./src/main/ipc')
const createMenu = require('./src/main/menu')
const windowStateKeeper = require('./src/main/window-state')

// HACK: enable GPU graphics acceleration on some older laptops
app.commandLine.appendSwitch('ignore-gpu-blacklist', 'true')
app.commandLine.appendSwitch('ignore-certificate-errors')

// Setup some handy dev tools shortcuts (only activates in dev mode)
// See https://github.com/sindresorhus/electron-debug
Expand Down Expand Up @@ -57,6 +58,14 @@ if (!gotTheLock) {
// https://github.com/atom/electron/blob/master/docs/api/app.md#appgetpathname
var userDataPath = app.getPath('userData')

if (!logger.configured) {
logger.configure({
label: 'main',
userDataPath,
isDev
})
}

var argv = minimist(process.argv.slice(2), {
default: {
port: 5000,
Expand All @@ -74,7 +83,7 @@ var argv = minimist(process.argv.slice(2), {
var _socketName

rabbit.findOpenSocket('mapeo').then((socketName) => {
logger.log('got socket', socketName)
logger.debug('got socket', socketName)
_socketName = socketName
if (argv.headless) startSequence()
else app.once('ready', openWindow)
Expand All @@ -89,7 +98,7 @@ app.on('window-all-closed', function () {

function openWindow () {
ipc.on('error', function (err) {
logger.error(err)
logger.error('ipc', err)
electron.dialog.showErrorBox('Error', err)
})
ipc.connect(_socketName)
Expand All @@ -108,8 +117,8 @@ function openWindow () {
} = require('electron-devtools-installer')
} catch (e) {}
installExtension(REACT_DEVELOPER_TOOLS)
.then(name => logger.log(`Added Extension: ${name}`))
.catch(err => logger.log('An error occurred: ', err))
.then(name => logger.debug(`Added Extension: ${name}`))
.catch(err => logger.error('Failed to add extension', err))
} else {
createBackgroundProcess(_socketName)
}
Expand All @@ -131,7 +140,7 @@ function openWindow () {

function startupMsg (txt) {
return function (done) {
logger.log('[STARTUP] ' + txt)
logger.debug('[STARTUP] ' + txt)
done()
}
}
Expand All @@ -151,7 +160,7 @@ function startSequence () {
],
function (err) {
if (err) logger.error('STARTUP FAILED', err)
else logger.log('STARTUP success!')
else logger.debug('STARTUP success!')
}
)
}
Expand All @@ -170,10 +179,9 @@ function initDirectories (done) {
}

function createServers (done) {
// TODO: rename/refactor
miscellaneousIpc(win)
electronIpc(win)

logger.log('initializing mapeo', userDataPath, argv.port)
logger.info('initializing mapeo', userDataPath, argv.port)
var opts = {
userDataPath,
datadir: argv.datadir,
Expand All @@ -183,9 +191,8 @@ function createServers (done) {

ipc.send('listen', opts, function (err, port) {
if (err) throw new Error('fatal: could not get port', err)
logger.log('listen port got back', port)
global.osmServerHost = '127.0.0.1:' + port
logger.log(global.osmServerHost)
logger.info(global.osmServerHost)
done()
})
}
Expand Down Expand Up @@ -243,6 +250,7 @@ function createWindow (socketName) {

// Create a hidden background window
function createBgWindow (socketName) {
logger.debug('loading electron background window')
var win = new BrowserWindow({
x: 0,
y: 0,
Expand All @@ -253,17 +261,18 @@ function createBgWindow (socketName) {
nodeIntegration: true
}
})
console.log('loading bg window')
var BG = 'file://' + path.join(__dirname, './src/background/index.html')
win.loadURL(BG)
win.webContents.on('did-finish-load', () => {
if (argv.debug) bg.webContents.openDevTools()
win.webContents.send('set-socket', {
name: socketName
win.webContents.send('configure', {
socketName,
userDataPath,
isDev
})
})
win.on('closed', () => {
console.log('background window closed')
logger.info('Background window closed')
app.quit()
})
return win
Expand All @@ -284,15 +293,16 @@ function createSplashWindow () {
}

function createBackgroundProcess (socketName) {
console.log('creating background process')
logger.debug('creating background process')
serverProcess = fork(path.join(__dirname, 'src', 'background', 'index.js'), [
'--subprocess',
app.getVersion(),
socketName
socketName,
userDataPath
])

serverProcess.on('message', msg => {
console.log(msg)
logger.debug(msg)
})
}

Expand Down Expand Up @@ -322,9 +332,9 @@ function beforeQuit (e) {

// 'close' event will gracefully close databases and wait for pending sync
// TODO: Show the user that a sync is pending finishing
console.log('ipc.send close')
logger.debug('ipc.send close')
ipc.send('close', null, () => {
console.log('closed')
logger.info('IPC closed')
clearTimeout(closingTimeoutId)
if (serverProcess) serverProcess.kill()
try { closingWin.close() } catch (e) {}
Expand Down
2 changes: 2 additions & 0 deletions messages/main/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"menu-copy": "Copy",
"menu-paste": "Paste",
"menu-selectall": "Select all",
"menu-debugging": "Debug",
"menu-report": "Report issue...",
"save-db-dialog": "Create a new database to syncronize",
"open-db-dialog": "Select a database to syncronize",
"feedback-contribute-button": "Feedback & Contribute",
Expand Down
Loading

0 comments on commit f55f43d

Please sign in to comment.