Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/logging #1520

Merged
merged 5 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Bitcoin from 'bitcoinjs-lib'
import BIP39 from 'bip39'

import * as crypto from 'blockchain-wallet-v4/src/walletCrypto'
import * as A from './actions'
import { actions, selectors } from 'data'
import { CUSTOM_DIMENSIONS } from './model'

Expand Down Expand Up @@ -47,15 +48,15 @@ export default ({ api }) => {
const isCryptoDisplayed = yield select(
selectors.preferences.getCoinDisplayed
)
yield call(startSession, { guid })
yield put(A.startSession(guid))
yield call(postMessage, {
method: 'setCustomDimension',
messageData: {
dimensionId: CUSTOM_DIMENSIONS.CURRENCY_PREFERENCE,
dimensionValue: isCryptoDisplayed ? 'crypto' : 'fiat'
}
})
yield call(logPageView, { route: '/home' })
yield put(A.logPageView('/home'))
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'initUserSession', e))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { testSaga } from 'redux-saga-test-plan'

import * as A from './actions'
import * as actions from '../actions'
import { selectors } from 'data'
import analyticsSagas, { logLocation } from './sagas'
Expand Down Expand Up @@ -40,7 +41,7 @@ describe('analyticsSagas', () => {
})

it('should call to start session', () => {
saga.next(true).call(startSession, { guid: mockGuid })
saga.next(true).put(A.startSession(mockGuid))
})

it('should log currency pref customDimension', () => {
Expand All @@ -56,7 +57,7 @@ describe('analyticsSagas', () => {
it('should log home page view', () => {
saga
.next()
.call(logPageView, { route: '/home' })
.put(A.logPageView('/home'))
.next()
.isDone()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ export default ({ api }) => {
const device = deviceR.getOrFail()
deviceType = prop('device_type', device)
}
const logLevel = yield select(selectors.logs.getLogLevel)
const appConnection = yield Lockbox.utils.pollForAppConnection(
deviceType,
appRequested,
timeout
timeout,
logLevel
)
yield put(
A.setConnectionInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export default ({ api }) => {
yield put(actions.alerts.displayInfo(C.PLEASE_LOGIN))
}

const defineLogLevel = function*(search) {
const params = new URLSearchParams(search)
const level = params.get('level')
window.logLevel = level
yield put(actions.logs.setLogLevel(level))
}

const defineActionGoal = function*(pathname, search) {
try {
// Other scenarios with actions encoded in base64
Expand All @@ -99,6 +106,8 @@ export default ({ api }) => {
if (startsWith('kyc', pathname)) return yield call(defineKycGoal, search)
if (startsWith('bitcoin', pathname))
return yield call(defineSendBtcGoal, pathname, search)
if (startsWith('log-level', pathname))
return yield call(defineLogLevel, search)
yield call(defineActionGoal, pathname, search)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const LOG_INFO_MSG = 'LOG_INFO_MSG'
export const LOG_ERROR_MSG = 'LOG_ERROR_MSG'
export const SET_LOG_LEVEL = 'SET_LOG_LEVEL'
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export const logErrorMessage = (file, method, message) => ({
type: AT.LOG_ERROR_MSG,
payload: { file, method, message }
})
export const setLogLevel = level => ({
type: AT.SET_LOG_LEVEL,
payload: { level }
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const LOG_LEVELS = {
OFF: 'off',
VERBOSE: 'verbose'
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as AT from './actionTypes'
import { insert } from 'ramda'
import { assoc, insert } from 'ramda'
import { LOG_LEVELS } from './model'

const INITIAL_STATE = []
const INITIAL_STATE = {
logLevel: LOG_LEVELS.OFF,
logs: []
}

const logger = (state = INITIAL_STATE, action) => {
const { type, payload } = action
Expand All @@ -12,16 +16,29 @@ const logger = (state = INITIAL_STATE, action) => {
file: payload.file,
method: payload.method,
timestamp: Date.now(),
message: JSON.stringify(payload.message)
message: payload.message
}
}

const { logLevel, logs } = state

switch (type) {
case AT.LOG_ERROR_MSG: {
return insert(0, createLog('ERROR', payload), state)
const log = createLog('ERROR', payload)
/* eslint-disable */
if (logLevel === LOG_LEVELS.VERBOSE) console.info('LOG: ', log)
/* eslint-enable */
return assoc('logs', insert(0, log, logs), state)
}
case AT.LOG_INFO_MSG: {
return insert(0, createLog('INFO', payload), state)
const log = createLog('INFO', payload)
/* eslint-disable */
if (logLevel === LOG_LEVELS.VERBOSE) console.info('LOG: ', log)
/* eslint-enable */
return assoc('logs', insert(0, log, logs), state)
}
case AT.SET_LOG_LEVEL: {
return assoc('logLevel', payload.level, state)
}
default:
return state
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { path } from 'ramda'

export const getLogs = path(['logs'])

export const getLogLevel = path(['logs', 'logLevel'])
7 changes: 4 additions & 3 deletions packages/blockchain-wallet-v4-frontend/src/data/model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as analytics from './analytics/model'
import * as rates from './modules/rates/model'
import * as profile from './modules/profile/model'
import * as components from './components/model'
import * as form from './form/model'
import * as logs from './logs/model'
import * as profile from './modules/profile/model'
import * as rates from './modules/rates/model'

export { analytics, form, rates, profile, components }
export { analytics, components, form, logs, profile, rates }
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { Types } from 'blockchain-wallet-v4/src'
import { deriveAddressFromXpub } from 'blockchain-wallet-v4/src/utils/eth'
import firmware from './firmware'
import constants from './constants'
import { model } from 'data'

const { LOG_LEVELS } = model.logs

const ethAccount = (xpub, label) => ({
label: label,
Expand All @@ -34,7 +37,7 @@ const btcAccount = (xpub, label) => Types.HDAccount.js(label, null, xpub)
* @param {Number} timeout - Length of time in ms to wait for a connection
* @returns {Promise<TransportU2F>} Returns a connected Transport or Error
*/
const pollForAppConnection = (deviceType, app, timeout = 45000) => {
const pollForAppConnection = (deviceType, app, timeout = 45000, logLevel) => {
if (!deviceType || !app) throw new Error('Missing required params')

return new Promise((resolve, reject) => {
Expand All @@ -46,14 +49,18 @@ const pollForAppConnection = (deviceType, app, timeout = 45000) => {
// transport.setDebugMode(true)
transport.setExchangeTimeout(timeout)
transport.setScrambleKey(scrambleKey)
console.info('POLL:START', deviceType, scrambleKey, app, timeout)
if (logLevel === LOG_LEVELS.VERBOSE) {
console.info('POLL:START', deviceType, scrambleKey, app, timeout)
}
// send NO_OP cmd until response is received (success) or timeout is hit (reject)
transport.send(...constants.apdus.no_op).then(
() => {},
res => {
// since no_op wont be recognized by any app as a valid cmd, this is always going
// to fail but a response, means a device is connected and unlocked
console.info('POLL:END', deviceType, scrambleKey, app, timeout)
if (logLevel === LOG_LEVELS.VERBOSE) {
console.info('POLL:END', deviceType, scrambleKey, app, timeout)
}
if (res.originalError) {
reject(res.originalError.metaData)
}
Expand Down
16 changes: 16 additions & 0 deletions packages/blockchain-wallet-v4/src/types/KVStoreEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ export const extractResponse = curry((encKey, res) => {
if (res === null) {
return res
} else {
// TODO: remove when redux/core is moved to frontend
if (window.logLevel === 'verbose' && res.type_id !== -1) {
console.info(
'LOG: ',
encKey
? compose(
decrypt(encKey),
prop('payload')
)(res)
: compose(
BufferToString,
B64ToBuffer,
prop('payload')
)(res)
)
}
return encKey
? compose(
JSON.parse,
Expand Down