-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Desktop): Add ability to view session logs in app
feat(Desktop): Store logs to disk
- Loading branch information
1 parent
6eb84b6
commit bea03bb
Showing
28 changed files
with
466 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const LOG_CHANNEL = { | ||
LOGS_WINDOW_OPEN: 'LOGS_WINDOW_OPEN', | ||
LOGS_PRINT: 'LOGS_PRINT', | ||
LOGS_GET: 'LOGS_GET', | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ipcMain } from 'electron' | ||
import { CHANNEL } from '../../preload/channels' | ||
import { logStore } from '../../preload/logging' | ||
import { makeHandlers, removeChannelHandlers } from '../utils/handler' | ||
import { valueToSuccessResult } from '../utils/ipc' | ||
import { LOG_CHANNEL } from './channels' | ||
import { showLogs } from './window' | ||
|
||
const registerLogHandlers = () => { | ||
ipcMain.handle(CHANNEL.LOGS_WINDOW_OPEN, async () => { | ||
showLogs() | ||
return valueToSuccessResult() | ||
}) | ||
|
||
ipcMain.handle(CHANNEL.LOGS_GET, async () => { | ||
return valueToSuccessResult(logStore) | ||
}) | ||
} | ||
|
||
const removeLogHandlers = () => { | ||
removeChannelHandlers(LOG_CHANNEL) | ||
} | ||
|
||
export const logHandlers = makeHandlers(registerLogHandlers, removeLogHandlers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { BrowserWindow } from 'electron' | ||
import { logHandlers } from '.' | ||
import { i18n } from '../../i18n' | ||
import { streamLogsToWindow } from '../../preload/logging' | ||
import { registerBaseMenu } from '../menu' | ||
import { createWindow } from '../window' | ||
import { onUiLoaded } from '../window/windowUtils' | ||
|
||
let logsWindow: BrowserWindow | null | ||
|
||
const logsUrl = '/logs' | ||
|
||
export const showLogs = () => { | ||
if (logsWindow) { | ||
logsWindow.show() | ||
return logsWindow | ||
} | ||
|
||
logsWindow = createWindow(logsUrl, { | ||
width: 600, | ||
height: 800, | ||
minWidth: 200, | ||
minHeight: 600, | ||
show: false, | ||
title: i18n.t('logs.title'), | ||
}) | ||
|
||
// The ID needs to be stored separately from the window object. Otherwise an error | ||
// is thrown because the time remove handlers are called the window object is already destroyed. | ||
const windowId = logsWindow.id | ||
|
||
logHandlers.register(windowId) | ||
|
||
logsWindow.on('closed', () => { | ||
logHandlers.remove(windowId) | ||
logsWindow = null | ||
}) | ||
|
||
streamLogsToWindow(logsWindow.webContents) | ||
|
||
onUiLoaded(logsWindow.webContents)(() => { | ||
logsWindow?.show() | ||
}) | ||
|
||
logsWindow.on('focus', () => { | ||
registerBaseMenu() | ||
}) | ||
|
||
logsWindow?.loadURL(logsUrl) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { WebContents } from 'electron' | ||
import log, { LogMessage } from 'electron-log' | ||
import { pubsub } from 'stencila' | ||
import { CHANNEL } from './channels' | ||
import { isDevelopment } from './utils/env' | ||
|
||
// In memory store of logs. Reset upon application launch | ||
export const logStore: LogMessage[] = [] | ||
|
||
// Only add log messages `warn` or higher to log file on disk | ||
log.transports.file.level = 'warn' | ||
|
||
// Only log messages `warn` or higher to console/log panel when in production | ||
log.transports.console.level = isDevelopment ? 'silly' : 'warn' | ||
|
||
// When a logging a message, store it for future retrieval by the "Application Log" window | ||
log.hooks.push((message: LogMessage, transport): LogMessage => { | ||
if (transport === log.transports.console) { | ||
// Constrain overall length of the array by dropping the oldest log item | ||
if (logStore.length > 200) { | ||
logStore.shift() | ||
} | ||
|
||
logStore.push(message) | ||
} | ||
return message | ||
}) | ||
|
||
/** | ||
* Subscribe to stdout log messages from Stencila CLI client and log to Electron app console. | ||
*/ | ||
export const enableLogging = () => { | ||
pubsub.subscribe('logging', (_topic: string, event: any) => { | ||
const { | ||
message, | ||
metadata: { level, target }, | ||
} = event | ||
|
||
const line = [`%c${target} |%c ${message}`, 'color: blue', 'color: unset'] | ||
|
||
switch (level) { | ||
case 'TRACE': | ||
return log.verbose(...line) | ||
case 'DEBUG': | ||
return log.debug(...line) | ||
case 'INFO': | ||
return log.info(...line) | ||
case 'WARN': | ||
return log.warn(...line) | ||
case 'ERROR': | ||
return log.error(...line) | ||
} | ||
}) | ||
} | ||
|
||
export const streamLogsToWindow = (wc: WebContents): void => { | ||
log.hooks.push((message: LogMessage, transport): LogMessage => { | ||
if (transport === log.transports.console) { | ||
wc.send(CHANNEL.LOGS_PRINT, message) | ||
} | ||
return message | ||
}) | ||
} |
Oops, something went wrong.