Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad22c75

Browse files
committed
custom logger (using developer tools is messy)
1 parent a2dc33c commit ad22c75

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@
216216
"default": "python",
217217
"description": "Path to Python, you can use a custom version of Python by modifying this setting to include the full path."
218218
},
219+
"python.devOptions": {
220+
"type": "array",
221+
"default": [],
222+
"description": "Advanced options used to enable new features or produce detailed diagnostics to identify extension issues."
223+
},
219224
"python.linting.enabled": {
220225
"type": "boolean",
221226
"default": true,

src/client/common/configSettings.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as fs from 'fs';
66

77
export interface IPythonSettings {
88
pythonPath: string;
9+
devOptions: any[];
910
linting: ILintingSettings;
1011
formatting: IFormattingSettings;
1112
unitTest: IUnitTestSettings;
@@ -54,6 +55,8 @@ export class PythonSettings implements IPythonSettings {
5455
private initializeSettings() {
5556
var pythonSettings = vscode.workspace.getConfiguration("python");
5657
this.pythonPath = pythonSettings.get<string>("pythonPath");
58+
this.devOptions = pythonSettings.get<any[]>("devOptions");
59+
this.devOptions = Array.isArray(this.devOptions) ? this.devOptions : [];
5760
var lintingSettings = pythonSettings.get<ILintingSettings>("linting");
5861
if (this.linting) {
5962
Object.assign<ILintingSettings, ILintingSettings>(this.linting, lintingSettings);
@@ -88,6 +91,7 @@ export class PythonSettings implements IPythonSettings {
8891
}
8992

9093
public pythonPath: string;
94+
public devOptions: any[];
9195
public linting: ILintingSettings;
9296
public formatting: IFormattingSettings;
9397
public autoComplete: IAutoCompeteSettings;

src/client/common/logger.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as vscode from 'vscode';
2+
import * as settings from './configSettings'
3+
4+
let outChannel: vscode.OutputChannel;
5+
let pythonSettings: settings.IPythonSettings;
6+
7+
class Logger {
8+
static initializeChannel() {
9+
if (pythonSettings) return;
10+
pythonSettings = new settings.PythonSettings();
11+
if (pythonSettings.devOptions.indexOf("DEBUG") >= 0) {
12+
outChannel = vscode.window.createOutputChannel('PythonExtLog');
13+
}
14+
}
15+
16+
static write(category: string = "log", title: string = "", message: any) {
17+
Logger.initializeChannel();
18+
if (title.length > 0) {
19+
Logger.writeLine(category, "---------------------------");
20+
Logger.writeLine(category, title);
21+
}
22+
23+
Logger.writeLine(category, message);
24+
}
25+
static writeLine(category: string = "log", line: any) {
26+
console[category](line);
27+
if (outChannel) {
28+
outChannel.appendLine(line);
29+
}
30+
}
31+
}
32+
export function error(title: string = "", message: any) {
33+
Logger.write.apply(Logger, ["error", title, message]);
34+
}
35+
export function warn(title: string = "", message: any) {
36+
Logger.write.apply(Logger, ["warn", title, message]);
37+
}
38+
export function log(title: string = "", message: any) {
39+
Logger.write.apply(Logger, ["log", title, message]);
40+
}

src/client/extension.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ export function activate(context: vscode.ExtensionContext) {
4444

4545
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(PYTHON, new PythonFormattingEditProvider(context, pythonSettings, outChannel)));
4646

47-
// var isWin = /^win/.test(process.platform);
48-
// if (!isWin) {
49-
context.subscriptions.push(new LintProvider(context, pythonSettings, outChannel));//).register(rootDir);
50-
//disposables.forEach(d=> context.subscriptions.push(d));
51-
// }
47+
context.subscriptions.push(new LintProvider(context, pythonSettings, outChannel));
5248
}
5349

5450
// this method is called when your extension is deactivated

src/client/providers/jediProxy.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as child_process from 'child_process';
66
import * as vscode from 'vscode';
77
import * as path from 'path';
88
import * as settings from './../common/configSettings';
9+
import * as logger from './../common/logger';
910

1011
var proc: child_process.ChildProcess;
1112
var pythonSettings = new settings.PythonSettings();
@@ -163,12 +164,13 @@ function killProcess() {
163164
}
164165

165166
function handleError(source: string, errorMessage: string) {
166-
console.error(`Error (${source}) ${errorMessage}`);
167+
logger.error(source + ' jediProxy', `Error (${source}) ${errorMessage}`);
167168
vscode.window.showErrorMessage(`There was an error in the python extension. Error ${errorMessage}`);
168169
}
169170

170171
function spawnProcess(dir: string) {
171172
try {
173+
logger.log('child_process.spawn in jediProxy', 'Value of pythonSettings.pythonPath is :' + pythonSettings.pythonPath);
172174
proc = child_process.spawn(pythonSettings.pythonPath, ["-u", "completion.py"], {
173175
cwd: dir
174176
});
@@ -180,20 +182,23 @@ function spawnProcess(dir: string) {
180182
handleError("stderr", data);
181183
});
182184
proc.on("end", (end) => {
183-
console.error("End - " + end);
185+
logger.error('spawnProcess.end', "End - " + end);
184186
});
185187
proc.on("error", error => {
186188
handleError("error", error);
187189
});
188190

189191
proc.stdout.on("data", (data) => {
192+
//Possible there was an exception in parsing the data returned
193+
//So append the data then parse it
190194
var dataStr = previousData = previousData + data + ""
191195
var responses: any[];
192196
try {
193197
responses = dataStr.split("\n").filter(line=> line.length > 0).map(resp=> JSON.parse(resp));
194198
previousData = "";
195199
}
196200
catch (ex) {
201+
//Possible we've only received part of the data, hence don't clear previousData
197202
handleError("stdout", ex.message);
198203
return;
199204
}

0 commit comments

Comments
 (0)