Skip to content

Commit 81509f8

Browse files
authored
[logging] Convert FlutterSdk class to using PluginLogger (#8427)
I'm not sure if the `FlutterSdk` class actually gets asked to call `flutter run`, `flutter attach`, or `flutter test`, but seems reasonable to check in case.
1 parent 7adfbad commit 81509f8

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/io/flutter/sdk/FlutterCommand.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,38 @@ public class FlutterCommand {
6464
/**
6565
* Returns a displayable version of the command that will be run.
6666
*/
67-
public String getDisplayCommand() {
67+
public @NotNull String getDisplayCommand() {
6868
final List<String> words = new ArrayList<>();
6969
words.add("flutter");
7070
words.addAll(type.subCommand);
7171
words.addAll(args);
7272
return String.join(" ", words);
7373
}
7474

75+
/**
76+
* Returns a displayable version of the command, with potential file paths redacted if path logging is disabled.
77+
*/
78+
public String getSanitizedDisplayCommand() {
79+
if (FlutterSettings.getInstance().isFilePathLoggingEnabled()) {
80+
return getDisplayCommand();
81+
}
82+
83+
final List<String> words = new ArrayList<>();
84+
words.add("flutter");
85+
words.addAll(type.subCommand);
86+
87+
final List<String> newArgs = new ArrayList<>(args);
88+
// For run, attach, and test commands, the last argument is typically a file path.
89+
// We redact it to avoid logging user-specific information.
90+
if (!newArgs.isEmpty() && (type == Type.RUN || type == Type.ATTACH || type == Type.TEST)) {
91+
final int lastIndex = newArgs.size() - 1;
92+
newArgs.set(lastIndex, "<path>");
93+
}
94+
95+
words.addAll(newArgs);
96+
return String.join(" ", words);
97+
}
98+
7599
protected boolean isPubRelatedCommand() {
76100
return pubRelatedCommands.contains(type);
77101
}
@@ -286,10 +310,11 @@ enum Type {
286310
TEST("Flutter test", "test");
287311

288312
final public String title;
289-
final ImmutableList<String> subCommand;
313+
final @NotNull ImmutableList<String> subCommand;
290314

291-
Type(String title, String... subCommand) {
315+
Type(String title, @NotNull String... subCommand) {
292316
this.title = title;
317+
assert subCommand != null;
293318
this.subCommand = ImmutableList.copyOf(subCommand);
294319
}
295320
}

src/io/flutter/sdk/FlutterSdk.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.flutter.FlutterBundle;
3333
import io.flutter.FlutterUtils;
3434
import io.flutter.dart.DartPlugin;
35+
import io.flutter.logging.PluginLogger;
3536
import io.flutter.module.FlutterProjectType;
3637
import io.flutter.pub.PubRoot;
3738
import io.flutter.run.FlutterDevice;
@@ -60,7 +61,7 @@ public class FlutterSdk {
6061

6162
private static final @NotNull String DART_CORE_SUFFIX = DART_SDK_SUFFIX + "/lib/core";
6263

63-
private static final @NotNull Logger LOG = Logger.getInstance(FlutterSdk.class);
64+
private static final @NotNull Logger LOG = PluginLogger.createLogger(FlutterSdk.class);
6465

6566
private static final @NotNull Map<String, FlutterSdk> projectSdkCache = new HashMap<>();
6667

@@ -443,7 +444,11 @@ public PubRoot createFiles(@NotNull VirtualFile baseDir, @Nullable Module module
443444
}
444445
}
445446
catch (InterruptedException e) {
446-
FlutterUtils.warn(LOG, e);
447+
if (FlutterSettings.getInstance().isFilePathLoggingEnabled()) {
448+
LOG.warn(e);
449+
} else {
450+
LOG.warn(e.toString());
451+
}
447452
return null;
448453
}
449454

@@ -694,22 +699,21 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
694699
}
695700
});
696701

697-
LOG.info("Calling " + command.getDisplayCommand());
698702
final long start = System.currentTimeMillis();
699703
process.startNotify();
700704
if (process.waitFor(5000)) {
701705
final long duration = System.currentTimeMillis() - start;
702-
LOG.info(command.getDisplayCommand() + ": " + duration + "ms");
706+
LOG.info(command.getSanitizedDisplayCommand() + " (" + duration + "ms)");
703707
final Integer code = process.getExitCode();
704708
if (code != null && code == 0) {
705709
return stdout.toString();
706710
}
707711
else {
708-
LOG.info("Exit code from " + command.getDisplayCommand() + ": " + code);
712+
LOG.info("Exit code " + code + " from " + command.getSanitizedDisplayCommand());
709713
}
710714
}
711715
else {
712-
LOG.info("Timeout when calling " + command.getDisplayCommand());
716+
LOG.info("Timeout when calling " + command.getSanitizedDisplayCommand());
713717
}
714718
return null;
715719
}

0 commit comments

Comments
 (0)