-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
204 additions
and
37 deletions.
There are no files selected for viewing
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,28 @@ | ||
import sbp from '@sbp/sbp' | ||
import '@sbp/okturtles.events' | ||
import { CAPTURED_LOGS } from '~/frontend/utils/events.js' | ||
|
||
export default (console: Object): Function => { | ||
// only log to server if we're in development mode and connected over the | ||
// tunnel (which creates URLs that begin with 'https://gi' per Gruntfile.js) | ||
if (process.env.NODE_ENV !== 'development' || !self.location.href.startsWith('https://gi')) return | ||
|
||
sbp('okTurtles.events/on', CAPTURED_LOGS, ({ level, msg: stringifyMe }) => { | ||
if (level === 'debug') return // comment out to send much more log info | ||
const value = JSON.stringify(stringifyMe) | ||
// To avoid infinite loop because we log all selector calls, we run sbp calls | ||
// here in a roundabout way by getting the function to which they're mapped. | ||
// The reason this works is because the entire `sbp` domain is blacklisted | ||
// from being logged. | ||
const apiUrl = sbp('sbp/selectors/fn', 'okTurtles.data/get')('API_URL') | ||
fetch(`${apiUrl}/log`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ level, value }) | ||
}).catch(e => { | ||
console.error(`[captureLogs] '${e.message}' attempting to log [${level}] to server:`, value) | ||
}) | ||
}) | ||
} |
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,86 @@ | ||
import sbp from '@sbp/sbp' | ||
import { debounce } from '@model/contracts/shared/giLodash.js' | ||
import { CAPTURED_LOGS, SET_APP_LOGS_FILTER } from '~/frontend/utils/events.js' | ||
import { MAX_LOG_ENTRIES } from '~/frontend/utils/constants.js' | ||
import { createLogger } from './logger.js' | ||
import logServer from './logServer.js' | ||
|
||
/* | ||
- giConsole/[username]/entries - the stored log entries. | ||
- giConsole/[username]/config - the logger config used. | ||
*/ | ||
|
||
const config = { | ||
maxEntries: MAX_LOG_ENTRIES | ||
} | ||
const originalConsole = self.console | ||
|
||
// These are initialized in `captureLogsStart()`. | ||
let logger: Object = null | ||
let identityContractID: string = '' | ||
|
||
// A default storage backend using `IndexedDB`. | ||
const getItem = (key: string): Promise<?string> => sbp('gi.db/logs/load', `giConsole/${identityContractID}/${key}`) | ||
const removeItem = (key: string): Promise<void> => sbp('gi.db/logs/delete', `giConsole/${identityContractID}/${key}`) | ||
const setItem = (key: string, value: any): Promise<void> => { | ||
return sbp('gi.db/logs/save', `giConsole/${identityContractID}/${key}`, typeof value === 'string' ? value : JSON.stringify(value)) | ||
} | ||
|
||
async function captureLogsStart (userLogged: string) { | ||
identityContractID = userLogged | ||
|
||
logger = await createLogger(config, { getItem, removeItem, setItem }) | ||
|
||
// Save the new config. | ||
await setItem('config', config) | ||
|
||
// TODO: Get this dynamically | ||
logger.setAppLogsFilter((((process.env.NODE_ENV === 'development' || new URLSearchParams(location.search).get('debug')) | ||
? ['error', 'warn', 'info', 'debug', 'log'] | ||
: ['error', 'warn', 'info']): string[])) | ||
|
||
// Subscribe to `swLogsFilter` changes. | ||
sbp('okTurtles.events/on', SET_APP_LOGS_FILTER, logger.setAppLogsFilter) | ||
|
||
// Overwrite the original console. | ||
self.console = logger.console | ||
|
||
originalConsole.log('Starting to capture logs of type:', logger.swLogsFilter) | ||
} | ||
|
||
async function captureLogsPause ({ wipeOut }: { wipeOut: boolean }): Promise<void> { | ||
if (wipeOut) { await clearLogs() } | ||
sbp('okTurtles.events/off', SET_APP_LOGS_FILTER) | ||
console.log('captureLogs paused') | ||
// Restore original console behavior. | ||
self.console = originalConsole | ||
} | ||
|
||
async function clearLogs () { | ||
await logger?.clear() | ||
} | ||
|
||
// In the SW, there's no event to detect when the SW is about to terminate. As | ||
// a result, we must save logs on every saved entry. However, this wouldn't be | ||
// very performant, so we debounce the save instead. | ||
sbp('okTurtles.events/on', CAPTURED_LOGS, debounce(() => { | ||
logger?.save().catch(e => { | ||
console.error('Error saving logs during CAPTURED_LOGS event handler', e) | ||
}) | ||
}, 500)) | ||
|
||
// Enable logging to the server | ||
logServer(originalConsole) | ||
|
||
export default (sbp('sbp/selectors/register', { | ||
'swLogs/get' () { return logger?.entries.toArray() ?? [] }, | ||
async 'swLogs/save' () { await logger?.save() }, | ||
'swLogs/pauseCapture': captureLogsPause, | ||
'swLogs/startCapture': captureLogsStart, | ||
async 'swLogs/clearLogs' (userID) { | ||
const savedID = identityContractID | ||
identityContractID = userID | ||
try { await clearLogs() } catch {} | ||
identityContractID = savedID | ||
} | ||
}): string[]) |
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