Skip to content

Commit ba4a2cd

Browse files
committed
(GH-100) Refactor Extension logging to an interface
In order to support common code between the Extension and an out-of-process debug adapter, the logging facility was moved to an interface and a series of concrete implementations. The Debug Adapter is not able to import the vscode namespace as it does not exist inside the editor. This commit; - Creates a ILogger interface which is passed between objects instead of the concrete implementation of Logger - Changes the current Logger object to be an OutputChannel Logger - Add a file, null and stdout logging implementation which cna be used by the Debug Adapter or during debugging the extenion itself - Update all of the references to Logger to ILogger in the extension
1 parent 867e857 commit ba4a2cd

File tree

14 files changed

+175
-105
lines changed

14 files changed

+175
-105
lines changed

client/src/commands/pdk/pdkNewClassCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import * as vscode from 'vscode';
44
import * as cp from 'child_process';
55
import ChildProcess = cp.ChildProcess;
6-
import { Logger } from '../../logging';
6+
import { ILogger } from '../../logging';
77
import { reporter } from '../../telemetry/telemetry';
88
import * as messages from '../../messages';
99

1010
export class pdkNewClassCommand {
11-
private logger: Logger = undefined;
11+
private logger: ILogger = undefined;
1212
private terminal: vscode.Terminal = undefined;
1313

14-
constructor(logger: Logger, terminal: vscode.Terminal) {
14+
constructor(logger: ILogger, terminal: vscode.Terminal) {
1515
this.logger = logger;
1616
this.terminal = terminal;
1717
}

client/src/commands/pdk/pdkNewModuleCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

33
import * as vscode from 'vscode';
4-
import { Logger } from '../../logging';
4+
import { ILogger } from '../../logging';
55
import { reporter } from '../../telemetry/telemetry';
66
import * as messages from '../../messages';
77

88
export class pdkNewModuleCommand {
9-
private logger: Logger = undefined;
9+
private logger: ILogger = undefined;
1010
private terminal: vscode.Terminal = undefined;
1111

12-
constructor(logger: Logger, terminal: vscode.Terminal) {
12+
constructor(logger: ILogger, terminal: vscode.Terminal) {
1313
this.logger = logger;
1414
this.terminal = terminal;
1515
}

client/src/commands/pdk/pdkTestCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import * as vscode from 'vscode';
44
import * as cp from 'child_process';
55
import ChildProcess = cp.ChildProcess;
6-
import { Logger } from '../../logging';
6+
import { ILogger } from '../../logging';
77
import { reporter } from '../../telemetry/telemetry';
88
import * as messages from '../../messages';
99

1010
export class pdkTestUnitCommand {
11-
private logger: Logger = undefined;
11+
private logger: ILogger = undefined;
1212
private terminal: vscode.Terminal = undefined;
1313

14-
constructor(logger: Logger, terminal: vscode.Terminal) {
14+
constructor(logger: ILogger, terminal: vscode.Terminal) {
1515
this.logger = logger;
1616
this.terminal = terminal;
1717
}

client/src/commands/pdk/pdkValidateCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import * as vscode from 'vscode';
44
import * as cp from 'child_process';
55
import ChildProcess = cp.ChildProcess;
6-
import { Logger } from '../../logging';
6+
import { ILogger } from '../../logging';
77
import { reporter } from '../../telemetry/telemetry';
88
import * as messages from '../../messages';
99

1010
export class pdkValidateCommand {
11-
private logger: Logger = undefined;
11+
private logger: ILogger = undefined;
1212
private terminal: vscode.Terminal = undefined;
1313

14-
constructor(logger: Logger, terminal: vscode.Terminal) {
14+
constructor(logger: ILogger, terminal: vscode.Terminal) {
1515
this.logger = logger;
1616
this.terminal = terminal;
1717
}

client/src/commands/pdkcommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as vscode from 'vscode';
22
import * as messages from '../../src/messages';
33
import { IConnectionManager } from '../../src/connection';
4-
import { Logger } from '../../src/logging';
4+
import { ILogger } from '../../src/logging';
55
import { pdkNewModuleCommand } from './pdk/pdkNewModuleCommand';
66
import { pdkNewClassCommand } from './pdk/pdkNewClassCommand';
77
import { pdkNewTaskCommand } from './pdk/pdkNewTaskCommand';
88
import { pdkValidateCommand } from './pdk/pdkValidateCommand';
99
import { pdkTestUnitCommand } from './pdk/pdkTestCommand';
1010

11-
export function setupPDKCommands(langID: string, connManager: IConnectionManager, ctx: vscode.ExtensionContext, logger: Logger, terminal: vscode.Terminal) {
11+
export function setupPDKCommands(langID: string, connManager: IConnectionManager, ctx: vscode.ExtensionContext, logger: ILogger, terminal: vscode.Terminal) {
1212
let newModuleCommand = new pdkNewModuleCommand(logger, terminal);
1313
ctx.subscriptions.push(newModuleCommand);
1414
ctx.subscriptions.push(vscode.commands.registerCommand(messages.PDKCommandStrings.PdkNewModuleCommandId, () => {

client/src/commands/puppet/puppetResourceCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as vscode from 'vscode';
44
import { IConnectionManager, ConnectionStatus } from '../../connection';
5-
import { Logger } from '../../logging';
5+
import { ILogger } from '../../logging';
66
import { reporter } from '../../telemetry/telemetry';
77
import * as messages from '../../messages';
88

@@ -13,9 +13,9 @@ class RequestParams implements messages.PuppetResourceRequestParams {
1313

1414
export class puppetResourceCommand {
1515
private _connectionManager: IConnectionManager = undefined;
16-
private logger: Logger = undefined;
16+
private logger: ILogger = undefined;
1717

18-
constructor(connMgr: IConnectionManager, logger: Logger) {
18+
constructor(connMgr: IConnectionManager, logger: ILogger) {
1919
this._connectionManager = connMgr;
2020
this.logger = logger;
2121
}

client/src/commands/puppetcommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as vscode from 'vscode';
22
import * as messages from '../../src/messages';
33
import { IConnectionManager } from '../../src/connection';
4-
import { Logger } from '../../src/logging';
4+
import { ILogger } from '../../src/logging';
55
import {
66
PuppetNodeGraphContentProvider, isNodeGraphFile,
77
getNodeGraphUri, showNodeGraph
88
} from '../../src/providers/previewNodeGraphProvider';
99
import { puppetResourceCommand } from '../commands/puppet/puppetResourceCommand';
1010

11-
export function setupPuppetCommands(langID:string, connManager:IConnectionManager, ctx:vscode.ExtensionContext, logger: Logger){
11+
export function setupPuppetCommands(langID:string, connManager:IConnectionManager, ctx:vscode.ExtensionContext, logger: ILogger){
1212

1313
let resourceCommand = new puppetResourceCommand(connManager, logger);
1414
ctx.subscriptions.push(resourceCommand);

client/src/connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import net = require('net');
22
import path = require('path');
33
import vscode = require('vscode');
44
import cp = require('child_process');
5-
import { Logger } from '../src/logging';
5+
import { ILogger } from '../src/logging';
66
import { LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient';
77
import { ConnectionConfiguration } from './configuration';
88
import { setupPuppetCommands } from '../src/commands/puppetcommands';
@@ -53,7 +53,7 @@ export class ConnectionManager implements IConnectionManager {
5353
private languageServerProcess = undefined;
5454
private extensionContext = undefined;
5555
private commandsRegistered = false;
56-
private logger: Logger = undefined;
56+
private logger: ILogger = undefined;
5757
private terminal: vscode.Terminal = undefined
5858

5959
public get status() : ConnectionStatus {
@@ -66,7 +66,7 @@ export class ConnectionManager implements IConnectionManager {
6666
this.logger.show()
6767
}
6868

69-
constructor(context: vscode.ExtensionContext, logger: Logger) {
69+
constructor(context: vscode.ExtensionContext, logger: ILogger) {
7070
this.logger = logger;
7171
this.extensionContext = context;
7272
}

client/src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import * as path from 'path';
55

66
import { ConnectionManager, IConnectionConfiguration, ConnectionType } from './connection';
77
import { ConnectionConfiguration } from './configuration';
8-
import { Logger } from './logging';
8+
import { ILogger } from './logging';
9+
import { OutputChannelLogger } from './logging/outputchannel';
910
import { Reporter } from './telemetry/telemetry';
1011

1112
const langID = 'puppet'; // don't change this
@@ -21,7 +22,7 @@ export function activate(context: vscode.ExtensionContext) {
2122
notifyOnNewExtensionVersion(context, puppetExtensionVersion)
2223

2324
context.subscriptions.push(new Reporter(context));
24-
var logger = new Logger();
25+
var logger = new OutputChannelLogger();
2526
connManager = new ConnectionManager(context, logger);
2627

2728
var configSettings = new ConnectionConfiguration(context);

client/src/logging.ts

Lines changed: 12 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,18 @@
33
import * as vscode from 'vscode';
44

55
export enum LogLevel {
6-
Verbose,
7-
Debug,
8-
Normal,
9-
Warning,
10-
Error
6+
Verbose,
7+
Debug,
8+
Normal,
9+
Warning,
10+
Error
1111
}
1212

13-
export class Logger {
14-
15-
private logChannel: vscode.OutputChannel;
16-
17-
// Minimum log level that is shown to users on logChannel
18-
private minimumUserLogLevel: LogLevel = undefined;
19-
20-
constructor() {
21-
this.logChannel = vscode.window.createOutputChannel("Puppet");
22-
23-
let config = vscode.workspace.getConfiguration('puppet');
24-
let logLevelName = config['languageclient']['minimumUserLogLevel'];
25-
let logLevel = this.logLevelFromString(logLevelName);
26-
27-
if(logLevel == undefined) {
28-
this.minimumUserLogLevel = LogLevel.Normal;
29-
this.error("Logger could not interpret " + logLevelName + " as a log level setting");
30-
} else {
31-
this.minimumUserLogLevel = logLevel;
32-
}
33-
}
34-
35-
public show(){
36-
this.logChannel.show();
37-
}
38-
39-
public verbose(message: string) {
40-
this.logWithLevel(LogLevel.Verbose, message);
41-
}
42-
43-
public debug(message: string) {
44-
this.logWithLevel(LogLevel.Debug, message);
45-
}
46-
47-
public normal(message: string) {
48-
this.logWithLevel(LogLevel.Normal, message);
49-
}
50-
51-
public warning(message: string) {
52-
this.logWithLevel(LogLevel.Warning, message);
53-
}
54-
55-
public error(message: string) {
56-
this.logWithLevel(LogLevel.Error, message);
57-
}
58-
59-
private logWithLevel(level: LogLevel, message) {
60-
let logMessage = this.logLevelPrefixAsString(level) + (new Date().toISOString()) + ' ' + message
61-
62-
console.log(logMessage);
63-
if (level >= this.minimumUserLogLevel) {
64-
this.logChannel.appendLine(logMessage);
65-
}
66-
}
67-
68-
private logLevelFromString(logLevelName: String): LogLevel {
69-
switch (logLevelName.toLowerCase()) {
70-
case "verbose": return LogLevel.Verbose;
71-
case "debug": return LogLevel.Debug;
72-
case "normal": return LogLevel.Normal;
73-
case "warning": return LogLevel.Warning;
74-
case "error": return LogLevel.Error;
75-
default: return undefined;
76-
}
77-
}
78-
79-
private logLevelPrefixAsString(level: LogLevel): String {
80-
switch (level) {
81-
case LogLevel.Verbose: return "VERBOSE: ";
82-
case LogLevel.Debug: return "DEBUG: ";
83-
case LogLevel.Warning: return "WARNING: ";
84-
case LogLevel.Error: return "ERROR: ";
85-
default: return "";
86-
}
87-
}
13+
export interface ILogger {
14+
show();
15+
verbose(message: string);
16+
debug(message: string);
17+
normal(message: string);
18+
warning(message: string);
19+
error(message: string);
8820
}
89-

0 commit comments

Comments
 (0)