Skip to content

Commit 317475d

Browse files
author
Akos Kitta
committed
Enable log when requested by the IDE.
- Can modify `logpath` if requested. Signed-off-by: Akos Kitta <kittaakos@typefox.io>
1 parent 0f89ca5 commit 317475d

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/extension.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { posix } from 'path';
1+
import { stat } from 'fs';
2+
import { basename } from 'path';
3+
import { promisify } from 'util';
24
import { spawnSync } from 'child_process';
35
import deepEqual from 'deep-equal';
46
import WebRequest from 'web-request';
@@ -13,6 +15,13 @@ interface LanguageServerConfig {
1315
readonly fqbn: string;
1416
readonly name?: string;
1517
}
18+
/**
19+
* `true` if the LS should generate log files into the default location. The default location is `cwd` of the process. It's very often the same
20+
* as the workspace root of the IDE, aka the sketch folder.
21+
* When it is a string, it is the folder where the log files should be generated. If the path is invalid (does not exist, not a folder),
22+
* the log files will be generated into the default location.
23+
*/
24+
readonly log?: boolean | string;
1625
readonly env?: any;
1726
readonly flags?: string[];
1827
}
@@ -71,7 +80,7 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
7180
if (!rawStdout) {
7281
if (rawStdErr) {
7382
if (rawStdErr.indexOf('compiled sketch not found in') !== -1) {
74-
vscode.window.showErrorMessage(`Sketch '${posix.basename(config.sketchPath)}' was not compiled. Please compile the sketch and start debugging again.`);
83+
vscode.window.showErrorMessage(`Sketch '${basename(config.sketchPath)}' was not compiled. Please compile the sketch and start debugging again.`);
7584
} else {
7685
vscode.window.showErrorMessage(rawStdErr);
7786
}
@@ -125,29 +134,44 @@ async function startLanguageServer(context: ExtensionContext, config: LanguageSe
125134
}
126135
if (!languageClient || !deepEqual(latestConfig, config)) {
127136
latestConfig = config;
128-
languageClient = buildLanguageClient(config);
137+
languageClient = await buildLanguageClient(config);
129138
crashCount = 0;
130139
}
131140

132141
languageServerDisposable = languageClient.start();
133142
context.subscriptions.push(languageServerDisposable);
134143
}
135144

136-
function buildLanguageClient(config: LanguageServerConfig): LanguageClient {
145+
async function buildLanguageClient(config: LanguageServerConfig): Promise<LanguageClient> {
137146
if (!serverOutputChannel) {
138147
serverOutputChannel = vscode.window.createOutputChannel('Arduino Language Server');
139148
}
140149
if (!serverTraceChannel) {
141150
serverTraceChannel = vscode.window.createOutputChannel('Arduino Language Server (trace)');
142151
}
143-
const { lsPath: command, clangdPath, cliPath, board, flags, env } = config;
152+
const { lsPath: command, clangdPath, cliPath, board, flags, env, log } = config;
144153
const args = ['-clangd', clangdPath, '-cli', cliPath, '-fqbn', board.fqbn];
145154
if (board.name) {
146155
args.push('-board-name', board.name);
147156
}
148157
if (flags && flags.length) {
149158
args.push(...flags);
150159
}
160+
if (!!log) {
161+
args.push('-log');
162+
let logPath: string | undefined = undefined;
163+
if (typeof log === 'string') {
164+
try {
165+
const stats = await promisify(stat)(log);
166+
if (stats.isDirectory()) {
167+
logPath = log;
168+
}
169+
} catch { }
170+
}
171+
if (logPath) {
172+
args.push('-logpath', logPath);
173+
}
174+
}
151175
return new LanguageClient(
152176
'ino',
153177
'Arduino Language Server',

0 commit comments

Comments
 (0)