Skip to content

Commit 9f1e25a

Browse files
committed
Use chrome-debug-adapter logger
Also added extension host logger
1 parent ebadc75 commit 9f1e25a

File tree

6 files changed

+54
-124
lines changed

6 files changed

+54
-124
lines changed

src/common/logger.ts

+9-98
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,13 @@
1-
import * as fs from 'fs';
2-
3-
export enum LoggerMessageType {
4-
Log,
5-
Info,
6-
Warning,
7-
Error
1+
enum LogLevel {
2+
Verbose = 0,
3+
Log = 1,
4+
Warn = 2,
5+
Error = 3,
6+
Stop = 4,
87
}
98

10-
export interface LoggerMessageEventArgs {
11-
message: string,
12-
type: LoggerMessageType
9+
interface ILogger {
10+
log(msg: string, level?: LogLevel): void;
1311
}
1412

15-
export type LoggerHandler = ((args: LoggerMessageEventArgs) => void);
16-
type TaggedLoggerHandler = { handler: LoggerHandler, tags: string[] };
17-
18-
/**
19-
* The logger is a singleton.
20-
*/
21-
export class Logger {
22-
private _handlers: TaggedLoggerHandler[];
23-
24-
constructor() {
25-
this._handlers = [];
26-
}
27-
28-
private handleMessage(message: string, type: LoggerMessageType = LoggerMessageType.Log, tag: string = null) {
29-
for (let handler of this._handlers) {
30-
if (!handler.tags || handler.tags.length == 0 || handler.tags.indexOf(tag) > -1) {
31-
handler.handler({ message: message, type: type });
32-
}
33-
}
34-
}
35-
36-
public log(message: string, tag: string = null): void {
37-
this.handleMessage(message, LoggerMessageType.Log, tag);
38-
}
39-
40-
public info(message: string, tag: string = null): void {
41-
this.handleMessage(message, LoggerMessageType.Info, tag);
42-
}
43-
44-
public warn(message: string, tag: string = null): void {
45-
this.handleMessage(message, LoggerMessageType.Warning, tag);
46-
}
47-
48-
public error(message: string, tag: string = null): void {
49-
this.handleMessage(message, LoggerMessageType.Error, tag);
50-
}
51-
52-
public addHandler(handler: LoggerHandler, tags: string[] = null) {
53-
tags = tags || [];
54-
this._handlers.push({ handler: handler, tags: tags });
55-
}
56-
57-
/**
58-
* Removes all occurrence of this handler, ignoring the associated tags
59-
*/
60-
public removeHandler(handlerToRemove: LoggerHandler) {
61-
let i = this._handlers.length;
62-
while (i--) {
63-
if (this._handlers[i].handler == handlerToRemove) {
64-
this._handlers.splice(i, 1);
65-
}
66-
}
67-
}
68-
}
69-
70-
export namespace Tags {
71-
export const FrontendMessage: string = "LoggerTag.FrontendMessage";
72-
}
73-
74-
export namespace Handlers {
75-
export function stdStreamsHandler(args: LoggerMessageEventArgs) {
76-
var message = args.message.replace(/\n$/, "");
77-
switch(args.type) {
78-
case LoggerMessageType.Log:
79-
console.log(message);
80-
break;
81-
case LoggerMessageType.Info:
82-
console.info(message);
83-
break;
84-
case LoggerMessageType.Warning:
85-
console.warn(message);
86-
break;
87-
case LoggerMessageType.Error:
88-
console.error(message);
89-
break;
90-
}
91-
};
92-
93-
export function createStreamHandler(stream: fs.WriteStream, encoding: string = 'utf8'): LoggerHandler {
94-
let isStreamClosed = false;
95-
stream.on('close', () => { isStreamClosed = true; });
96-
return (args: LoggerMessageEventArgs) => {
97-
if (stream && !isStreamClosed) {
98-
stream.write(args.message, encoding);
99-
}
100-
}
101-
}
102-
}
13+
export { ILogger, LogLevel }

src/debug-adapter/nativeScriptDebugAdapter.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChromeDebugAdapter } from 'vscode-chrome-debug-core';
1+
import { ChromeDebugAdapter, logger } from 'vscode-chrome-debug-core';
22
import { OutputEvent, TerminatedEvent, Event } from 'vscode-debugadapter';
33
import * as utils from '../common/utilities';
44
import {IosProject} from '../project/iosProject';
@@ -9,7 +9,6 @@ import * as path from 'path';
99
import {DebugResult} from '../project/project';
1010
import * as extProtocol from '../common/extensionProtocol';
1111
import { DebugProtocol } from 'vscode-debugprotocol';
12-
import {Logger} from '../common/logger';
1312
import {NativeScriptCli} from '../project/nativeScriptCli';
1413

1514
export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
@@ -55,7 +54,7 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
5554
this._session.sendEvent(new Event(extProtocol.BEFORE_DEBUG_START));
5655

5756
const tnsPath = await this.callRemoteMethod<string>('workspaceConfigService', 'tnsPath');
58-
const cli = new NativeScriptCli(tnsPath, new Logger());
57+
const cli = new NativeScriptCli(tnsPath, logger);
5958

6059
const project = args.platform == "ios" ?
6160
new IosProject(args.appRoot, cli) :

src/main.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,28 @@ import {IosProject} from './project/iosProject';
66
import {AndroidProject} from './project/androidProject';
77
import * as utils from './common/utilities';
88
import * as extProtocol from './common/extensionProtocol';
9+
import { ChannelLogger } from './services/channelLogger';
10+
import { ILogger } from './common/logger';
911

1012
// this method is called when the extension is activated
1113
export function activate(context: vscode.ExtensionContext) {
1214
Services.globalState = context.globalState;
1315
Services.cliPath = Services.workspaceConfigService.tnsPath || Services.cliPath;
1416

15-
Services.analyticsService.initialize();
17+
const channel = vscode.window.createOutputChannel("NativeScript Extension");
18+
const logger = new ChannelLogger(channel);
19+
Services.logger = logger;
1620

1721
// Check if NativeScript CLI is installed globally and if it is compatible with the extension version
1822
let cliVersion = Services.cli().version;
1923
if (!cliVersion.isCompatible) {
2024
vscode.window.showErrorMessage(cliVersion.errorMessage);
2125
}
2226

23-
let channel = createInfoChannel(cliVersion.version.toString());
27+
logExtensionInfo(logger, cliVersion.version.toString());
28+
29+
Services.analyticsService.initialize();
30+
2431
let showOutputChannelCommand = vscode.commands.registerCommand('nativescript.showOutputChannel', () => {
2532
channel.show();
2633
});
@@ -99,14 +106,11 @@ export function activate(context: vscode.ExtensionContext) {
99106
context.subscriptions.push(showOutputChannelCommand);
100107
}
101108

102-
function createInfoChannel(cliVersion: string): vscode.OutputChannel {
103-
let channel = vscode.window.createOutputChannel("NativeScript Extension");
109+
function logExtensionInfo(logger: ILogger, cliVersion: string): void {
104110
const packageJSON = vscode.extensions.getExtension("Telerik.nativescript").packageJSON;
105111

106-
packageJSON.version && channel.appendLine(`Version: ${packageJSON.version}`);
107-
packageJSON.buildVersion && channel.appendLine(`Build version: ${packageJSON.buildVersion}`);
108-
packageJSON.commitId && channel.appendLine(`Commit id: ${packageJSON.commitId}`);
109-
channel.appendLine(`NativeScript CLI: ${cliVersion}`);
110-
111-
return channel;
112+
packageJSON.version && logger.log(`Version: ${packageJSON.version}`);
113+
packageJSON.buildVersion && logger.log(`Build version: ${packageJSON.buildVersion}`);
114+
packageJSON.commitId && logger.log(`Commit id: ${packageJSON.commitId}`);
115+
logger.log(`NativeScript CLI: ${cliVersion}`);
112116
}

src/project/nativeScriptCli.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {spawn, execSync, ChildProcess} from 'child_process';
22
import {Version} from '../common/version';
3-
import {Logger, Tags} from '../common/logger';
3+
import { ILogger } from '../common/logger';
44
import * as utils from '../common/utilities';
55
import * as os from 'os';
66

@@ -47,9 +47,9 @@ export class NativeScriptCli {
4747
private _path: string;
4848
private _shellPath: string;
4949
private _cliVersion: CliVersion;
50-
private _logger: Logger;
50+
private _logger: ILogger;
5151

52-
constructor(cliPath: string, logger: Logger) {
52+
constructor(cliPath: string, logger: ILogger) {
5353
this._path = cliPath;
5454
this._logger = logger;
5555

@@ -66,7 +66,7 @@ export class NativeScriptCli {
6666
versionStr = this.executeSync(["--version"], undefined);
6767
}
6868
catch(e) {
69-
this._logger.log(e, Tags.FrontendMessage);
69+
this._logger.log(e);
7070
throw new Error("NativeScript CLI not found. Use 'nativescript.tnsPath' workspace setting to explicitly set the absolute path to the NativeScript CLI.");
7171
}
7272
let cliVersion: Version = versionStr ? Version.parse(versionStr) : null;
@@ -85,15 +85,15 @@ export class NativeScriptCli {
8585
public executeSync(args: string[], cwd: string): string {
8686
args.unshift("--analyticsClient", "VSCode");
8787
let command: string = `${this._path} ${args.join(' ')}`;
88-
this._logger.log(`[NativeScriptCli] execute: ${command}\n`, Tags.FrontendMessage);
88+
this._logger.log(`[NativeScriptCli] execute: ${command}`,);
8989

9090
return execSync(command, { encoding: "utf8", cwd: cwd, shell: this._shellPath}).toString().trim();
9191
}
9292

9393
public execute(args: string[], cwd: string): ChildProcess {
9494
args.unshift("--analyticsClient", "VSCode");
9595
let command: string = `${this._path} ${args.join(' ')}`;
96-
this._logger.log(`[NativeScriptCli] execute: ${command}\n`, Tags.FrontendMessage);
96+
this._logger.log(`[NativeScriptCli] execute: ${command}`);
9797

9898
let options = { cwd: cwd, shell: this._shellPath };
9999
let child: ChildProcess = spawn(this._path, args, options);

src/services/channelLogger.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ILogger, LogLevel } from '../common/logger';
2+
import { OutputChannel } from 'vscode';
3+
4+
export class ChannelLogger implements ILogger {
5+
private minLogLevel: LogLevel = LogLevel.Log;
6+
private channel: OutputChannel;
7+
8+
constructor(channel: OutputChannel) {
9+
this.channel = channel;
10+
}
11+
12+
log(msg: string, level: LogLevel = LogLevel.Log): void {
13+
if (level >= this.minLogLevel) {
14+
this.channel.appendLine(msg);
15+
}
16+
}
17+
}

src/services/services.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import {Logger} from '../common/logger';
1+
import {ILogger} from '../common/logger';
22
import {NativeScriptCli} from '../project/nativeScriptCli';
33

44
export class Services {
55
protected _cliPath: string;
66

7-
protected _logger: Logger;
7+
protected _logger: ILogger;
88
protected _cli: NativeScriptCli;
99

1010
public get cliPath(): string { return this._cliPath; }
1111

1212
public set cliPath(cliPath: string) { this._cliPath = cliPath; }
1313

14-
public logger(): Logger {
15-
this._logger = this._logger || new Logger();
16-
return this._logger;
17-
}
14+
public get logger(): ILogger { return this._logger; }
15+
16+
public set logger(logger: ILogger) { this._logger = logger; }
1817

1918
public cli(): NativeScriptCli {
20-
this._cli = this._cli || new NativeScriptCli(this._cliPath, this.logger());
19+
this._cli = this._cli || new NativeScriptCli(this._cliPath, this.logger);
2120
return this._cli;
2221
}
2322
}

0 commit comments

Comments
 (0)