Skip to content

Commit b1d4e3b

Browse files
committed
send logs from client to ext channel
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 4e1e1a5 commit b1d4e3b

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

src/channel.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ class Channel implements Channel {
2828
// action may be an object.type or plain string
2929
const actionType: string = typeof action === 'string' ? action : action.type
3030

31-
logger(`EXT RECEIVED: "${actionType}"`)
31+
if (actionType === 'CLIENT_LOG') {
32+
// logs in web client are not easily visible
33+
// it's simpler to log to the "CodeRoad (Logs)" channel
34+
logger(action.payload)
35+
return
36+
}
37+
38+
logger(actionType)
3239

3340
switch (actionType) {
3441
case 'EDITOR_STARTUP':

src/services/logger/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const logger = (...messages: Log[]): void => {
99
// to get around it, we can log with multiple log statements
1010
for (const message of messages) {
1111
if (typeof message === 'object') {
12-
logChannel.appendLine(JSON.stringify(message))
12+
logChannel.appendLine(message)
1313
} else {
1414
logChannel.appendLine(message)
1515
}

web-app/src/Routes.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ const Routes = () => {
2020
return <ErrorView send={send} error={context.error} />
2121
}
2222

23-
logger(`ROUTE: ${route}`)
24-
logger(`POSITION: ${JSON.stringify(context.position)}`)
23+
logger(
24+
`ROUTE: "${route}": ${context.position?.complete ? 'Completed' : 'On'} level ${
25+
context.position?.levelId || 'unknown'
26+
}, step ${context.position?.stepId || 'unknown'}`,
27+
)
2528

2629
return (
2730
<Router route={route}>

web-app/src/environment.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
export const DEBUG: boolean = (process.env.REACT_APP_DEBUG || '').toLowerCase() === 'true'
1010
export const VERSION: string = process.env.VERSION || 'unknown'
1111
export const NODE_ENV: string = process.env.NODE_ENV || 'development'
12-
export const LOG: boolean = (process.env.REACT_APP_LOG || '').toLowerCase() === 'true'
1312
export const TUTORIAL_LIST_URL: string = process.env.REACT_APP_TUTORIAL_LIST_URL || ''
1413

1514
// config variables

web-app/src/services/logger/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { LOG } from '../../environment'
1+
import { editor } from '../state/useStateMachine'
22

33
export type Log = string | object | number | null
44

55
const logger = (...messages: Log[]): void => {
6-
if (!LOG) {
7-
return
8-
}
6+
// logs are difficult to view in the web client.
7+
// for debugging purposes it's easier to collect logs in the "CodeRoad (Logs)" output channel
8+
editor.postMessage({
9+
type: 'CLIENT_LOG',
10+
payload: messages,
11+
source: 'coderoad', // filter events by source on editor side
12+
})
13+
914
// Inside vscode, you console.log does not allow more than 1 param
1015
// to get around it, we can log with multiple log statements
1116
for (const message of messages) {

web-app/src/services/state/useStateMachine.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ interface Output {
1313

1414
declare let acquireVsCodeApi: any
1515

16-
const editor = acquireVsCodeApi()
17-
const editorSend = (action: T.Action) => {
18-
logger(`TO EXT: "${action.type}"`)
19-
return editor.postMessage({
16+
export const editor = acquireVsCodeApi()
17+
18+
const editorSend = (action: T.Action) =>
19+
editor.postMessage({
2020
...action,
2121
source: 'coderoad', // filter events by source on editor side
2222
})
23-
}
2423

2524
// router finds first state match of <Route path='' />
2625
const useStateMachine = (): Output => {
2726
const [state, send] = useMachine<T.MachineContext, any>(createMachine({ editorSend }))
2827

2928
const sendWithLog = (action: T.Action): void => {
30-
logger(`SEND: ${action.type}`, action)
29+
logger(action)
3130
send(action)
3231
}
3332

@@ -43,7 +42,7 @@ const useStateMachine = (): Output => {
4342
// filter out events from other extensions
4443
return
4544
}
46-
sendWithLog(action)
45+
send(action)
4746
}
4847
window.addEventListener(listener, handler)
4948
return () => {

0 commit comments

Comments
 (0)