Skip to content

Commit 8afc419

Browse files
committed
Make Logger service more flexible. Remove custom message logging mechanisms.
1 parent 506766e commit 8afc419

File tree

13 files changed

+179
-148
lines changed

13 files changed

+179
-148
lines changed

src/debug-adapter/adapter/adapterProxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*--------------------------------------------------------*/
44

55
import * as utils from '../utilities';
6+
import {Logger} from '../../services/Logger';
67
import {DebugProtocol} from 'vscode-debugprotocol';
78

89
export type EventHandler = (event: DebugProtocol.Event) => void;
@@ -77,7 +78,7 @@ export class AdapterProxy {
7778
this._eventHandler(event);
7879
}
7980
} catch (e) {
80-
utils.Logger.log('Error handling adapter event: ' + (e ? e.stack : ''));
81+
Logger.error('Error handling adapter event: ' + (e ? e.stack : ''));
8182
}
8283
}
8384
}

src/debug-adapter/adapter/pathTransformer.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*--------------------------------------------------------*/
44

55
import * as utils from '../utilities';
6+
import {Logger} from '../../services/Logger';
67
import {DebugProtocol} from 'vscode-debugprotocol';
78
import * as path from 'path';
89

@@ -43,15 +44,15 @@ export class PathTransformer implements DebugProtocol.IDebugTransformer {
4344

4445
if (utils.isURL(args.source.path)) {
4546
// already a url, use as-is
46-
utils.Logger.log(`Paths.setBP: ${args.source.path} is already a URL`);
47+
Logger.log(`Paths.setBP: ${args.source.path} is already a URL`);
4748
resolve();
4849
return;
4950
}
5051

5152
const url = utils.canonicalizeUrl(args.source.path);
5253
if (this._clientPathToWebkitUrl.has(url)) {
5354
args.source.path = this._clientPathToWebkitUrl.get(url);
54-
utils.Logger.log(`Paths.setBP: Resolved ${url} to ${args.source.path}`);
55+
Logger.log(`Paths.setBP: Resolved ${url} to ${args.source.path}`);
5556
resolve();
5657
}
5758
else if (this.inferedDeviceRoot) {
@@ -71,11 +72,11 @@ export class PathTransformer implements DebugProtocol.IDebugTransformer {
7172
inferedUrl = inferedUrl.replace(`.${this._platform}.`, '.');
7273

7374
args.source.path = inferedUrl;
74-
utils.Logger.log(`Paths.setBP: Resolved (by infering) ${url} to ${args.source.path}`);
75+
Logger.log(`Paths.setBP: Resolved (by infering) ${url} to ${args.source.path}`);
7576
resolve();
7677
}
7778
else {
78-
utils.Logger.log(`Paths.setBP: No target url cached for client path: ${url}, waiting for target script to be loaded.`);
79+
Logger.log(`Paths.setBP: No target url cached for client path: ${url}, waiting for target script to be loaded.`);
7980
args.source.path = url;
8081
this._pendingBreakpointsByPath.set(args.source.path, { resolve, reject, args });
8182
}
@@ -96,7 +97,7 @@ export class PathTransformer implements DebugProtocol.IDebugTransformer {
9697
if (!this.inferedDeviceRoot && this._platform === "android")
9798
{
9899
this.inferedDeviceRoot = utils.inferDeviceRoot(this._webRoot, this._platform, webkitUrl);
99-
utils.Logger.log("\n\n\n ***Inferred device root: " + this.inferedDeviceRoot + "\n\n\n");
100+
Logger.log("\n\n\n ***Inferred device root: " + this.inferedDeviceRoot + "\n\n\n");
100101

101102
if (this.inferedDeviceRoot.indexOf("/data/user/0/") != -1)
102103
{
@@ -107,17 +108,17 @@ export class PathTransformer implements DebugProtocol.IDebugTransformer {
107108
const clientPath = utils.webkitUrlToClientPath(this._webRoot, this._platform, webkitUrl);
108109

109110
if (!clientPath) {
110-
utils.Logger.log(`Paths.scriptParsed: could not resolve ${webkitUrl} to a file in the workspace. webRoot: ${this._webRoot}`);
111+
Logger.log(`Paths.scriptParsed: could not resolve ${webkitUrl} to a file in the workspace. webRoot: ${this._webRoot}`);
111112
} else {
112-
utils.Logger.log(`Paths.scriptParsed: resolved ${webkitUrl} to ${clientPath}. webRoot: ${this._webRoot}`);
113+
Logger.log(`Paths.scriptParsed: resolved ${webkitUrl} to ${clientPath}. webRoot: ${this._webRoot}`);
113114
this._clientPathToWebkitUrl.set(clientPath, webkitUrl);
114115
this._webkitUrlToClientPath.set(webkitUrl, clientPath);
115116

116117
event.body.scriptUrl = clientPath;
117118
}
118119

119120
if (this._pendingBreakpointsByPath.has(event.body.scriptUrl)) {
120-
utils.Logger.log(`Paths.scriptParsed: Resolving pending breakpoints for ${event.body.scriptUrl}`);
121+
Logger.log(`Paths.scriptParsed: Resolving pending breakpoints for ${event.body.scriptUrl}`);
121122
const pendingBreakpoint = this._pendingBreakpointsByPath.get(event.body.scriptUrl);
122123
this._pendingBreakpointsByPath.delete(event.body.scriptUrl);
123124
this.setBreakpoints(pendingBreakpoint.args).then(pendingBreakpoint.resolve, pendingBreakpoint.reject);
@@ -141,4 +142,4 @@ export class PathTransformer implements DebugProtocol.IDebugTransformer {
141142
}
142143
});
143144
}
144-
}
145+
}

src/debug-adapter/adapter/sourceMaps/pathUtilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as Path from 'path';
88
import * as URL from 'url';
9-
9+
import {Logger} from '../../../services/Logger';
1010
import * as utils from '../../utilities';
1111

1212
export function getPathRoot(p: string) {
@@ -83,16 +83,16 @@ export function getAbsSourceRoot(sourceRoot: string, webRoot: string, generatedP
8383
}
8484
}
8585

86-
utils.Logger.log(`SourceMap: resolved sourceRoot ${sourceRoot} -> ${absSourceRoot}`);
86+
Logger.log(`SourceMap: resolved sourceRoot ${sourceRoot} -> ${absSourceRoot}`);
8787
} else {
8888
if (Path.isAbsolute(generatedPath)) {
8989
absSourceRoot = Path.dirname(generatedPath);
90-
utils.Logger.log(`SourceMap: no sourceRoot specified, using script dirname: ${absSourceRoot}`);
90+
Logger.log(`SourceMap: no sourceRoot specified, using script dirname: ${absSourceRoot}`);
9191
} else {
9292
// runtime script is not on disk, resolve the sourceRoot location on disk
9393
const scriptPathDirname = Path.dirname(URL.parse(generatedPath).pathname);
9494
absSourceRoot = Path.join(webRoot, scriptPathDirname);
95-
utils.Logger.log(`SourceMap: no sourceRoot specified, using webRoot + script path dirname: ${absSourceRoot}`);
95+
Logger.log(`SourceMap: no sourceRoot specified, using webRoot + script path dirname: ${absSourceRoot}`);
9696
}
9797
}
9898

src/debug-adapter/adapter/sourceMaps/sourceMapTransformer.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import * as path from 'path';
66
import * as fs from 'fs';
7+
import {Logger} from '../../../services/Logger';
78
import {DebugProtocol} from 'vscode-debugprotocol';
89
import {ISourceMaps, SourceMaps} from './sourceMaps';
910
import * as utils from '../../utilities';
@@ -59,7 +60,7 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
5960
const argsPath = args.source.path;
6061
const mappedPath = this._sourceMaps.MapPathFromSource(argsPath);
6162
if (mappedPath) {
62-
utils.Logger.log(`SourceMaps.setBP: Mapped ${argsPath} to ${mappedPath}`);
63+
Logger.log(`SourceMaps.setBP: Mapped ${argsPath} to ${mappedPath}`);
6364
args.authoredPath = argsPath;
6465
args.source.path = mappedPath;
6566

@@ -68,11 +69,11 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
6869
const mappedLines = args.lines.map((line, i) => {
6970
const mapped = this._sourceMaps.MapFromSource(argsPath, line, /*column=*/0);
7071
if (mapped) {
71-
utils.Logger.log(`SourceMaps.setBP: Mapped ${argsPath}:${line}:0 to ${mappedPath}:${mapped.line}:${mapped.column}`);
72+
Logger.log(`SourceMaps.setBP: Mapped ${argsPath}:${line}:0 to ${mappedPath}:${mapped.line}:${mapped.column}`);
7273
mappedCols[i] = mapped.column;
7374
return mapped.line;
7475
} else {
75-
utils.Logger.log(`SourceMaps.setBP: Mapped ${argsPath} but not line ${line}, column 0`);
76+
Logger.log(`SourceMaps.setBP: Mapped ${argsPath} but not line ${line}, column 0`);
7677
mappedCols[i] = 0;
7778
return line;
7879
}
@@ -100,10 +101,10 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
100101
});
101102
} else if (this._allRuntimeScriptPaths.has(argsPath)) {
102103
// It's a generated file which is loaded
103-
utils.Logger.log(`SourceMaps.setBP: SourceMaps are enabled but ${argsPath} is a runtime script`);
104+
Logger.log(`SourceMaps.setBP: SourceMaps are enabled but ${argsPath} is a runtime script`);
104105
} else {
105106
// Source (or generated) file which is not loaded, need to wait
106-
utils.Logger.log(`SourceMaps.setBP: ${argsPath} can't be resolved to a loaded script.`);
107+
Logger.log(`SourceMaps.setBP: ${argsPath} can't be resolved to a loaded script.`);
107108
this._pendingBreakpointsByPath.set(argsPath, { resolve, reject, args, requestSeq });
108109
return;
109110
}
@@ -131,10 +132,10 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
131132
response.breakpoints.forEach((bp, i) => {
132133
const mapped = this._sourceMaps.MapToSource(args.source.path, args.lines[i], args.cols[i]);
133134
if (mapped) {
134-
utils.Logger.log(`SourceMaps.setBP: Mapped ${args.source.path}:${bp.line}:${bp.column} to ${mapped.path}:${mapped.line}`);
135+
Logger.log(`SourceMaps.setBP: Mapped ${args.source.path}:${bp.line}:${bp.column} to ${mapped.path}:${mapped.line}`);
135136
bp.line = mapped.line;
136137
} else {
137-
utils.Logger.log(`SourceMaps.setBP: Can't map ${args.source.path}:${bp.line}:${bp.column}, keeping the line number as-is.`);
138+
Logger.log(`SourceMaps.setBP: Can't map ${args.source.path}:${bp.line}:${bp.column}, keeping the line number as-is.`);
138139
}
139140

140141
this._requestSeqToSetBreakpointsArgs.delete(requestSeq);
@@ -200,7 +201,7 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
200201
this._sourceMaps.ProcessNewSourceMap(event.body.scriptUrl, sourceMapUrlValue).then(() => {
201202
const sources = this._sourceMaps.AllMappedSources(event.body.scriptUrl);
202203
if (sources) {
203-
utils.Logger.log(`SourceMaps.scriptParsed: ${event.body.scriptUrl} was just loaded and has mapped sources: ${JSON.stringify(sources)}`);
204+
Logger.log(`SourceMaps.scriptParsed: ${event.body.scriptUrl} was just loaded and has mapped sources: ${JSON.stringify(sources)}`);
204205
sources.forEach(this.resolvePendingBreakpoints, this);
205206
}
206207
});
@@ -240,7 +241,7 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
240241
private resolvePendingBreakpoints(sourcePath: string): void {
241242
// If there's a setBreakpoints request waiting on this script, go through setBreakpoints again
242243
if (this._pendingBreakpointsByPath.has(sourcePath)) {
243-
utils.Logger.log(`SourceMaps.scriptParsed: Resolving pending breakpoints for ${sourcePath}`);
244+
Logger.log(`SourceMaps.scriptParsed: Resolving pending breakpoints for ${sourcePath}`);
244245
const pendingBreakpoint = this._pendingBreakpointsByPath.get(sourcePath);
245246
this._pendingBreakpointsByPath.delete(sourcePath);
246247

src/debug-adapter/adapter/sourceMaps/sourceMaps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as FS from 'fs';
1010
import {SourceMapConsumer} from 'source-map';
1111
import * as PathUtils from './pathUtilities';
1212
import * as utils from '../../utilities';
13-
import {Logger} from '../../utilities';
13+
import {Logger} from '../../../services/Logger';
1414

1515

1616
export interface MappingResult {

src/debug-adapter/connection/androidConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as http from 'http';
22
import {EventEmitter} from 'events';
3-
import {Logger} from '../utilities';
3+
import {Logger} from '../../services/Logger';
44
import * as Net from 'net';
55
import * as ns from '../../services/NsCliService';
66
import { INSDebugConnection } from './INSDebugConnection';

src/debug-adapter/connection/iosConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as stream from 'stream';
77
import {EventEmitter} from 'events';
88
import {INSDebugConnection} from './INSDebugConnection';
99
import * as utils from '../utilities';
10-
import {Logger} from '../utilities';
10+
import {Logger} from '../../services/Logger';
1111
import * as ns from '../../services/NsCliService';
1212

1313
interface IMessageWithId {

src/debug-adapter/utilities.ts

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -137,66 +137,6 @@ export function retryAsync(fn: () => Promise<any>, timeoutMs: number): Promise<a
137137
return tryUntilTimeout();
138138
}
139139

140-
/**
141-
* Holds a singleton to manage access to console.log.
142-
* Logging is only allowed when running in server mode, because otherwise it goes through the same channel that Code uses to
143-
* communicate with the adapter, which can cause communication issues.
144-
*/
145-
export class Logger {
146-
private static _logger: Logger;
147-
private _isServer: boolean;
148-
private _diagnosticLogCallback: (msg: string) => void;
149-
private _diagnosticLoggingEnabled: boolean;
150-
151-
public static log(msg: string, forceDiagnosticLogging = false): void {
152-
if (this._logger) this._logger._log(msg, forceDiagnosticLogging);
153-
}
154-
155-
public static init(isServer: boolean, logCallback: (msg: string) => void): void {
156-
if (!this._logger) {
157-
this._logger = new Logger(isServer);
158-
this._logger._diagnosticLogCallback = logCallback;
159-
160-
if (isServer) {
161-
Logger.logVersionInfo();
162-
}
163-
}
164-
}
165-
166-
public static enableDiagnosticLogging(): void {
167-
if (this._logger) {
168-
this._logger._diagnosticLoggingEnabled = true;
169-
if (!this._logger._isServer) {
170-
Logger.logVersionInfo();
171-
}
172-
}
173-
}
174-
175-
public static logVersionInfo(): void {
176-
Logger.log(`OS: ${os.platform() } ${os.arch() }`);
177-
Logger.log('Node version: ' + process.version);
178-
Logger.log('Adapter version: ' + require('../../package.json').version);
179-
}
180-
181-
constructor(isServer: boolean) {
182-
this._isServer = isServer;
183-
}
184-
185-
private _log(msg: string, forceDiagnosticLogging: boolean): void {
186-
if (this._isServer || this._diagnosticLoggingEnabled || forceDiagnosticLogging) {
187-
this._sendLog(msg);
188-
}
189-
}
190-
191-
private _sendLog(msg: string): void {
192-
if (this._isServer) {
193-
console.log(msg);
194-
} else if (this._diagnosticLogCallback) {
195-
this._diagnosticLogCallback(msg);
196-
}
197-
}
198-
}
199-
200140
function tryFindSourcePathInNSProject(nsProjectPath: string, additionalFileExtension: string, resorcePath: string) : string {
201141
let guesses = [];
202142
const pathParts = resorcePath.split(path.sep);

src/debug-adapter/webKitDebug.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
*--------------------------------------------------------*/
44

55
import {WebKitDebugSession} from './webKitDebugSession';
6-
import {DebugSession} from 'vscode-debugadapter';
76

8-
DebugSession.run(WebKitDebugSession);
7+
WebKitDebugSession.run(WebKitDebugSession);

0 commit comments

Comments
 (0)