Skip to content

Commit

Permalink
Merge #6026 from justinmk3/fixlogger
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmk3 authored Nov 15, 2024
2 parents feb9155 + a527f96 commit 9eb37d2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
15 changes: 9 additions & 6 deletions packages/core/src/dev/beta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import { CancellationError } from '../shared/utilities/timeoutUtils'
import { isAmazonQ, isCloud9, productName } from '../shared/extensionUtilities'
import * as config from './config'
import { isReleaseVersion } from '../shared/vscode/env'
import { getRelativeDate } from '../shared/datetime'

const localize = nls.loadMessageBundle()
const logger = getLogger('dev/beta')

const downloadIntervalMs = 1000 * 60 * 60 * 24 // A day in milliseconds

Expand Down Expand Up @@ -57,12 +59,13 @@ export async function activate(ctx: vscode.ExtensionContext) {
* If this is the first time we are watching the beta version or if its been 24 hours since it was last checked then try to prompt for update
*/
export function watchBetaVSIX(vsixUrl: string): vscode.Disposable {
getLogger().info(`dev: watching ${vsixUrl} for beta artifacts`)

const toolkit = getBetaToolkitData(vsixUrl)
const lastCheckRel = toolkit ? getRelativeDate(new Date(toolkit.lastCheck)) : ''
logger.info('watching beta artifacts url (lastCheck: %s): %s', lastCheckRel, vsixUrl)

if (!toolkit || toolkit.needUpdate || Date.now() - toolkit.lastCheck > downloadIntervalMs) {
runAutoUpdate(vsixUrl).catch((e) => {
getLogger().error('runAutoUpdate failed: %s', (e as Error).message)
logger.error('runAutoUpdate failed: %s', (e as Error).message)
})
}

Expand All @@ -71,13 +74,13 @@ export function watchBetaVSIX(vsixUrl: string): vscode.Disposable {
}

async function runAutoUpdate(vsixUrl: string) {
getLogger().debug(`dev: checking ${vsixUrl} for a new version`)
logger.debug(`checking url for a new version: %s`, vsixUrl)

try {
await telemetry.aws_autoUpdateBeta.run(() => checkBetaUrl(vsixUrl))
} catch (e) {
if (!isUserCancelledError(e)) {
getLogger().warn(`dev: beta extension auto-update failed: %s`, e)
logger.warn('beta extension auto-update failed: %s', e)
}
}
}
Expand Down Expand Up @@ -165,7 +168,7 @@ async function promptInstallToolkit(pluginPath: vscode.Uri, newVersion: string,
switch (response) {
case installBtn:
try {
getLogger().info(`dev: installing artifact ${vsixName}`)
logger.info(`installing artifact: ${vsixName}`)
await vscode.commands.executeCommand('workbench.extensions.installExtension', pluginPath)
await updateBetaToolkitData(vsixUrl, {
lastCheck: Date.now(),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/shared/fs/watchedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ export abstract class WatchedFiles<T> implements vscode.Disposable {
item: item,
}
} else {
getLogger().info(`${this.name}: failed to process: ${uri}`)
getLogger().debug(`${this.name}: failed to process: ${uri}`)
// if value isn't valid for type, remove from registry
this.registryData.delete(pathAsString)
}
} catch (e) {
if (!quiet) {
throw e
}
getLogger().info(`${this.name}: failed to process: ${uri}: ${(e as Error).message}`)
getLogger().error(`${this.name}: failed to process: ${uri}: ${(e as Error).message}`)
}
return undefined
}
Expand Down
34 changes: 19 additions & 15 deletions packages/core/src/shared/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as vscode from 'vscode'

export type LogTopic = 'crashReport' | 'notifications' | 'test' | 'unknown'
export type LogTopic = 'crashReport' | 'dev/beta' | 'notifications' | 'test' | 'unknown'

class ErrorLog {
constructor(
Expand Down Expand Up @@ -143,24 +143,18 @@ function prependTopic(topic: string, message: string | Error): string | ErrorLog
}

/**
* Gets the logger if it has been initialized
* the logger is of `'main'` or `undefined`: Main logger; default impl: logs to log file and log output channel
* Gets the global default logger.
*
* @param topic: topic to be appended in front of the message.
*/
export function getLogger(topic?: LogTopic): Logger {
const logger = toolkitLoggers['main']
if (!logger) {
return new ConsoleLogger()
}
return new TopicLogger(topic ?? 'unknown', logger)
// `TopicLogger` will lazy-load the "main" logger when it becomes available.
return new TopicLogger(topic ?? 'unknown', 'main')
}

export function getDebugConsoleLogger(topic?: LogTopic): Logger {
const logger = toolkitLoggers['debugConsole']
if (!logger) {
return new ConsoleLogger()
}
return new TopicLogger(topic ?? 'unknown', logger)
// `TopicLogger` will lazy-load the "debugConsole" logger when it becomes available.
return new TopicLogger(topic ?? 'unknown', 'debugConsole')
}

// jscpd:ignore-start
Expand Down Expand Up @@ -215,15 +209,25 @@ export class ConsoleLogger extends BaseLogger {
}

/**
* Wraps a `ToolkitLogger` and defers to it for everything except `topic`.
* Wraps the specified `ToolkitLogger` and defers to it for everything except `topic`.
*
* Falls back to `ConsoleLogger` when the logger isn't available yet (during startup).
*/
export class TopicLogger extends BaseLogger implements vscode.Disposable {
// HACK: crude form of "lazy initialization", to support module-scope assignment of
// `getLogger()` without being sensitive to module-load ordering. So even if logging isn't ready
// at the time of the `getLogger` call, it will recover later. (This is a bit hacky, because it
// arguably doesn't belong in `TopicLogger`.)
public get logger() {
return toolkitLoggers[this.loggerKey] ?? new ConsoleLogger()
}

/**
* Wraps a `ToolkitLogger` and defers to it for everything except `topic`.
*/
public constructor(
public override topic: LogTopic,
public readonly logger: Logger
public readonly loggerKey: keyof typeof toolkitLoggers
) {
super()
}
Expand Down

0 comments on commit 9eb37d2

Please sign in to comment.