Skip to content

Commit 47ad9f8

Browse files
committed
update logging across app
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 0cce0af commit 47ad9f8

File tree

19 files changed

+74
-75
lines changed

19 files changed

+74
-75
lines changed

src/actions/onRunReset.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Context from '../services/context/context'
44
import reset from '../services/reset'
55
import * as hooks from '../services/hooks'
66
import getCommitHashByPosition from '../services/reset/lastHash'
7+
import logger from '../services/logger'
78

89
type ResetAction = {
910
type: 'LATEST' | 'POSITION'
@@ -22,7 +23,7 @@ const onRunReset = async (action: ResetAction, context: Context): Promise<void>
2223
const branch = tutorial?.config.repo.branch
2324

2425
if (!branch) {
25-
console.error('No repo branch found for tutorial')
26+
logger('Error: No repo branch found for tutorial')
2627
return
2728
}
2829

src/services/git/index.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const stashAllFiles = async (): Promise<never | void> => {
99
// stash files including untracked (eg. newly created file)
1010
const { stderr } = await exec({ command: `git stash --include-untracked` })
1111
if (stderr) {
12-
console.error(stderr)
12+
logger(`Error: ${stderr}`)
1313
throw new Error('Error stashing files')
1414
}
1515
}
1616

1717
const cherryPickCommit = async (commit: string, count = 0): Promise<never | void> => {
1818
if (count > 1) {
19-
console.warn('cherry-pick failed')
19+
logger('cherry-pick failed')
2020
return
2121
}
2222
try {
@@ -26,8 +26,8 @@ const cherryPickCommit = async (commit: string, count = 0): Promise<never | void
2626
if (!stdout) {
2727
throw new Error('No cherry-pick output')
2828
}
29-
} catch (error) {
30-
console.log('cherry-pick-commit failed')
29+
} catch (error: any) {
30+
logger(`cherry-pick-commit failed: ${error.message}`)
3131
// stash all files if cherry-pick fails
3232
await stashAllFiles()
3333
return cherryPickCommit(commit, ++count)
@@ -50,10 +50,10 @@ export function loadCommit(commit: string): Promise<never | void> {
5050
export async function saveCommit(message: string): Promise<never | void> {
5151
const { stdout, stderr } = await exec({ command: `git commit -am '${message}'` })
5252
if (stderr) {
53-
console.error(stderr)
53+
logger(`Error: ${stderr}`)
5454
throw new Error('Error saving progress to Git')
5555
}
56-
logger(['save with commit & continue stdout', stdout])
56+
logger(stdout)
5757
}
5858

5959
export async function clear(): Promise<Error | void> {
@@ -63,9 +63,9 @@ export async function clear(): Promise<Error | void> {
6363
if (!stderr) {
6464
return
6565
}
66-
console.error(stderr)
67-
} catch (error) {
68-
console.error(error)
66+
logger(`Error: ${stderr}`)
67+
} catch (error: any) {
68+
logger(`Error: ${error.message}`)
6969
}
7070
throw new Error('Error cleaning up current unsaved work')
7171
}
@@ -127,7 +127,7 @@ export async function addRemote(repo: string): Promise<never | void> {
127127

128128
// validate the response is acceptable
129129
if (!alreadyExists && !successfulNewBranch) {
130-
console.error(stderr)
130+
logger(`Error: ${stderr}`)
131131
throw new Error('Error adding git remote')
132132
}
133133
}
@@ -142,7 +142,8 @@ export async function checkRemoteExists(): Promise<boolean> {
142142
// string match on remote output
143143
// TODO improve the specificity of this regex
144144
return !!stdout.match(gitOrigin)
145-
} catch (error) {
145+
} catch (error: any) {
146+
logger(`Warn: ${error.message}`)
146147
return false
147148
}
148149
}
@@ -168,8 +169,9 @@ export async function loadCommitHistory(): Promise<string[]> {
168169
}
169170
// string match on remote output
170171
return stdout.split('\n')
171-
} catch (error) {
172+
} catch (error: any) {
172173
// likely no git setup or no commits
174+
logger(`Warn: ${error.message}`)
173175
return []
174176
}
175177
}
@@ -189,8 +191,8 @@ export async function getCommitMessage(hash: string): Promise<string | null> {
189191
}
190192
// string match on remote output
191193
return stdout
192-
} catch (error) {
193-
logger('error', error)
194+
} catch (error: any) {
195+
logger(`Error: ${error.message}`)
194196
// likely no git commit message found
195197
return null
196198
}
@@ -204,8 +206,8 @@ export async function commitsExistsByMessage(message: string): Promise<boolean>
204206
return false
205207
}
206208
return !!stdout.length
207-
} catch (error) {
208-
logger('error', error)
209+
} catch (error: any) {
210+
logger(`Error: ${error.message}`)
209211
// likely no commit found
210212
return false
211213
}

src/services/hooks/utils/openFiles.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from 'path'
22
import * as vscode from 'vscode'
3+
import logger from '../../logger'
34

45
const openFiles = async (files: string[] = []): Promise<void> => {
56
if (!files.length) {
@@ -16,7 +17,7 @@ const openFiles = async (files: string[] = []): Promise<void> => {
1617
const doc = await vscode.workspace.openTextDocument(absoluteFilePath)
1718
await vscode.window.showTextDocument(doc, vscode.ViewColumn.One)
1819
} catch (error: any) {
19-
console.log(`Failed to open file ${filePath}: ${error.message}`)
20+
logger(`Failed to open file ${filePath}: ${error.message}`)
2021
}
2122
}
2223
}

src/services/hooks/utils/runCommands.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { exec } from '../../node'
22
import { send } from '../../../commands'
3+
import logger from '../../logger'
34

45
const runCommands = async (commands: string[] = []): Promise<void> => {
56
if (!commands.length) {
@@ -14,9 +15,9 @@ const runCommands = async (commands: string[] = []): Promise<void> => {
1415
let result: { stdout: string; stderr: string }
1516
try {
1617
result = await exec({ command })
17-
console.log(result)
18+
logger(result)
1819
} catch (error: any) {
19-
console.error(`Command failed: ${error.message}`)
20+
logger(`Command failed: ${error.message}`)
2021
send({ type: 'COMMAND_FAIL', payload: { process: { ...process, status: 'FAIL' } } })
2122
return
2223
}

src/services/logger/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export type Log = any
55
const logChannel = getOutputChannel('CodeRoad (Logs)')
66

77
const logger = (...messages: Log[]): void => {
8-
// Inside vscode, you console.log does not allow more than 1 param
9-
// to get around it, we can log with multiple log statements
108
for (const message of messages) {
119
if (typeof message === 'object') {
1210
logChannel.appendLine(message)

src/services/node/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as fs from 'fs'
33
import { join } from 'path'
44
import { promisify } from 'util'
55
import { WORKSPACE_ROOT } from '../../environment'
6+
import logger from '../logger'
67

78
const asyncExec = promisify(cpExec)
89
const asyncRemoveFile = promisify(fs.unlink)
@@ -35,13 +36,13 @@ export const removeFile = (...paths: string[]) => {
3536
export const readFile = (...paths: string[]): Promise<string | void> => {
3637
const filePath = getWorkspacePath(...paths)
3738
return asyncReadFile(getWorkspacePath(...paths), 'utf8').catch((err) => {
38-
console.warn(`Failed to read from ${filePath}: ${err.message}`)
39+
logger(`Failed to read from ${filePath}: ${err.message}`)
3940
})
4041
}
4142

4243
export const writeFile = (data: any, ...paths: string[]): Promise<void> => {
4344
const filePath = getWorkspacePath(...paths)
4445
return asyncWriteFile(filePath, data).catch((err) => {
45-
console.warn(`Failed to write to ${filePath}: ${err.message}`)
46+
logger(`Failed to write to ${filePath}: ${err.message}`)
4647
})
4748
}

src/services/reset/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { exec, removeFile } from '../node'
2-
2+
import logger from '../logger'
33
interface Input {
44
hash: string
55
branch: string
@@ -17,13 +17,13 @@ const reset = async ({ branch, hash }: Input): Promise<void> => {
1717
try {
1818
// if no git init, will initialize
1919
// otherwise re-initializes git
20-
await exec({ command: 'git init' }).catch(console.log)
20+
await exec({ command: 'git init' }).catch(logger)
2121

2222
// capture current branch
2323
const hasBranch = await exec({ command: 'git branch --show-current' })
2424
const localBranch = hasBranch.stdout
2525
// check if coderoad remote exists
26-
const hasRemote = await exec({ command: 'git remote -v' }).catch(console.warn)
26+
const hasRemote = await exec({ command: 'git remote -v' }).catch(logger)
2727
if (!hasRemote || !hasRemote.stdout || !hasRemote.stdout.length) {
2828
throw new Error('No remote found')
2929
} else if (!hasRemote.stdout.match(new RegExp(remote))) {
@@ -64,8 +64,7 @@ const reset = async ({ branch, hash }: Input): Promise<void> => {
6464
command: `git reset --hard ${hash}`,
6565
})
6666
} catch (error: any) {
67-
console.error('Error resetting')
68-
console.error(error.message)
67+
logger(`Error resetting: ${error.message}`)
6968
}
7069
}
7170

src/services/storage/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import { readFile, writeFile } from '../node'
33
import { SESSION_STORAGE_PATH } from '../../environment'
4+
import logger from '../logger'
45

56
// NOTE: localStorage is not available on client
67
// and must be stored in editor
@@ -46,17 +47,17 @@ class Storage<T> {
4647
return data
4748
}
4849
}
49-
} catch (err) {
50-
console.warn(`Failed to read or parse session file: ${SESSION_STORAGE_PATH}/${this.filePath}.json`)
50+
} catch (err: any) {
51+
logger(`Failed to read or parse session file: ${SESSION_STORAGE_PATH}/${this.filePath}.json: ${err.message}`)
5152
}
5253
}
5354
const value: string | undefined = await this.storage.get(this.key)
5455
if (value) {
5556
// 2. read from local storage
5657
try {
5758
return JSON.parse(value)
58-
} catch (err) {
59-
console.warn(`Failed to parse session state from local storage: ${value}`)
59+
} catch (err: any) {
60+
logger(`Failed to parse session state from local storage: ${value}: ${err.message}`)
6061
}
6162
}
6263
// 3. fallback to the default
@@ -83,7 +84,9 @@ class Storage<T> {
8384
try {
8485
writeFile(data, SESSION_STORAGE_PATH, `${this.filePath}.json`)
8586
} catch (err: any) {
86-
console.warn(`Failed to write coderoad session to path: ${SESSION_STORAGE_PATH}/${this.filePath}.json`)
87+
logger(
88+
`Failed to write coderoad session to path: ${SESSION_STORAGE_PATH}/${this.filePath}.json: ${err.message}`,
89+
)
8790
}
8891
}
8992
}

src/services/testRunner/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ const createTestRunner = (data: TT.Tutorial, callbacks: Callbacks): ((params: an
4040
// calculate level & step from position
4141
const level: TT.Level | null = data.levels.find((l) => l.id === position.levelId) || null
4242
if (!level) {
43-
console.warn(`Level "${position.levelId}" not found`)
43+
logger(`Error: Level "${position.levelId}" not found`)
4444
return
4545
}
4646
const step: TT.Step | null = level.steps.find((s) => s.id === position.stepId) || null
4747
if (!step) {
48-
console.warn(`Step "${position.stepId}" not found`)
48+
logger(`Error: Step "${position.stepId}" not found`)
4949
return
5050
}
5151

src/services/webview/render.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as vscode from 'vscode'
33
import { asyncReadFile } from '../node'
44
import { onError } from '../telemetry'
55
import { CONTENT_SECURITY_POLICY_EXEMPTIONS } from '../../environment'
6+
import logger from '../logger'
67

78
const getNonce = (): string => {
89
let text = ''
@@ -142,8 +143,8 @@ async function render(panel: vscode.WebviewPanel, rootPath: string): Promise<voi
142143
// set view
143144
panel.webview.html = html
144145
} catch (error: any) {
146+
logger(`Error: ${error.message}`)
145147
onError(error)
146-
console.error(error)
147148
}
148149
}
149150

web-app/src/components/Error/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { css, jsx } from '@emotion/core'
55
import Markdown from '../Markdown'
66
import Button from '../../components/Button'
77
import { Theme } from '../../styles/theme'
8+
import logger from '../../services/logger'
89

910
const styles = {
1011
container: (theme: Theme) => ({
@@ -42,7 +43,7 @@ const ErrorMarkdown = ({ error, send }: Props) => {
4243
React.useEffect(() => {
4344
if (error) {
4445
// log error
45-
console.log(`ERROR in markdown: ${error.message}`)
46+
logger(`ERROR in markdown: ${error.message}`)
4647
}
4748
}, [error])
4849

web-app/src/components/ErrorBoundary/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class ErrorBoundary extends React.Component {
99
// Display fallback UI
1010
this.setState({ errorMessage: error.message })
1111
// You can also log the error to an error reporting service
12-
logger('ERROR in component:', JSON.stringify(error))
13-
logger('ERROR info:', JSON.stringify(info))
12+
logger(`ERROR in component: ${JSON.stringify(error)}`)
13+
logger(`ERROR info:: ${JSON.stringify(info)}`)
1414
}
1515

1616
public render() {

web-app/src/components/Markdown/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import MarkdownIt from 'markdown-it'
22
import Prism from 'prismjs'
3-
import { css, jsx, InterpolationWithTheme } from '@emotion/core'
3+
import { css, jsx } from '@emotion/core'
44
// @ts-ignore no types for package
55
import markdownEmoji from 'markdown-it-emoji'
66
import * as React from 'react'
77
// load prism styles & language support
88
import './prism'
9+
import logger from '../../services/logger'
910

1011
// markdown highlighter instance
1112
const md: MarkdownIt = new MarkdownIt({
@@ -17,8 +18,8 @@ const md: MarkdownIt = new MarkdownIt({
1718

1819
try {
1920
hl = Prism.highlight(str, Prism.languages[lang], lang)
20-
} catch (error) {
21-
console.error(error)
21+
} catch (error: any) {
22+
logger(`Error highlighing markdown: ${error.message}`)
2223
hl = md.utils.escapeHtml(str)
2324
}
2425

@@ -66,7 +67,7 @@ const Markdown = (props: Props) => {
6667
} catch (error) {
6768
const message = `Failed to parse markdown for ${props.children}`
6869
// TODO: onError(new Error(message))
69-
console.log(message)
70+
logger(message)
7071
html = `<div style='background-color: #FFB81A; padding: 0.5rem;'>
7172
<strong style='padding-bottom: 0.5rem;'>ERROR: Failed to parse markdown</strong>
7273
<p>${props.children}</p>

web-app/src/components/Router/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react'
2+
import logger from '../../services/logger'
23

34
interface RouterProps {
45
children: any
@@ -41,7 +42,7 @@ export const Router = ({ children, route }: RouterProps) => {
4142
}
4243
const message = `No Route matches for "${JSON.stringify(route)}"`
4344
// TODO: onError(new Error(message))
44-
console.warn(message)
45+
logger(message)
4546
return null
4647
}
4748

web-app/src/containers/SelectTutorial/LoadTutorialSummary.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Dialog } from '@alifd/next'
33
import useFetch from '../../services/hooks/useFetch'
44
import * as TT from 'typings/tutorial'
55
import LoadingPage from '../Loading'
6+
import logger from '../../services/logger'
67

78
interface Props {
89
url: string
@@ -16,7 +17,7 @@ const LoadTutorialSummary = (props: Props) => {
1617
return <LoadingPage text="Loading tutorial summary..." />
1718
}
1819
if (error) {
19-
console.log(`Failed to load tutorial summary: ${error}`)
20+
logger(`Failed to load tutorial summary: ${error}`)
2021
return <div>Error loading summary</div>
2122
}
2223
if (!data) {

0 commit comments

Comments
 (0)