Skip to content

Commit 1cd8e71

Browse files
committed
chore: support LogOutputChannel in logger
1 parent 7a03927 commit 1cd8e71

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

src/entrypoint.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,32 @@ function createDebugFacade(context: vscode.ExtensionContext) {
1818
return debug;
1919
}
2020

21-
function createOutputChannel() {
22-
if (utils.Features.logOutputLanguageEnabled()) {
23-
return vscode.window.createOutputChannel(config.ExtensionPrettyName, 'log');
21+
function createLogger(context: vscode.ExtensionContext): utils.ILogger {
22+
let outputChannel;
23+
let logger;
24+
25+
if (utils.Features.hasLogOutputChannel()) {
26+
outputChannel = vscode.window.createOutputChannel(config.ExtensionPrettyName, {log: true});
27+
logger = new utils.VsCodeLogChannelLogger(outputChannel);
2428
} else {
25-
return vscode.window.createOutputChannel(config.ExtensionPrettyName);
26-
}
27-
}
28-
29-
function createLogger(context: vscode.ExtensionContext): utils.VsCodeLogger {
30-
const outputChannel = createOutputChannel();
31-
const logger = new utils.VsCodeLogger(outputChannel, getCurrentLogLevel());
32-
const logLevel = config.ConfigSections.LogLevel;
33-
const fullConfigSectionName = config.getFullConfigSection(logLevel);
34-
vscode.workspace.onDidChangeConfiguration(event => {
35-
if (!event.affectsConfiguration(fullConfigSectionName)) {
36-
return;
29+
if (utils.Features.logOutputLanguageEnabled()) {
30+
outputChannel = vscode.window.createOutputChannel(config.ExtensionPrettyName, 'log');
31+
} else {
32+
outputChannel = vscode.window.createOutputChannel(config.ExtensionPrettyName);
3733
}
38-
39-
logger.minLogLevel = getCurrentLogLevel();
40-
}, undefined, context.subscriptions);
34+
35+
const logLevelConfigSection = config.ConfigSections.LogLevel;
36+
const fullConfigSectionName = config.getFullConfigSection(logLevelConfigSection);
37+
const vsLogger = new utils.VsCodeLogger(outputChannel, getCurrentLogLevel());
38+
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(event => {
39+
if (!event.affectsConfiguration(fullConfigSectionName)) {
40+
return;
41+
}
42+
43+
vsLogger.minLogLevel = getCurrentLogLevel();
44+
}, undefined, context.subscriptions));
45+
logger = vsLogger;
46+
}
4147

4248
context.subscriptions.push(outputChannel);
4349
return logger;

src/formatter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ class PgindentDocumentFormatterProvider implements vscode.DocumentFormattingEdit
492492
}
493493
}
494494

495-
function registerDiffCommand(logger: utils.VsCodeLogger,
495+
function registerDiffCommand(logger: utils.ILogger,
496496
formatter: PgindentDocumentFormatterProvider) {
497497
/* Preview formatter changes command */
498498
vscode.commands.registerCommand(Configuration.Commands.FormatterDiffView, async () => {
@@ -508,16 +508,16 @@ function registerDiffCommand(logger: utils.VsCodeLogger,
508508
} catch (err) {
509509
logger.error('failed to format file %s', document.uri.fsPath, err);
510510
vscode.window.showErrorMessage('Failed to format document. See error in logs');
511-
logger.channel.show(true);
511+
logger.focus();
512512
return;
513513
}
514-
514+
515515
try {
516516
await vscode.commands.executeCommand('vscode.diff', document.uri, parsed, 'PostgreSQL formatting')
517517
} catch (err) {
518518
logger.error(`failed to show diff for document %s`, document.uri.fsPath, err);
519519
vscode.window.showErrorMessage('Failed to show diff. See error in logs');
520-
logger.channel.show(true);
520+
logger.focus();
521521
} finally {
522522
if (await utils.fileExists(parsed)) {
523523
await utils.deleteFile(parsed);
@@ -526,7 +526,7 @@ function registerDiffCommand(logger: utils.VsCodeLogger,
526526
});
527527
}
528528

529-
export async function registerFormatting(logger: utils.VsCodeLogger) {
529+
export async function registerFormatting(logger: utils.ILogger) {
530530
const formatter = new PgindentDocumentFormatterProvider(logger);
531531
for (const lang of ['c', 'h']) {
532532
languages.registerDocumentFormattingEditProvider({

src/utils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export interface ILogger {
116116
info: (message: string, ...args: any[]) => void;
117117
warn: (message: string, ...args: any[]) => void;
118118
error: (message: string, ...args: any[]) => void;
119+
focus: () => void;
119120
}
120121

121122
export enum LogLevel {
@@ -164,6 +165,40 @@ export class VsCodeLogger implements ILogger {
164165
error(message: string, ...args: any[]) {
165166
this.logGeneric(LogLevel.Error, 'ERROR', message, ...args);
166167
}
168+
focus() {
169+
this.channel.show(true);
170+
}
171+
}
172+
173+
export class VsCodeLogChannelLogger implements ILogger {
174+
constructor(
175+
readonly channel: vscode.LogOutputChannel) {
176+
}
177+
178+
debug(message: string, ...args: any[]) {
179+
this.channel.debug(message, ...args);
180+
}
181+
182+
info(message: string, ...args: any[]) {
183+
this.channel.info(message, ...args);
184+
}
185+
186+
warn(message: string, ...args: any[]) {
187+
this.channel.warn(message, ...args);
188+
}
189+
190+
error(message: string, ...args: any[]) {
191+
let err = undefined;
192+
if (args.length > 0 && args[args.length - 1] instanceof Error) {
193+
err = args.pop();
194+
}
195+
196+
this.channel.error(message, args);
197+
this.channel.error(err);
198+
}
199+
focus() {
200+
this.channel.show(true);
201+
}
167202
}
168203

169204
export interface IDebuggerFacade {
@@ -588,6 +623,7 @@ let hasArrayLengthFeature: boolean | undefined = undefined;
588623
let logOutputLanguageEnabled: boolean | undefined = undefined;
589624
let hasWorkspaceFs: boolean | undefined = undefined;
590625
let hasUriJoinPath: boolean | undefined = undefined;
626+
let hasLogOutputChannel: boolean | undefined = undefined;
591627

592628
export class Features {
593629
static versionAtLeast(ver: string) {
@@ -635,6 +671,14 @@ export class Features {
635671
}
636672
return hasUriJoinPath;
637673
}
674+
675+
static hasLogOutputChannel() {
676+
if (hasLogOutputChannel === undefined) {
677+
hasLogOutputChannel = this.versionAtLeast('1.74.0');
678+
}
679+
680+
return hasLogOutputChannel;
681+
}
638682
}
639683

640684
export function joinPath(base: vscode.Uri, ...paths: string[]) {

0 commit comments

Comments
 (0)